如何更改 python 应用程序引擎中日志消息的默认格式?
我想默认在来自请求处理程序的日志消息中记录模块和类名。
通常的方法似乎是通过调用logging.basicConfig来设置自定义格式字符串,但这只能调用一次,并且在我的代码运行时已经被调用。
另一种方法是创建一个新的日志处理程序,它可以传递一个新的日志格式化程序,但这似乎不正确,因为我想使用该应用程序的现有日志处理程序发动机已安装。
将额外信息添加到 python App Engine 中的所有日志消息中,但使用现有日志格式和接收器的正确方法是什么?
I would like to log the module and classname by default in log messages from my request handlers.
The usual way to do this seems to be to set a custom format string by calling logging.basicConfig
, but this can only be called once and has already been called by the time my code runs.
Another method is to create a new log Handler
which can be passed a new log Formatter
, but this doesn't seem right as I want to use the existing log handler that App Engine has installed.
What is the right way to have extra information added to all log messages in python App Engine, but otherwise use the existing log format and sink?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过阅读
logging
模块的__init__.py
来完成此操作。我不知道这是否正确,但它似乎有效:I cooked this up by reading the
logging
module's__init__.py
. I don't know if this is proper, but it seems to work:我发现这适用于 Python 3.6,它将为所有后续日志记录调用设置日志记录级别/格式,即使之前的导入调用了
logging
。I found this to be working for Python 3.6, it will set the logging level / format for all subsequent logging calls, even if
logging
is called by previous imports.