我想将日志记录与消息字典一起使用。
例如,记录器附加了一个格式化程序:
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")