简体   繁体   English

从多个模块记录到同一个日志文件

[英]Logging from multiple modules to the same log file

I have a simple project structure - my main module calls two other modules residing within the same directory.我有一个简单的项目结构 - 我的主模块调用驻留在同一目录中的另外两个模块。 I followed the instructions per this answer to set up my test.按照这个答案的说明来设置我的测试。

My code is as follows:我的代码如下:

Main module:主要模块:

# log_test.py
import logging
import imported_1
import imported_2

def main():

    logger = logging.getLogger(__name__)
    logging.basicConfig(filename="test.log", format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
    logger.setLevel(logging.DEBUG)
    logger.debug("This is a debug message")
    logger.info("For your info")
    logger.warning("This is a warning message")
    logger.error("This is an error message")
    logger.critical("This is a critical message")

    imported_1.one_fn(5, 6)
    imported_2.two_fn(10, 20)

if __name__ == '__main__':
    main()

The imported modules - imported_1.py导入的模块 -imported_1.py

# imported_1.py
import logging
logger = logging.getLogger(__name__)

def one_fn(x, y):
    print(f"Logging function one with {x} and {y}")
    logger.info(f"Logging function one with {x} and {y}")

And imported_2.py和imported_2.py

# imported_2.py
import logging
logger = logging.getLogger(__name__)

def two_fn(x, y):
    print(f"Logging function two with {x} and {y}")
    logger.info(f"Logging function one with {x} and {y}")

The generated log file test.log only contains entries from the main log_test.py module.生成的日志文件test.log仅包含来自主log_test.py模块的条目。 The imported modules are not logged here:导入的模块没有记录在这里:

2019-12-21 18:26:41,351 - __main__ - DEBUG - This is a debug message
2019-12-21 18:26:41,351 - __main__ - INFO - For your info
2019-12-21 18:26:41,351 - __main__ - WARNING - This is a warning message
2019-12-21 18:26:41,351 - __main__ - ERROR - This is an error message

I am looking for log messages from the imported modules to show up in the same log file as specified by basicConfig.我正在寻找来自导入模块的日志消息,以显示在 basicConfig 指定的同一日志文件中。 What am I doing wrong here?我在这里做错了什么?

In order for the loggers of imported1 and import2 to know the original logger you created, they need to be its children.为了让imported1 和import2 的记录器知道您创建的原始记录器,它们需要是它的子代。
In python loggers are arranged by '.', so logger abc is a child of logger ab which is a child of a在python记录器由设置“”,所以记录器abc是记录器的一个子ab这是一个子a

You can achieve what you want this way:你可以通过这种方式实现你想要的:
In log_test.pylog_test.py

logger = logging.getLogger('my_logger')

In imported_1.pyimported_1.py

logger = logging.getLogger('my_logger.' + __name__) # will be my_logger.imported_1

In imported_2.pyimported_2.py

logger = logging.getLogger('my_logger.' + __name__) # will be my_logger.imported_2

You can check out more here https://docs.python.org/3/howto/logging-cookbook.html#using-logging-in-multiple-modules您可以在此处查看更多信息https://docs.python.org/3/howto/logging-cookbook.html#using-logging-in-multiple-modules

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

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