我想向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?