Python 日志记录 - dictConfig - 子模块的日志记录目的地
Python Logging - dictConfig - logging destination for sub modules
我有一个 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" 模块完成您需要的所有任务;这是配置文件的示例:http://www.zetadev.com/software/aspen/trunk/doc/html/logging-conf.html
我有一个 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" 模块完成您需要的所有任务;这是配置文件的示例:http://www.zetadev.com/software/aspen/trunk/doc/html/logging-conf.html