简体   繁体   English

记录器配置以记录到文件

[英]logger configuration to log to file

Python newbie. Python新手。 Now I have been stuck around this for a while. 现在我已经坚持了一段时间。 When I try to write logs in a file using ini configuration nothing is captured in the file. 当我尝试使用ini配置在文件中写入日志时,文件中未捕获任何内容。 I tried to debug the problem but couldn't get any clue. 我试图调试问题,但是没有任何线索。 Writing logs without using ini file works perfectly fine. 在不使用ini文件的情况下编写日志非常正常。

Below is the code and ini file 下面是代码和ini文件

import logging

from logging.config import fileConfig

def info(message):


    fileConfig('logging_config.ini')
    logger=logging.getLogger("simple logger")

    logger.warning('Something is not right')
    logger.warning(message)

logging_config.ini logging_config.ini

[loggers]
keys=root

[handlers]
keys=file_handler

[logger_root]
level=WARNING
handlers=file_handler

[formatters]
keys=formatter

[formatter_formatter]
format='%(message)s'

[handler_file_handler]
class=FileHandler
level=WARNING
formatter=formatter
args=('dummyoutput.log','w')

I checked the logger object also to see if I can get any clue from its properties. 我还检查了记录器对象,以查看是否可以从其属性中获得任何线索。 Below is the object 下面是对象

{'disabled': 0,
 'filters': [],
 'handlers': [<logging.FileHandler object at 0x7ff03358ce90>],
 'level': 30,
 'name': 'root',
 'parent': None,
 'propagate': 1}

Not sure if its helpful but I noticed the property disabled was earlier shown TRUE but now it is 0 every time. 不知道它是否有用,但是我注意到disabled的属性先前显示为TRUE但现在每次都为0

Has anyone got any clue on this? 有人对此有任何线索吗?

Update: The issue was due to multiple calls to logging.config.fileConfig() for the same configuration file. 更新:问题是由于对同一配置文件多次调用logging.config.fileConfig()引起的。 But I couldn't really understand why nothing was written when that function was called for the last time. 但是我真的不明白为什么最后一次调用该函数时什么也没写。 Any idea on that?? 有什么想法吗?

There is a naming convention for loggers, and the name "simple logger" is not valid because of the space. 记录器有一个命名约定,并且由于空间限制,名称“ simple logger”无效。

You should replace the space by a period. 您应该用句点代替空格。 We usually use Python package names for loggers. 我们通常将Python软件包名称用于记录器。

Here is how you can fix that: 解决方法如下:

import logging

from logging.config import fileConfig


def info(message):

    fileConfig('logging_config.ini')
    logger = logging.getLogger("simple.logger")

    logger.warning('Something is not right')
    logger.warning(message)


if __name__ == "__main__":
    info("hello")

It woks perfectly. 完美地苏醒。

'Something is not right'
'hello'

Quoting Logger Objects: 引用记录器对象:

The name is potentially a period-separated hierarchical value, like foo.bar.baz (though it could also be just plain foo, for example). 该名称可能是用句点分隔的分层值,例如foo.bar.baz (例如foo,也可以是纯foo, )。 Loggers that are further down in the hierarchical list are children of loggers higher up in the list. 层次结构列表中位于最下方的记录器是列表中较高处的记录器的子级。 For example, given a logger with a name of foo , loggers with names of foo.bar , foo.bar.baz , and foo.bam are all descendants of foo . 例如,给定名称为foo记录器,名称为foo.barfoo.bar.bazfoo.bam记录器都是foo后代。 The logger name hierarchy is analogous to the Python package hierarchy, and identical to it if you organise your loggers on a per-module basis using the recommended construction logging.getLogger(__name__) . 记录器名称层次结构类似于Python包层次结构,如果您使用建议的构造logging.getLogger(__name__)在每个模块的基础上组织记录器,则记录器名称层次结构与Python包层次结构相同。 That's because in a module, __name__ is the module's name in the Python package namespace. 这是因为在模块中, __name__是Python包命名空间中模块的名称。

Figured out what the (stupid) mistake was. 弄清楚什么是(愚蠢的)错误。 Actually the info(message) was getting called more than once and so was fileConfig() for the same configuration file as it was inside info function. 实际上, info(message)被调用了不止一次,因此同一个配置文件的fileConfig()info函数内部一样。 Hence, the issue. 因此,问题。 Modified code as below and it worked. 修改代码如下,它可以工作。

import logging
from logging.config import fileConfig

def info(message):

    logger.warning('Something is not right')
    logger.warning(message)

fileConfig('logging_config.ini')
logger=logging.getLogger("simple.logger")

Update: Even if we don't follow the logger naming convention for eg. 更新:即使我们不遵循记录器命名约定,例如。 as I gave simple logger instead of simple.logger it works fine. 正如我给simple logger而不是simple.logger它工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM