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")
令人惊叹的!很容易!谢谢;)