Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.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 3.x Python日志记录-使用带有键/代码的预格式化消息文件_Python 3.x_Logging - Fatal编程技术网

Python 3.x Python日志记录-使用带有键/代码的预格式化消息文件

Python 3.x Python日志记录-使用带有键/代码的预格式化消息文件,python-3.x,logging,Python 3.x,Logging,我想将日志记录与消息字典一起使用 例如,记录器附带了格式化程序: formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 我想要一个这样的文件,例如带有预配置格式消息的文件: ERROR-1234 : "The entity : {1} doesn't exist" ERROR-4321 : "The client : {1} with the name {2} doesn't

我想将日志记录与消息字典一起使用

例如,记录器附带了格式化程序:

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
我想要一个这样的文件,例如带有预配置格式消息的文件:

ERROR-1234 : "The entity : {1} doesn't exist"
ERROR-4321 : "The client : {1} with the name {2} doesn't exist"
我打电话的时候可能是这样的:

logger.error(ERROR-1234, "entity-1")
logger.error(ERROR-4321, "25", "John Smith")
结果呢

2018-05-31 16:55:42,584 - Example - ERROR- The entity : entity-1 doesn't exist}
2018-05-31 16:55:42,584 - Example - ERROR- The client : 25 with the name John Smith doesn't exist}
我们可以通过日志库实现这种行为(比如Log4J)吗

谢谢

制作一个模块:

# my_error_messages.py
errors = {
    1234: "The entity : %s doesn't exist",
    4321: "The client : %s with the name %s doesn't exist",
}
在代码中:

from my_error_messages import errors

logger.error(errors[4321], "25", "John Smith")

令人惊叹的!很容易!谢谢;)