如何将用于日志记录的 ContextFilter class 移动到单独的模块?

How to move ContextFilter class for logging to separate module?

我的 python 脚本中有 ContextFilter Class,我用它在日志格式化程序中有一个计数器变量。它按预期工作并在日志中打印增量变量的值。

但是当我尝试将此 Class 移动到我创建的自定义模块时,它总是打印变量的初始值而不是增量值。显然我错过了一些东西。

模块内容 log_format.py:

import logging
import datetime as dt

# As I need date in 2022-02-22 16:42:44.289193 format
class Formatter(logging.Formatter):
    converter=dt.datetime.fromtimestamp
    def formatTime(self, record, datefmt=None):
        ct = self.converter(record.created)
        if datefmt:
            s = ct.strftime(datefmt)
        else:
            t = ct.strftime("%Y-%m-%d %H:%M:%S")
            s = "%s,%03d" % (t, record.msecs)
        return s


#class ContextFilter(logging.Filter): # RUN 2
#    def __init__(self, i):           # RUN 2
#        self.i = i                   # RUN 2
#                                     # RUN 2
#    def filter(self, record):        # RUN 2
#        record.log_count = self.i    # RUN 2
#        return True                  # RUN 2

python 脚本的内容 log_format_test.py

import os
import sys
import logging

py_lib = os.environ.get('app_dir') + "/lib"
sys.path.append(py_lib)

import log_format as lf

class ContextFilter(logging.Filter): # RUN 1
    def filter(self, record):        # RUN 1
        record.log_count = i         # RUN 1
        return True                  # RUN 1


logger = logging.getLogger(__name__)
l_level = 'INFO' #'DEBUG' #'INFO'
l_level = eval("logging." + l_level.upper())
logger.setLevel(l_level)

int_var = os.getpid()
i = 0
str_var = os.path.basename(__file__)

handler = logging.StreamHandler()
handler.setLevel(l_level)
lf_format = f'ZZZZZZZZ|%(asctime)s|{int_var}|%(log_count)s|{str_var}|%(message)s'
formatter = lf.Formatter(fmt=lf_format, datefmt='%Y-%m-%d %H:%M:%S.%f') # 2022-02-22 16:42:44.289193

logger.addFilter(ContextFilter()) # RUN 1
#logger.addFilter(lf.ContextFilter(i)) # RUN 2

handler.setFormatter(formatter)
logger.addHandler(handler)

i += 1; logger.info("I am INFO 0")
i += 1; logger.info("I am INFO 1")
i += 1; logger.debug("I am DEBUG 0")

当我 运行 脚本 log_format_test.py 它确实按预期打印日志,每个日志条目的第 4 个字段递增 (运行 1):

ZZZZZZZZ|2022-02-24 11:41:19.578733|20527|1|log_format_test.py|I am INFO 0
ZZZZZZZZ|2022-02-24 11:41:19.578868|20527|2|log_format_test.py|I am INFO 1

但是当我尝试通过取消注释 # RUN 2 行并注释 # RUN 1 行将 class ContextFilterlog_format_test.py 移到 log_format.py 时,日志显示只有第 4 个字段的初始值是 0 (运行 2):

ZZZZZZZZ|2022-02-24 11:55:24.679430|29115|0|log_format_test.py|I am INFO 0
ZZZZZZZZ|2022-02-24 11:55:24.679552|29115|0|log_format_test.py|I am INFO 1

问题: 如何将 move class ContextFilterlog_format_test.py 移到 log_format.py(或它自己的新模块) ?我在这里错过了什么?

我想出了如何做到这一点。

模块内容 log_format.py

import logging
import datetime as dt


class Formatter(logging.Formatter):
    converter=dt.datetime.fromtimestamp
    def formatTime(self, record, datefmt=None):
        ct = self.converter(record.created)
        if datefmt:
            s = ct.strftime(datefmt)
        else:
            t = ct.strftime("%Y-%m-%d %H:%M:%S")
            s = "%s,%03d" % (t, record.msecs)
        return s


class ContextFilter(logging.Filter): # RUN 2
    def __init__(self, i):           # RUN 2
        self.i = i                   # RUN 2
                                     # RUN 2
    def filter(self, record):        # RUN 2
        self.i += 1                  # NEW LINE
        record.log_count = self.i    # RUN 2
        return True                  # RUN 2

python 脚本的内容 log_format_test.py

import os
import sys
import logging

py_lib = os.environ.get('app_dir') + "/lib"
sys.path.append(py_lib)

import log_format as lf


logger = logging.getLogger(__name__)
l_level = 'INFO' #'DEBUG' #'INFO'
l_level = eval("logging." + l_level.upper())
logger.setLevel(l_level)

int_var = os.getpid()
i = 0
str_var = os.path.basename(__file__)

handler = logging.StreamHandler()
handler.setLevel(l_level)
lf_format = f'ZZZZZZZZ|%(asctime)s|{int_var}|%(log_count)s|{str_var}|%(message)s'
formatter = lf.Formatter(fmt=lf_format, datefmt='%Y-%m-%d %H:%M:%S.%f') # 2022-02-22 16:42:44.289193

obj_filter = lf.ContextFilter(i)               # NEW LINE
logger.addFilter(lf.ContextFilter(obj_filter)) # CHANGED LINE

handler.setFormatter(formatter)
logger.addHandler(handler)

logger.info("I am INFO 1")
logger.info("I am INFO 2")
logger.debug("I am DEBUG 3")

这会按预期打印日志,我什至不必每次都手动递增 i。

ZZZZZZZZ|2022-02-24 11:41:19.578733|20527|1|log_format_test.py|I am INFO 1
ZZZZZZZZ|2022-02-24 11:41:19.578868|20527|2|log_format_test.py|I am INFO 2