如何防止日志警告? [英] How to prevent logging warning?

查看:107
本文介绍了如何防止日志警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要将一些日志记录调用添加到我拥有的库中。我怎么能阻止使用该库的脚本打印

''当没有处理程序时,可以找到logger" comtypes.client"''

脚本运行?


我想设置日志记录,以便在没有配置任何内容时没有日志记录,也没有警告消息打印。


托马斯

I''m about to add some logging calls to a library I have. How can I
prevent that the script that uses the library prints
''No handlers could be found for logger "comtypes.client"'' when the
script runs?

I would like to setup the logging so that there is no logging when
nothing is configured, and no warning messages are printed.

Thomas

推荐答案



可能是这样你会找到有用的:


def getLog(logName,fileName = None):


if fileName:

hdl = logging.FileHandler(fileName)

else:

hdl = logging.StreamHandler()


fmt = logging.Formatter(" ;%(name)s:\t%(levelname)s:\ t%(作为ctime)s:\t%(message)s")

hdl.setFormatter(fmt)

log = logging.getLogger(logName)

log.addHandler(hdl)


返回日志

Thomas Heller写道:

may be this you will find usefull:

def getLog(logName, fileName = None):

if fileName:
hdl = logging.FileHandler(fileName)
else:
hdl = logging.StreamHandler()

fmt = logging.Formatter("%(name)s:\t%(levelname)s:\t%(as ctime)s:\t%(message)s")
hdl.setFormatter(fmt)
log = logging.getLogger(logName)
log.addHandler(hdl)

return log
Thomas Heller wrote:
我''即将添加一些日志调用到我拥有的库。我怎样才能防止使用该库的脚本打印
'当
脚本运行时,无法找到logger" comtypes.client"''的处理程序?

我想设置日志记录,以便在没有配置任何内容时没有日志记录,也不会打印任何警告消息。

Thomas
I''m about to add some logging calls to a library I have. How can I
prevent that the script that uses the library prints
''No handlers could be found for logger "comtypes.client"'' when the
script runs?

I would like to setup the logging so that there is no logging when
nothing is configured, and no warning messages are printed.

Thomas



-

祝你好运,

Maksim Kasimov

mailto: ma ************ @ gmail.com


> ;托马斯海勒写道:
> Thomas Heller wrote:
我即将添加一些日志调用到我拥有的库。我怎样才能防止使用该库的脚本打印
'当
脚本运行时,无法找到logger" comtypes.client"''的处理程序?
我想设置日志记录,以便在没有配置任何内容时没有记录,也不会打印任何警告消息。
I''m about to add some logging calls to a library I have. How can I
prevent that the script that uses the library prints
''No handlers could be found for logger "comtypes.client"'' when the
script runs?
I would like to setup the logging so that there is no logging when
nothing is configured, and no warning messages are printed.



Maksim Kasimov< ma **** ********@gmail.com>写道:

可能是你会发现有用的:

def getLog(logName,fileName = None):

如果fileName:
hdl = logging.FileHandler(fileName)
否则:
hdl = logging.StreamHandler()

fmt = logging.Formatter("%(name)s:\ t%(levelname)s:\ t%(作为ctime)s:\t%(message)s")
hdl.setFormatter(fmt)
log = logging.getLogger(logName)
log.addHandler(hdl)

返回日志


Maksim Kasimov <ma************@gmail.com> writes:
may be this you will find usefull:

def getLog(logName, fileName = None):

if fileName:
hdl = logging.FileHandler(fileName)
else:
hdl = logging.StreamHandler()

fmt = logging.Formatter("%(name)s:\t%(levelname)s:\t%(as ctime)s:\t%(message)s")
hdl.setFormatter(fmt)
log = logging.getLogger(logName)
log.addHandler(hdl)

return log




不是 - 我知道如何设置处理程序,但我认为应该是

可选。假设我已经


log = logging.getLogger(" comtypes.client")


及更高版本


log.warn(" foo bar")


库代码。


如果我使用库我的脚本,我收到上面提到的警告

以上。我希望默认情况下脚本与库

logging无关。当我想查看日志消息时,我总是可以在脚本中查看日志消息中的
logging.basicConfig()





当我在

库中添加''NULL''处理程序时,我得到了我想要的行为,但这真的是如何记录日志的使用?


< library>

log = logging.getLogger(" comtypes.client")


class NULLHandler(logging.Handler):

def emit(self,* args):

pass


log .addHandler(NULLHandler())

< / library>


Thomas



Not really - I know how to set up handlers, but I think it should be
optional. Assume I have

log = logging.getLogger("comtypes.client")

and later

log.warn("foo bar")

in the library code.

If I use the library an my script, I get the warning that I mentioned
above. I want the script by default to be agnostic about the libraries
logging. When I want to see the log messages, I can always do

logging.basicConfig()

in the script to see the log messages.

I get the behaviour that I want when I add a ''NULL'' handler in the
library, but is this really how logging is intended to be used?

<library>
log = logging.getLogger("comtypes.client")

class NULLHandler(logging.Handler):
def emit(self, *args):
pass

log.addHandler(NULLHandler())
</library>

Thomas


Thomas Heller写道:
Thomas Heller wrote:
Thomas Heller写道:

Thomas Heller wrote:

我即将添加一些记录调用我所拥有的库。我怎样才能防止使用该库的脚本打印
'当
脚本运行时,无法找到logger" comtypes.client"''的处理程序?
我想设置日志记录,以便在没有配置任何内容时没有记录,也不会打印任何警告消息。
I''m about to add some logging calls to a library I have. How can I
prevent that the script that uses the library prints
''No handlers could be found for logger "comtypes.client"'' when the
script runs?
I would like to setup the logging so that there is no logging when
nothing is configured, and no warning messages are printed.



Maksim卡西莫夫< ma ************ @ gmail.com>写道:



Maksim Kasimov <ma************@gmail.com> writes:

这可能是你会发现有用的:

def getLog(logName,fileName = None):

if fileName :
hdl = logging.FileHandler(fileName)
否则:
hdl = logging.StreamHandler()

fmt = logging.Formatter("%(name) s:\t%(levelname)s:\ t%(作为ctime)s:\t%(message)s")
hdl.setFormatter(fmt)
log = logging.getLogger (logName)
log.addHandler(hdl)

返回日志
may be this you will find usefull:

def getLog(logName, fileName = None):

if fileName:
hdl = logging.FileHandler(fileName)
else:
hdl = logging.StreamHandler()

fmt = logging.Formatter("%(name)s:\t%(levelname)s:\t%(as ctime)s:\t%(message)s")
hdl.setFormatter(fmt)
log = logging.getLogger(logName)
log.addHandler(hdl)

return log



不是真的 - 我知道如何设置处理程序,但我认为它应该是可选的。假设我有log / logging.getLogger(" comtypes.client")

以及后来

log.warn(" foo bar" ;)

在库代码中。

如果我使用我的脚本库,我收到上面提到的警告
。我希望默认情况下脚本与库
日志记录无关。当我想查看日志消息时,我总是可以在脚本中看到日志消息。



>当我在
库中添加''NULL''处理程序时,我得到了我想要的行为,但是这真的是如何使用日志记录吗?

< library>
log = logging.getLogger(" comtypes.client")
类NULLHandler(logging.Handler):
def emit(self,* args):
传递

log.addHandler(NULLHandler())
< / library>

Thomas



Not really - I know how to set up handlers, but I think it should be
optional. Assume I have

log = logging.getLogger("comtypes.client")

and later

log.warn("foo bar")

in the library code.

If I use the library an my script, I get the warning that I mentioned
above. I want the script by default to be agnostic about the libraries
logging. When I want to see the log messages, I can always do

logging.basicConfig()

in the script to see the log messages.

I get the behaviour that I want when I add a ''NULL'' handler in the
library, but is this really how logging is intended to be used?

<library>
log = logging.getLogger("comtypes.client")

class NULLHandler(logging.Handler):
def emit(self, *args):
pass

log.addHandler(NULLHandler())
</library>

Thomas



你好,


Absolutley没有,我有完全相同的问题,这是非常讨厌的
。日志处理程序应该从外部的

配置文件中设置,而不是在代码中明确设置。我只需要用它来生活

,这很烦人但是你无能为力(简短

玩stdout但是避免这是为什么你首先使用了记录

!)。


干杯,


Neil


-


Neil Benn

高级自动化工程师

Cenix BioScience

BioInnovations Zentrum

Tatzberg 46

D-01307

德累斯顿

德国


电话:+49(0)351 4173 154

电子邮件: be ** @ cenix-bioscience.com

Cenix网站: http: //www.cenix-bioscience.com


Hello,

Absolutley not, I have exactly the same problem and it is
really annoying. The logging handlers should be set from an external
config file and not explicitly in the code. I simply had to just live
with it, it''s annoying but there is nothing you can do about it (short
of playing around with stdout but avoiding that is why you used logging
in the first place!).

Cheers,

Neil

--

Neil Benn
Senior Automation Engineer
Cenix BioScience
BioInnovations Zentrum
Tatzberg 46
D-01307
Dresden
Germany

Tel : +49 (0)351 4173 154
e-mail : be**@cenix-bioscience.com
Cenix Website : http://www.cenix-bioscience.com


这篇关于如何防止日志警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆