【问题标题】:Python Logging - dictConfig - logging destination for sub modulesPython 日志记录 - dictConfig - 子模块的日志记录目标
【发布时间】:2017-06-04 08:54:08
【问题描述】:

我有一个 python 日志服务器、两个测试应用程序和一个共享模块 (submod.py) 我希望两个应用程序都能够将日志事件发送到服务器并让服务器决定如何将它们存储到单独的日志文件中。在共享模块开始记录之前,这相当容易,我不知道如何让服务器识别子模块正在发送日志事件以存储到正确的日志文件中的程序。

我的日志服务器是我找到的代码的略微修改版本here

我尝试修改它以使用类似于以下的字典日志记录配置:

test_log.conf

"handlers": {
            "console": {
                "class": "logging.StreamHandler",
                "level": "DEBUG",
                "formatter": "complex",
                "stream": "ext://sys.stdout"
            },
            "test_01": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "complex",
                "filename": "test_01.log",
                "mode": "a",
                "backupCount": 5,
                "encoding": "utf8"
            },
            "test_02": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "complex",
                "filename": "test_02.log",
                "mode": "a",
                "backupCount": 5,
                "encoding": "utf8"
            },
            "file": {
                "class": "logging.handlers.RotatingFileHandler",
                "level": "INFO",
                "formatter": "complex",
                "filename": "root.log",
                "mode": "a",
                "backupCount": 5,
                "encoding": "utf8"
            }
        },
        "loggers": {
            "root": {
                "level": "INFO",
                "handlers": ["console", "file"]
            },
            "test_01":{
                "level": "INFO",
                "handlers": ["console", "test_01"]
            },
            "test_02": {
                "level": "INFO",
                "handlers": ["console", "test_02"]
            }
        }

test_01.py

main_logger = logging.getLogger('')
main_logger.setLevel(logging.DEBUG)

socketHandler = logging.handlers.SocketHandler('localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT)

main_logger.addHandler(socketHandler)

logging.info('Test 01 main program')

a = submod.SubClass()

test_02.py

main_logger = logging.getLogger('')
main_logger.setLevel(logging.DEBUG)

socketHandler = logging.handlers.SocketHandler('localhost', logging.handlers.DEFAULT_TCP_LOGGING_PORT)

main_logger.addHandler(socketHandler)

logging.info('Test 02 main program')

a = submod.SubClass()

submod.py

class SubClass(object):
    def __init__(self):
        log = logging.getLogger()
        log.debug('Debug')
        log.info('Info')
        log.warn('Warning')
        log.error('Error')
        log.critical('Critical')
        print(__name__)

当 test_01 和 test_02 都在调用它时,如何让日志服务器智能地知道从 submod.py 记录消息的位置。

对于格式和令人困惑的解释,我深表歉意,这个问题在这一点上对我造成了脑损伤。

已编辑: 为了清楚起见并重新措辞不好的解释。

【问题讨论】:

    标签: python logging socketserver


    【解决方案1】:

    只需使用一个配置文件,您就可以根据使用它的程序预定义日志文件的目的地。 Python“日志”模块完成了你需要的所有任务;这是一个配置文件的示例:http://www.zetadev.com/software/aspen/trunk/doc/html/logging-conf.html

    【讨论】:

    • 据我了解,您引用的配置并不能解决问题。我正在尝试有一个动态配置,它可以应用于使用相同子模块的多个程序,但让服务器智能地确定何时应将来自子模块的消息保存到每个主程序的日志文件中。
    • 知道了,那这个怎么样? docs.python.org/2/howto/…
    • 这可行,我希望在应用程序之间重用模块,因此静态分配父级有点痛苦。有没有办法确定调用或的“父母”?或者我应该只是将调用它的父级的名称作为变量传递并设置它。
    • 将日志记录模块编写为装饰器,然后您所需要做的就是为您的每个父母添加一行@logging_module。
    猜你喜欢
    • 1970-01-01
    • 2011-10-21
    • 2016-08-15
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多