将自定义级别记录到不同的fi

2024-10-03 19:30:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我想为python日志记录添加一个自定义级别,并且只将这个级别和这个级别记录到一个特定的文件中。我还想把这个记录器添加到根记录器,这样我就不必每次都获取记录器了。你知道吗

我做到了以下几点:

DEBUG_LEVELV_NUM = 60
logging.addLevelName(DEBUG_LEVELV_NUM, "DEBUGV")

actionLogger = logging.FileHandler(filename='ImportantActions.log')
actionLogger.setLevel(DEBUG_LEVELV_NUM)
# set a format which is simpler for console use
formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
# tell the handler to use this format
actionLogger.setFormatter(formatter)
# add the handler to the root logger
logging.getLogger('').addHandler(actionLogger)

logging.log(60, "Testing")

这确实有效,但是我想避免使用logging.info(60,。你知道吗

是否可以向日志模块添加一个API,例如logging.important_message(),它会自动将这个级别记录到日志中ImportantActions.log?你知道吗


Tags: thedebuglogformatmessageuseformatterlogging