如何添加自定义参数到Python日志格式化?[已解决] [英] How to add custom parameter into Python logging formatter?[Solved]
本文介绍了如何添加自定义参数到Python日志格式化?[已解决]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
admin - 2013-10-11 15:11:47,033 action0
user1 - 2013-10-11 15:11:48,033 action1
user2 - 2013-10-11 15:11:49,033 action2
admin - 2013-10-11 15:11:50,033 action3
我正在使用RotatingFileHandler :
def get_user_name():
return session.get(username,)
file_handler = RotatingFileHandler(fname,maxBytes = 1 * 1024 * 1024,backupCount = 5)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
'%(username)s - %(asctime)s%(levelname)-10s%(message)s [in%(pathname)s:%(lineno)d]'
))#如何插入get_user_name )而不是%(用户名)s?
app.logger.addHandler(file_handler)
什么是正确的方法做这样的记录器?感谢。
解决方案
有许多方法,记录在这里。
I using standard Python logging module with Flask framework. I want to have one log file with the all records of users actions with custom parameter - %(username)s to logging.Formatter:
admin - 2013-10-11 15:11:47,033 action0
user1 - 2013-10-11 15:11:48,033 action1
user2 - 2013-10-11 15:11:49,033 action2
admin - 2013-10-11 15:11:50,033 action3
I'm using RotatingFileHandler:
def get_user_name():
return session.get("username", "")
file_handler = RotatingFileHandler(fname, maxBytes=1 * 1024 * 1024, backupCount=5)
file_handler.setLevel(logging.DEBUG)
file_handler.setFormatter(logging.Formatter(
'%(username)s - %(asctime)s %(levelname)-10s %(message)s [in %(pathname)s:%(lineno)d]'
)) # how to insert get_user_name() instead %(username)s?
app.logger.addHandler(file_handler)
What is the right way to do such logger? Thanks.
解决方案
There are a number of approaches, documented here.
这篇关于如何添加自定义参数到Python日志格式化?[已解决]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文