python logging string formatting

前端 未结 3 2107
隐瞒了意图╮
隐瞒了意图╮ 2021-01-04 14:09

I am using python\'s log formatter to format log records and i have a fmt value of

fmt = \"[%(filename)s:%(lineno)s] %(message)s\"

What i w

相关标签:
3条回答
  • 2021-01-04 14:32

    Option 1

    Start here: http://docs.python.org/library/logging.html#formatter-objects

    You'll create your own customized subclass of Formatter that provides it's own unique format method.

    Then you must be sure to call setFormatter() in each of your Handlers so that they use your new formatter.

    Option 2

    Create your own subclass of LogRecord with the additional property.

    Subclass Logger and override makeRecord to create your new subclass of LogRecord.

    Provide a customized format that uses this new property value.

    0 讨论(0)
  • 2021-01-04 14:42

    Using @rob-cowie's answer as a basis, I've found the following useful:

    class MyFormatter(logging.Formatter):
        width = 24
        datefmt='%Y-%m-%d %H:%M:%S'
    
        def format(self, record):
            cpath = '%s:%s:%s' % (record.module, record.funcName, record.lineno)
            cpath = cpath[-self.width:].ljust(self.width)
            record.message = record.getMessage()
            s = "%-7s %s %s : %s" % (record.levelname, self.formatTime(record, self.datefmt), cpath, record.getMessage())
            if record.exc_info:
                # Cache the traceback text to avoid converting it multiple times
                # (it's constant anyway)
                if not record.exc_text:
                    record.exc_text = self.formatException(record.exc_info)
            if record.exc_text:
                if s[-1:] != "\n":
                    s = s + "\n"
                s = s + record.exc_text
            #if record.stack_info:
            #    if s[-1:] != "\n":
            #        s = s + "\n"
            #    s = s + self.formatStack(record.stack_info)
            return s
    
    logFormatter = MyFormatter()
    logger = logging.getLogger("example")
    logger.setFormatter(logFormatter)
    

    Which gives output like:

    WARNING 2014-03-28 16:05:09 module:function:31       : Message
    WARNING 2014-03-28 16:05:09 dule:longerfunctions:140 : Message
    
    0 讨论(0)
  • 2021-01-04 14:50

    As an example, this Formatter ensures a fixed width "[%(filename)s:%(lineno)s]" by either truncating the filename, or right-padding (after the line number) with spaces.

    class MyFormatter(logging.Formatter):
        width = 10
    
        def format(self, record):
            max_filename_width = self.width - 3 - len(str(record.lineno))
            filename = record.filename
            if len(record.filename) > max_filename_width:
                filename = record.filename[:max_filename_width]
            a = "%s:%s" % (filename, record.lineno)
            return "[%s] %s" % (a.ljust(self.width), record.msg)
    
    if __name__ == '__main__':
        logger = logging.getLogger('simple_example')
        logger.setLevel(logging.DEBUG)
        ch = logging.StreamHandler()
        ch.setLevel(logging.DEBUG)
        formatter = MyFormatter()
        ch.setFormatter(formatter)
        logger.addHandler(ch)
    
        logger.debug('No one expects the spammish repetition')
    

    EDIT:

    If you want to ensure a minimum width of 10 characters, ditch the filename stuff.

    def format(self, record):
        a = "%s:%s" % (record.filename, record.lineno)
        return "[%s] %s" % (a.ljust(self.width), record.msg)
    
    0 讨论(0)
提交回复
热议问题