如何更改 python 应用程序引擎中日志消息的默认格式?

发布于 2024-08-27 03:44:41 字数 259 浏览 8 评论 0原文

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

通常的方法似乎是通过调用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三五鸿雁 2024-09-03 03:44:41

我通过阅读 logging 模块的 __init__.py 来完成此操作。我不知道这是否正确,但它似乎有效:

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!

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!
烟雨扶苏 2024-09-03 03:44:41

我发现这适用于 Python 3.6,它将为所有后续日志记录调用设置日志记录级别/格式,即使之前的导入调用了 logging

logging_level = logging.INFO
logging_fmt = "%(levelname)s:%(name)s:%(message)s"   # the default
try:
    root_logger = logging.getLogger()
    root_logger.setLevel(logging_level)
    root_handler = root_logger.handlers[0]
    root_handler.setFormatter(logging.Formatter(logging_fmt))
except IndexError:
    logging.basicConfig(level=logging_level, format=logging_fmt)

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.

logging_level = logging.INFO
logging_fmt = "%(levelname)s:%(name)s:%(message)s"   # the default
try:
    root_logger = logging.getLogger()
    root_logger.setLevel(logging_level)
    root_handler = root_logger.handlers[0]
    root_handler.setFormatter(logging.Formatter(logging_fmt))
except IndexError:
    logging.basicConfig(level=logging_level, format=logging_fmt)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文