在python中使用多个参数进行日志记录

csh*_*der 2 python formatting logging

在 python 日志记录模块中,日志使用以下格式进行格式化:

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

**simple_example.py**
# 'application' code
logger.debug('debug message')
logger.info('info message')
logger.warning('warn message')
Run Code Online (Sandbox Code Playgroud)

这给出了如下输出:

输出:

2005-03-19 15:10:26,618 - simple_example - DEBUG - debug message
2005-03-19 15:10:26,620 - simple_example - INFO - info message
2005-03-19 15:10:26,695 - simple_example - WARNING - warn message
Run Code Online (Sandbox Code Playgroud)

我只是想知道是否有任何方法可以在末尾而不是在中间添加多条消息,例如

 My custom message 1  - simple_example - DEBUG - my custom message 2
Run Code Online (Sandbox Code Playgroud)

有什么办法可以格式化它:

formatter = logging.Formatter('%(message1)s - %(name)s - %(levelname)s - %(message2)s')
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激

Mik*_*tty 5

您可以编写自己的 Formatter 类并将额外的消息作为 kwargs 传递:

import logging

class MyFormatter(logging.Formatter):
    def format(self, record):
        record.message2 = record.args.get("message2")
        return super().format(record)

logger = logging.getLogger('test')
ch = logging.StreamHandler()
formatter = MyFormatter('%(asctime)s - %(message2)s - %(name)s - %(levelname)s - %(message)s')
ch.setFormatter(formatter)
ch.setLevel(logging.ERROR)
logger.addHandler(ch)

logger.error("debug message", {"message2": "Blub"})
Run Code Online (Sandbox Code Playgroud)

输出:

2019-02-08 14:33:50,487 - Blub - 测试 - 错误 - 调试消息


编辑:我不知道,为什么这不能在 INFO 级别开箱即用,但您可以执行以下操作,这将起作用:

import logging

class MyFormatter(logging.Formatter):
    def format(self, record):
        record.message2 = record.args.get("message2")
        return super().format(record)

logger = logging.getLogger('test')
ch = logging.StreamHandler()
ch.setFormatter(MyFormatter('%(asctime)s - %(message2)s - %(name)s - %(levelname)s - %(message)s'))
logging.basicConfig( level=logging.INFO, handlers=[ch] )

logger.info("debug message", {"message2": "Blub"})
Run Code Online (Sandbox Code Playgroud)

输出:

2019-02-11 12:53:17,014 - Blub - 测试 - 信息 - 调试消息


编辑 2:为了在不提供带有message2的 dict 的情况下工作,您可以按如下方式更改代码:

import logging

class MyFormatter(logging.Formatter):
    def format(self, record):
        record.message2 = ""
        if(record.args):
            record.message2 = record.args.get("message2", "Fallback Value")
        return super().format(record)

logger = logging.getLogger('test')
ch = logging.StreamHandler()
ch.setFormatter(MyFormatter('%(asctime)s - %(message2)s - %(name)s - %(levelname)s - %(message)s'))
logging.basicConfig( level=logging.INFO, handlers=[ch] )

logger.info("debug message", {"message2": "Blub"})
logger.info("This is my sample log")
logger.info("This is my sample log", {"hello": "World"})
Run Code Online (Sandbox Code Playgroud)

输出:

2019-02-11 13:20:53,419 - Blub - test - INFO - debug message
2019-02-11 13:20:53,419 -  - test - INFO - This is my sample log
2019-02-11 13:20:53,419 - Fallback Value - test - INFO - This is my sample log
Run Code Online (Sandbox Code Playgroud)