Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的多参数日志记录_Python_Logging_Formatting - Fatal编程技术网

python中的多参数日志记录

python中的多参数日志记录,python,logging,formatting,Python,Logging,Formatting,在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') 其输出如下所

在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')
其输出如下所示:

输出:

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
我只是想知道是否有任何方法可以添加多条消息,不是在最后,而是在中间,例如

 My custom message 1  - simple_example - DEBUG - my custom message 2
是否有任何方法可以将其格式化为:

formatter = logging.Formatter('%(message1)s - %(name)s - %(levelname)s - %(message2)s')

欢迎提供任何帮助。您可以直接在
格式化程序中使用自定义消息,并使用
%(消息)s
放置日志消息

参见下面的示例

formatter = logging.Formatter('My custome message 1 - %(name)s - %(levelname)s - %(message)s my custom message 2')

您可以编写自己的格式化程序类,并将额外消息作为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"})
输出:

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
2019-02-08 14:33:50487-Blub-测试-错误-调试消息


编辑:我不知道,为什么这在信息级别上不起作用,但您可以执行以下操作:

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"})
输出:

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
2019-02-11 12:53:17014-Blub-测试-信息-调试消息


编辑2:若要在不提供带有消息2的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"})
输出:

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

通过自定义消息的含义,它意味着每次都会包含不同的值,因此这不是一个正确的答案感谢发布示例,只是想知道,为什么它不使用setLevel作为信息,即ch.setLevel(logging.INFO)和logger.INFO(“消息”),再次感谢,我尝试做的最后一件事是,如果我这样编辑,那么我的默认日志消息不起作用,即logger.info(“这是我的示例日志”)给我AttributeError:“tuple”对象没有属性“get”