What is Python's default logging formatter?

后端 未结 5 1611
有刺的猬
有刺的猬 2021-02-05 01:02

I\'m trying to decipher the information contained in my logs (the logging setup is using the default formatter). The documentation states:

Do formatting f

相关标签:
5条回答
  • 2021-02-05 01:42

    The default seems to be %(levelname)s:%(name)s:%(message)s

    import logging
    logging.error("Some error")
    print "fmt: " , logging.root.handlers[0].formatter._fmt
    # ERROR:root:Some error
    # fmt:  %(levelname)s:%(name)s:%(message)s
    
    0 讨论(0)
  • 2021-02-05 01:44

    Here is the example of an advanced way of logging:-

    import logging
    class logger_con():
       def log_test(self):
          """
            :create logger
            :set logger level
            :create console handler
            :add formatter to console handler
            :add console handler to logger
            :add logging massage
            :return:
          """
        #create logger and set level
        logger=logging.getLogger(logger_con.__name__)
    
        logger.setLevel(logging.INFO)
    
        #create console handler(we are using steamHandler which is only used to display log in console)
    
        con_handler=logging.StreamHandler()
        con_handler.setLevel(logging.INFO)
    
        #create formatter and add formatter to con_handler
        formatter=logging.Formatter('%(asctime)s : %(message)s : %(levelname)s -%(name)s',datefmt='%d%m%Y %I:%M:%S %p')
        con_handler.setFormatter(formatter)
        #add console handler to logger
        logger.addHandler(con_handler)
    
        logger.debug('Program debugging')
        logger.info('massage conatain information')
        logger.warning('warning message')
        logger.error('massage contains error')
        logger.critical('critical massages')
    
    0 讨论(0)
  • 2021-02-05 01:46

    The default format is located here which is:

    BASIC_FORMAT = "%(levelname)s:%(name)s:%(message)s"  
    

    The Format code will tell you how you can customize it. Here is one example on how you can customize it.

    import sys
    import logging
    
    logging.basicConfig(
        level=logging.DEBUG,
        format="[%(asctime)s] %(levelname)s [%(name)s.%(funcName)s:%(lineno)d] %(message)s",
        datefmt="%d/%b/%Y %H:%M:%S",
        stream=sys.stdout)
    
    logging.info("HEY")
    

    Which results in:

    [26/May/2013 06:41:40] INFO [root.<module>:1] HEY
    
    0 讨论(0)
  • 2021-02-05 01:47

    It's in the source of logging/__init__.py:

    _defaultFormatter = Formatter()
    

    The default formatting string is %(message)s, which is in the source as well:

    if fmt:
        self._fmt = fmt
    else:
        self._fmt = "%(message)s"
    
    0 讨论(0)
  • 2021-02-05 01:56
    import logging
    print(logging.BASIC_FORMAT)
    

    Old thread but this comes up first in my google search results for the query "python logging default format", so I thought I should add my answer.

    Also some comments asked about how one could have come to discover this on their own. Here is a natural thing to do:

    import logging
    print(dir(logging))
    

    BASIC_FORMAT is in there, in fact it is the first entry in the result in my case.

    0 讨论(0)
提交回复
热议问题