简体   繁体   English

获取当前(或基本)python 日志配置作为字典

[英]Get current (or basic) python logging configuration as a dictionary

I am using the Python standard logging configuration, combined with yaml to load a config file with the dictConfig() function:我正在使用 Python 标准日志配置,结合yaml使用dictConfig()函数加载配置文件:

import logging
import yaml
 
with open('logging.yaml','r') as f:
    logConfig = yaml.safe_load(f.read())
logging.config.dictConfig(logConfig)

Since an incremental logging configuration in python limits the capabilities , every logging file has to contain a minimum amount of information, something like this:由于python中的增量日志配置限制了功能,因此每个日志文件都必须包含最少量的信息,如下所示:

version: 1
formatters:
    simple:
        format: "%(asctime)s - %(name)s - %(levelname)s - %(message)s"

handlers:
    console:
        class: logging.StreamHandler
        level: DEBUG
        formatter: simple

loggers:
    my_module:
        level: ERROR

root:
    level: INFO
    handlers: [console] 

Which either forces one to learn this by heart, store it somewhere, or look it up each time.这要么迫使人们记住这一点,将其存储在某处,要么每次都查找。 Since neither of these really work for me, I would like to find a way to generate it.由于这些都不适合我,我想找到一种方法来生成它。 This brings me to the question:这让我想到了一个问题:

Is there a way to get the current (or basic) logging config as a dictionary?有没有办法将当前(或基本)日志配置作为字典获取?

This would make it easy to make an initial config file by running the following code once, and just remove/edit what you want:通过运行以下代码,这将使创建初始配置文件变得容易,只需删除/编辑您想要的内容:

import logging
import yaml

logConfig = logging.get_current_config_as_a_dictionary()
with open('logging.yaml','w') as f:
    f.write(yaml.dump(logConfig))

Yaml is just my personal preference of course, the same question could be posted for something like JSON. Yaml 当然只是我个人的喜好,同样的问题也可以针对 JSON 之类的内容发布。

I'm not sure if there's a clean way do to this through any public logging function.我不确定是否有一种干净的方法可以通过任何公共logging功能来做到这一点。 Looking through the package CPython source , the defaults aren't set aside and neatly defined in any structure (such as a dict).查看包 CPython source ,默认值没有放在一边,并在任何结构(例如字典)中整齐地定义。 Instead, they're hardcoded into the function logging.basicConfig .相反,它们被硬编码到函数logging.basicConfig Specifically, this function contains some common "default" setup keywords, but unfortunately for your purposes, these are hardcoded into the function.具体来说,此函数包含一些常见的“默认”设置关键字,但不幸的是,出于您的目的,这些已硬编码到函数中。 For example:例如:

style = kwargs.pop("style", '%')

This uses the root logger (which itself is root = RootLogger(logging.WARNING) ) and adds some config to it.这使用root记录器(它本身是root = RootLogger(logging.WARNING) )并添加一些配置。

One way to work with that, which is really not ideal, would be to look at the "before and after" config tied to logging.root before and after you call logging.basicConfig() *.一种处理这种情况的方法,这实际上并不理想,是在调用logging.basicConfig() * 之前和之后查看与logging.root相关的“之前和之后”配置。 For example:例如:

>>> root = logging.root
>>> root.handlers
[]
>>> logging.basicConfig()
>>> root.handlers
[<StreamHandler <stderr> (NOTSET)>]

Lastly, I would mention that the default style comes from:最后,我要提到默认样式来自:

logging._STYLES['%']

which works out to %(levelname)s:%(name)s:%(message)s .这适用于%(levelname)s:%(name)s:%(message)s

*If you call this function with no arguments, there is really not a whole lot done; *如果你不带参数调用这个函数,真的没有做很多事情; the main change is adding a StreamHandler (stdout) to the root logger and then adding a Formatter to that.主要的变化是向根记录器添加一个StreamHandler (stdout),然后向其中添加一个Formatter

I don't see a complete solution for you, but the logging_tree package looks like a step in the right direction.我没有看到适合您的完整解决方案,但logging_tree 包看起来像是朝着正确方向迈出的一步。 If you look at its source code, you may be able to change its output to match the dictionary you're looking for.如果您查看它的源代码,您也许能够更改其输出以匹配您要查找的字典。

Here's the output it currently generates, from its home page:这是它当前从其主页生成的输出:

>>> logging.getLogger('a')
>>> logging.getLogger('a.b').setLevel(logging.DEBUG)
>>> logging.getLogger('x.c')
>>> from logging_tree import printout
>>> printout()
<--""
   Level WARNING
   |
   o<--"a"
   |   Level NOTSET so inherits level WARNING
   |   |
   |   o<--"a.b"
   |       Level DEBUG
   |
   o<--[x]
       |
       o<--"x.c"
           Level NOTSET so inherits level WARNING

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

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