Python logging - 显示触发自定义日志记录模块的脚本名称
Python logging - display the name of the script that triggers the custom logging module
我想获取触发自定义日志记录模块的脚本的名称。
名为 module_x
的日志记录模块的格式化程序保存在 logger.py
中,看起来像:
import logging.config
import sys
class ConsoleFormatter(logging.Formatter):
def __init__(self, datefmt, output_format):
super().__init__()
self.datefmt = datefmt
self.output_format = output_format
"""Logging Formatter to add colors and count warning / errors"""
INFO = '3[94m'
DEBUG = '3[37m'
WARNING = '3[33m'
FAIL = '3[91m'
ENDC = '3[0m'
BOLD = '3[1m'
formatting = output_format
self.FORMATS = {
logging.DEBUG: DEBUG + formatting + BOLD + ENDC,
logging.INFO: INFO + formatting + BOLD +ENDC,
logging.WARNING: WARNING + formatting + BOLD + ENDC,
logging.ERROR: FAIL + formatting + BOLD + ENDC,
logging.CRITICAL: FAIL + formatting + BOLD + ENDC
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt, datefmt=self.datefmt)
return formatter.format(record)
class Log(ConsoleFormatter):
def __init__(self, datefmt=None, format=None, handlers=None):
if datefmt == None:
datefmt = "%Y-%m-%d %H:%M:%S"
if format == None:
format = "%(asctime)s [%(levelname)s] [%(filename)s] %(message)s"
super().__init__(datefmt, format)
self.logger = logging.getLogger('test')
...
在 test.py
中,我使用以下方式触发模块:
logs = Log()
logs.output(msg="This is the first test.", level="INFO")
使用这段代码,我得到的显示是:
2022-01-11 16:15:28 [INFO] [logger.py] This is the first test.
我想获取触发 logger.py
的文件名,它是 test.py
,而不是 logger.py
。我怎样才能做到这一点?
您可以在调用 getLogger
时 name a logger,然后您可以将其用作记录器格式中的 %(name)s
。
因此在 test.py
中您可以使用当前文件名调用:
import os
logs = Log(name=os.path.basename(__file__))
logs.output(msg="This is the first test.", level="INFO")
并将 logger.py
中日志 class 的定义更改为如下内容:
class Log(ConsoleFormatter):
def __init__(self, name="test", datefmt=None, format=None, handlers=None): ##
if datefmt == None:
datefmt = "%Y-%m-%d %H:%M:%S"
if format == None:
format = "%(asctime)s [%(levelname)s] [%(name)s] %(message)s" ##
super().__init__(datefmt, format)
self.logger = logging.getLogger(name) ##
(已更改标有 ##
的行)
我想获取触发自定义日志记录模块的脚本的名称。
名为 module_x
的日志记录模块的格式化程序保存在 logger.py
中,看起来像:
import logging.config
import sys
class ConsoleFormatter(logging.Formatter):
def __init__(self, datefmt, output_format):
super().__init__()
self.datefmt = datefmt
self.output_format = output_format
"""Logging Formatter to add colors and count warning / errors"""
INFO = '3[94m'
DEBUG = '3[37m'
WARNING = '3[33m'
FAIL = '3[91m'
ENDC = '3[0m'
BOLD = '3[1m'
formatting = output_format
self.FORMATS = {
logging.DEBUG: DEBUG + formatting + BOLD + ENDC,
logging.INFO: INFO + formatting + BOLD +ENDC,
logging.WARNING: WARNING + formatting + BOLD + ENDC,
logging.ERROR: FAIL + formatting + BOLD + ENDC,
logging.CRITICAL: FAIL + formatting + BOLD + ENDC
}
def format(self, record):
log_fmt = self.FORMATS.get(record.levelno)
formatter = logging.Formatter(log_fmt, datefmt=self.datefmt)
return formatter.format(record)
class Log(ConsoleFormatter):
def __init__(self, datefmt=None, format=None, handlers=None):
if datefmt == None:
datefmt = "%Y-%m-%d %H:%M:%S"
if format == None:
format = "%(asctime)s [%(levelname)s] [%(filename)s] %(message)s"
super().__init__(datefmt, format)
self.logger = logging.getLogger('test')
...
在 test.py
中,我使用以下方式触发模块:
logs = Log()
logs.output(msg="This is the first test.", level="INFO")
使用这段代码,我得到的显示是:
2022-01-11 16:15:28 [INFO] [logger.py] This is the first test.
我想获取触发 logger.py
的文件名,它是 test.py
,而不是 logger.py
。我怎样才能做到这一点?
您可以在调用 getLogger
时 name a logger,然后您可以将其用作记录器格式中的 %(name)s
。
因此在 test.py
中您可以使用当前文件名调用:
import os
logs = Log(name=os.path.basename(__file__))
logs.output(msg="This is the first test.", level="INFO")
并将 logger.py
中日志 class 的定义更改为如下内容:
class Log(ConsoleFormatter):
def __init__(self, name="test", datefmt=None, format=None, handlers=None): ##
if datefmt == None:
datefmt = "%Y-%m-%d %H:%M:%S"
if format == None:
format = "%(asctime)s [%(levelname)s] [%(name)s] %(message)s" ##
super().__init__(datefmt, format)
self.logger = logging.getLogger(name) ##
(已更改标有 ##
的行)