简体   繁体   English

Python日志记录 - 如何继承根记录器级别和处理程序

[英]Python Logging - How to inherit root logger level & handler

I am a python newbie, trying to implement logging into my code. 我是一个python新手,试图实现登录我的代码。 I have two modules 我有两个模块

main.py submodule.py main.py submodule.py

main.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 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

I thought as longs as I call the getLogger from my submodule, it should inherit the log level & handler details from root logger. 我认为,当我从子模块调用getLogger时,它应该从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. 想法是根据主模块中设置的日志级别,将主模块和子模块打印在同一日志中的单个日志文件。

Thanks in advance 提前致谢

I am sorry, as this could be a duplicate question and I did go through those similar questions, but couldn't figure out how this works. 对不起,因为这可能是一个重复的问题,我确实经历了那些类似的问题,但无法弄清楚这是如何工作的。 Hence posting this question. 因此发布这个问题。 I am not intentionally creating a duplicate bcz I didn't lookup for it. 我不是故意创建一个重复的bcz我没有查找它。

The problem here is that you're not initializing the root logger; 这里的问题是你没有初始化根记录器; you're initializing the logger for your main module. 您正在初始化主模块的记录器。

Try this for main.py: 试试这个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()

Then try this for submodule.py: 然后尝试使用submodule.py:

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

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). 由于您说要将所有子模块中的日志消息发送到同一位置,因此应初始化根记录器,然后只需使用消息记录方法(以及setlevel()调用)。 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. 因为你的子模块没有显式处理程序,所以logging.getLogger(__name__)将遍历树到根,在那里它将找到你在main.py中建立的处理程序。

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

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