Python 设置日志记录级别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38537905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:06:10  来源:igfitidea点击:

Set logging levels

pythonlogging

提问by Luis Ramon Ramirez Rodriguez

I'm trying to use the standard library to debug my code:

我正在尝试使用标准库来调试我的代码:

This works fine:

这工作正常:

import logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
logger.info('message')

I can't make work the logger for the lower levels:

我无法为较低级别的记录器工作:

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.info('message')

logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
logger.debug('message')

I don't get any response for neither of those.

我没有得到任何回应。

回答by spinkus

What Python version? That workd for me in 3.4. But note that basicConfig()won't affect the root handler if it's already setup:

什么Python版本?这在 3.4 中对我有用。但请注意,如果已经设置,basicConfig()不会影响根处理程序:

This function does nothing if the root logger already has handlers configured for it.

如果根记录器已经为其配置了处理程序,则此函数不执行任何操作。

To set the level on root explicitly do logging.getLogger().setLevel(logging.DEBUG). But ensure you've called basicConfig()before hand so the root logger initially has some setup. I.e.:

要在 root 上显式设置级别,请执行logging.getLogger().setLevel(logging.DEBUG). 但请确保您basicConfig()事先已调用,以便根记录器最初进行了一些设置。IE:

import logging
logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
logging.getLogger('foo').debug('bah')
logging.getLogger().setLevel(logging.INFO)
logging.getLogger('foo').debug('bah')

Also note that "Loggers" and their "Handlers" both have distinct independent log levels. So if you've previously explicitly loaded some complex logger config in you Python script, and that has messed with the root logger's handler(s), then this can have an effect, and just changing the loggers log level with logging.getLogger().setLevel(..)may not work. This is because the attached handler may have a log level set independently. This is unlikely to be the case and not something you'd normally have to worry about.

另请注意,“记录器”及其“处理程序”都具有不同的独立日志级别。因此,如果您之前在 Python 脚本中显式加载了一些复杂的记录器配置,并且与根记录器的处理程序相混淆,那么这可能会产生影响,仅更改记录器日志级别logging.getLogger().setLevel(..)可能不起作用。这是因为附加的处理程序可能具有独立设置的日志级别。这不太可能是这种情况,您通常不必担心。

回答by Tim Seed

I use the following setup for logging

我使用以下设置进行日志记录

Yaml based config

基于 Yaml 的配置

Create a yaml file called logging.ymllike this

像这样创建一个名为logging.yml的 yaml 文件

version: 1

formatters:
    simple:
        format: "%(name)s - %(lineno)d -  %(message)s"

    complex:
        format: "%(asctime)s - %(name)s - %(lineno)d -  %(message)s"


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

    file:
        class: logging.handlers.TimedRotatingFileHandler
        when: midnight
        backupCount: 5
        level: DEBUG
        formatter: simple
        filename : Thrift.log

loggers:

    qsoWidget:
        level: INFO
        handlers: [console,file]
        propagate: yes

    __main__:   
        level: DEBUG
        handlers: [console]
        propagate: yes

Python - The main

Python - 主要

The "main" module should look like this

“主”模块应该是这样的

import logging.config
import logging

with open('logging.yaml','rt') as f:
        config=yaml.safe_load(f.read())
        f.close()
logging.config.dictConfig(config)
logger=logging.getLogger(__name__)
logger.info("Contest is starting")

Sub Modules/Classes

子模块/类

These should start like this

这些应该像这样开始

import logging

class locator(object):
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        self.logger.debug('{} initialized')

Hope that helps you...

希望能帮到你...