How to add a custom loglevel to Python's logging facility

前端 未结 16 2316
旧时难觅i
旧时难觅i 2020-11-27 09:54

I\'d like to have loglevel TRACE (5) for my application, as I don\'t think that debug() is sufficient. Additionally log(5, msg) isn\'t what I want.

16条回答
  •  一整个雨季
    2020-11-27 10:21

    In my experience, this is the full solution the the op's problem... to avoid seeing "lambda" as the function in which the message is emitted, go deeper:

    MY_LEVEL_NUM = 25
    logging.addLevelName(MY_LEVEL_NUM, "MY_LEVEL_NAME")
    def log_at_my_log_level(self, message, *args, **kws):
        # Yes, logger takes its '*args' as 'args'.
        self._log(MY_LEVEL_NUM, message, args, **kws)
    logger.log_at_my_log_level = log_at_my_log_level
    

    I've never tried working with a standalone logger class, but I think the basic idea is the same (use _log).

提交回复
热议问题