[英]Python Logging - dictConfig - logging destination for sub modules
我有一個python日志記錄服務器,兩個測試應用程序和一個共享模塊(submod.py)我希望兩個應用程序能夠將日志事件發送到服務器並讓服務器決定如何將它們存儲到單獨的日志文件中。 這是相當容易的,直到共享模塊開始記錄,我不知道如何允許服務器識別子模塊正在發送日志事件以存儲到正確的日志文件的程序。
我的日志記錄服務器是我在這里找到的代碼的略微修改版本
我試圖修改它以使用類似於以下的字典日志記錄配置:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.