Python记录-如何继承root记录器级别&处理程序 [英] Python Logging - How to inherit root logger level & handler

查看:115
本文介绍了Python记录-如何继承root记录器级别&处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python新手,试图实现登录到我的代码中.我有两个模块

I am a python newbie, trying to implement logging into my code. I have two modules

main.py submodule.py

main.py submodule.py

main.py

import logging
from logging.handlers import RotatingFileHandler
import submodule


import logging
from logging.handlers import RotatingFileHandler

logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

fh = RotatingFileHandler('master.log', maxBytes=2000000, backupCount=10)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

logger.debug('DEBUG LEVEL - MAIN MODULE')
logger.info('INFO LEVEL - MAIN MODULE')

submodule.loggerCall()

submodule.py

import logging
from logging.handlers import RotatingFileHandler


def loggerCall():
    logger = logging.getLogger(__name__)
#    logger.setLevel(logging.DEBUG)

    fh = RotatingFileHandler('master.log', maxBytes=2000000, backupCount=10)
    formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    fh.setFormatter(formatter)
    logger.addHandler(fh)

    logger.debug('SUBMODULE: DEBUG LOGGING MODE : ')
    logger.info('Submodule: INFO LOG')

    return

我认为,只要我从子模块调用getLogger,它就应该继承日志级别&来自根记录器的处理程序详细信息.但是,就我而言,我必须在子模块中再次指定日志级别和处理程序,以使其打印到同一日志文件中.

I thought as longs as I call the getLogger from my submodule, it should inherit the log level & handler details from root logger. However, in my case, I have to specify log level and handler again in submodule to get them print to same log file.

此外,如果我在子模块中有很多方法和类.我该如何处理而不必定义我的日志级别&再次处理.

Also, If I have lots of methods, and classes inside my submodule. How can I go about it without having to define my log level & handler again.

想法将只有一个日志文件,其中主模块和子模块将根据主模块中设置的日志级别在同一日志中打印.

Idea is to have a single log file with main, and sub modules printing in the same log based on the log level set in the main module.

预先感谢

很抱歉,这可能是一个重复的问题,我的确曾回答过类似的问题,但无法弄清楚它是如何工作的.因此发布这个问题.我不是故意创建一个重复的bcz,我没有查找它.

推荐答案

这里的问题是您没有初始化根记录程序;您正在初始化主模块的记录器.

The problem here is that you're not initializing the root logger; you're initializing the logger for your main module.

尝试使用main.py:

Try this for main.py:

import logging
from logging.handlers import RotatingFileHandler
import submodule

logger = logging.getLogger()  # Gets the root logger
logger.setLevel(logging.DEBUG)

fh = RotatingFileHandler('master.log', maxBytes=2000000, backupCount=10)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(formatter)
logger.addHandler(fh)

logger.debug('DEBUG LEVEL - MAIN MODULE')
logger.info('INFO LEVEL - MAIN MODULE')

submodule.loggerCall()

然后为submodule.py尝试一下:

Then try this for submodule.py:

def loggerCall():
    logger = logging.getLogger(__name__)
    logger.debug('SUBMODULE: DEBUG LOGGING MODE : ')
    logger.info('Submodule: INFO LOG')
    return

由于您说过要将所有子模块的日志消息发送到同一位置,因此应该初始化根记录器,然后简单地使用消息记录方法(适当时还有setlevel()调用).因为子模块没有显式的处理程序,所以logging.getLogger(__name__)会将树遍历到根,在树的根目录中将找到您在main.py中建立的处理程序.

Since you said you wanted to send log messages from all your submodules to the same place, you should initialize the root logger and then simply use the message logging methods (along with setlevel() calls, as appropriate). Because there's no explicit handler for your submodule, logging.getLogger(__name__) will traverse the tree to the root, where it will find the handler you established in main.py.

这篇关于Python记录-如何继承root记录器级别&处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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