如何更改Python应用程序引擎中日志消息的默认格式? [英] How do I change the default format of log messages in python app engine?

查看:80
本文介绍了如何更改Python应用程序引擎中日志消息的默认格式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的请求处理程序的日志消息中默认记录模块和类名。

I would like to log the module and classname by default in log messages from my request handlers.

通常的做法似乎是设置一个自定义格式字符串通过调用 logging.basicConfig ,但这只能被调用一次,并且已经被我的代码运行时调用。

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.

另一种方法是创建一个新日志 Handler ,它可以传递一个新日志 Formatter ,但是这看起来不正确,因为我想使用App Engine已安装的现有日志处理程序。

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.

有什么方法可以将额外信息添加到所有日志消息中在python App Engine中,但是否则使用现有的日志格式和接收器?

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?

推荐答案

$ c> 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:

import logging

logging.basicConfig(level=logging.DEBUG,
                    format='%(asctime)s %(name)-12s %(levelname)-8s %(message)s',
                    datefmt='%m-%d %H:%M',
                    )

logging.info('Danger Will Robinson!')
# 03-31 20:00 root         INFO     Danger Will Robinson!
root = logging.getLogger()
hdlr = root.handlers[0]
fmt = logging.Formatter('%(name)-12s: %(levelname)-8s %(message)s')
hdlr.setFormatter(fmt)
logging.info('Danger Will Robinson!')
# root        : INFO     Danger Will Robinson!

这篇关于如何更改Python应用程序引擎中日志消息的默认格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆