0

I created a custom log level as below:

class MyLoggerAdapter(logging.LoggerAdapter):
    def __init__(self, logger, extra):
        logging.LoggerAdapter.__init__(self, logger, extra)
        self.logger = logger
        logging.addLevelName(25, "NOTICE")

    def notice(self, msg, *args, **kwargs):
        self.logger.log(25, msg, *args, **kwargs)

The issue I have is that my logger format uses %(pathname)s and %(lineno)d, and whenever I call logger.notice() those values refer to the line within the definition of the notice method, rather than the location that the method was called from. How do I add the notice log level but print the correct location of the message to the logs?

ewok
  • 20,148
  • 51
  • 149
  • 254
  • http://stackoverflow.com/questions/12980512/custom-logger-class-and-correct-line-number-function-name-in-log asks about something similar but for writing a custom handler - are any of the suggestions there useful? Alternatively, http://www.gossamer-threads.com/lists/python/python/1190721 suggests ways of adding new log levels without creating a new LoggerAdapter class, and http://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility has answers going into more detail about adding custom loglevels. – bouteillebleu Oct 15 '15 at 20:01
  • @bouteillebleu is it possible to do what I want in a subclass? – ewok Oct 19 '15 at 14:19

0 Answers0