如何实现全局Python记录器?

时间:2016-06-22 04:29:10

标签: python logging global

如何为所有python文件实现全局记录器?一些相关的SE问题是onetwo,但这两个问题都不是我想要的。我也希望在控制台中看到日志文件输出。

1 个答案:

答案 0 :(得分:6)

你这样做:

main.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import log
import test

logger = log.setup_custom_logger('root')

def main():
    logger.info("informational message")
    logger.debug("debugging message")
    logger.critical("critical message")
    test.test_message()
    return 0

if __name__ == '__main__':
    main()

log.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging
import logging.handlers

def setup_custom_logger(name):
    # logger settings
    log_file = "log/testing.log"
    log_file_max_size = 1024 * 1024 * 20 # megabytes
    log_num_backups = 3
    log_format = "%(asctime)s [%(levelname)s]: %(filename)s(%(funcName)s:%(lineno)s) >> %(message)s"
    log_date_format = "%m/%d/%Y %I:%M:%S %p"
    log_filemode = "w" # w: overwrite; a: append

    # setup logger
    # datefmt=log_date_format
    logging.basicConfig(filename=log_file, format=log_format, filemode=log_filemode ,level=logging.DEBUG)
    rotate_file = logging.handlers.RotatingFileHandler(
        log_file, maxBytes=log_file_max_size, backupCount=log_num_backups
    )
    logger = logging.getLogger(name)
    logger.addHandler(rotate_file)

    # print log messages to console
    consoleHandler = logging.StreamHandler()
    logFormatter = logging.Formatter(log_format)
    consoleHandler.setFormatter(logFormatter)
    logger.addHandler(consoleHandler)

    return logger

# source: https://docs.python.org/2/howto/logging.html
# logger.debug("")      // Detailed information, typically of interest only when diagnosing problems.
# logger.info("")       // Confirmation that things are working as expected.
# logger.warning("")    // An indication that something unexpected happened, or indicative of some problem in the near future
# logger.error("")      // Due to a more serious problem, the software has not been able to perform some function.
# logger.critical("")   // A serious error, indicating that the program itself may be unable to continue running.

test.py

#!/usr/bin/python
# -*- coding: utf-8 -*-

import logging

logger = logging.getLogger('root')

def test_message():
    logger.warning("warning message")
    logger.error("error message")
    return 0

确保您有一个名为log的目录,用于testing.log的文件python main.py。对于使用日志记录的每个文件,您必须像test.py中一样调用这两行:import logginglogger = logging.getLogger('root')

示例控制台和日志文件输出:

2016-06-21 23:24:43,945 [DEBUG]: main.py(main:11) >> debugging message
2016-06-21 23:24:43,945 [INFO]: main.py(main:10) >> informational message    
2016-06-21 23:24:43,945 [CRITICAL]: main.py(main:12) >> critical message
2016-06-21 23:24:43,946 [WARNING]: test.py(test_message:9) >> warning message
2016-06-21 23:24:43,946 [ERROR]: test.py(test_message:10) >> error message