根据Python3中的消息日志记录级别修改日志记录消息格式 [英] Modifying logging message format based on message logging level in Python3

查看:112
本文介绍了根据Python3中的消息日志记录级别修改日志记录消息格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对python 2提出了这个问题

I asked this question for python 2 here, but bumped into the issue again when the the answer no longer worked for Python 3.2.3.

以下是适用于Python 2.7.3的代码:

Here's code that works on Python 2.7.3:

import logging

# Attempt to set up a Python3 logger than will print custom messages
# based on each message's logging level.
# The technique recommended for Python2 does not appear to work for
# Python3

class CustomConsoleFormatter(logging.Formatter):
    """
    Modify the way DEBUG messages are displayed.

    """
    def __init__(self, fmt="%(levelno)d: %(msg)s"):
        logging.Formatter.__init__(self, fmt=fmt)

    def format(self, record):

        # Remember the original format
        format_orig = self._fmt

        if record.levelno == logging.DEBUG:
            self._fmt = "DEBUG: %(msg)s"

        # Call the original formatter to do the grunt work
        result = logging.Formatter.format(self, record)

        # Restore the original format
        self._fmt = format_orig

        return result


# Set up a logger
my_logger = logging.getLogger("my_custom_logger")
my_logger.setLevel(logging.DEBUG)

my_formatter = CustomConsoleFormatter()

console_handler = logging.StreamHandler()
console_handler.setFormatter(my_formatter)

my_logger.addHandler(console_handler)

my_logger.debug("This is a DEBUG-level message")
my_logger.info("This is an INFO-level message")

使用Python 2.7.3运行:

A run using Python 2.7.3:

tcsh-16: python demo_python_2.7.3.py 
DEBUG: This is a DEBUG-level message
20: This is an INFO-level message


据我所知,向Python3的转换只需要对CustomConsoleFormatter进行少量修改即可. init ():

As far as I can tell, conversion to Python3 requires only a slight mod to CustomConsoleFormatter.init():

def __init__(self):
    super().__init__(fmt="%(levelno)d: %(msg)s", datefmt=None, style='%')

在Python 3.2.3上:

On Python 3.2.3:

tcsh-26: python3 demo_python_3.2.3.py
10: This is a DEBUG-level message
20: This is an INFO-level message


如您所见,我想用"DEBUG"代替"10"的愿望被挫败了.


As you can see, my desire to replace '10' with 'DEBUG' is being thwarted.

我尝试在Python3源代码中进行挖掘,看来PercentStyle实例正在破坏self._fmt,直到我自己破坏了.

I've tried digging around in Python3 source and it looks like the PercentStyle instantiation is clobbering self._fmt after I, well, clobber it myself.

我的伐木砍伐因无法解决这种皱纹而停止.

My logging chops stop just short of being able to work around this wrinkle.

任何人都可以推荐另一种方式,或者指出我忽略的内容吗?

Can anyone recommend another way or perhaps point out what I'm overlooking?

推荐答案

稍加挖掘,我便能够修改Python 2解决方案以与Python 3配合使用.在Python2中,有必要暂时覆盖Formatter._fmt .在Python3中,对多种格式字符串类型的支持要求我们暂时覆盖Formatter._style._fmt.

With a bit of digging, I was able to modify the Python 2 solution to work with Python 3. In Python2, it was necessary to temporarily overwrite Formatter._fmt. In Python3, support for multiple format string types requires us to temporarily overwrite Formatter._style._fmt instead.

# Custom formatter
class MyFormatter(logging.Formatter):

    err_fmt  = "ERROR: %(msg)s"
    dbg_fmt  = "DBG: %(module)s: %(lineno)d: %(msg)s"
    info_fmt = "%(msg)s"

    def __init__(self):
        super().__init__(fmt="%(levelno)d: %(msg)s", datefmt=None, style='%')  

    def format(self, record):

        # Save the original format configured by the user
        # when the logger formatter was instantiated
        format_orig = self._style._fmt

        # Replace the original format with one customized by logging level
        if record.levelno == logging.DEBUG:
            self._style._fmt = MyFormatter.dbg_fmt

        elif record.levelno == logging.INFO:
            self._style._fmt = MyFormatter.info_fmt

        elif record.levelno == logging.ERROR:
            self._style._fmt = MyFormatter.err_fmt

        # Call the original formatter class to do the grunt work
        result = logging.Formatter.format(self, record)

        # Restore the original format configured by the user
        self._style._fmt = format_orig

        return result

这是Halloleo在脚本中如何使用以上代码的示例(来自

And here is Halloleo's example of how to use the above in your script (from the Python2 version of this question):

fmt = MyFormatter()
hdlr = logging.StreamHandler(sys.stdout)

hdlr.setFormatter(fmt)
logging.root.addHandler(hdlr)
logging.root.setLevel(DEBUG)

这篇关于根据Python3中的消息日志记录级别修改日志记录消息格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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