pythonloggingpython-logging

What is Python's default logging formatter?


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

Do formatting for a record - if a formatter is set, use it. Otherwise, use the default formatter for the module.

However, I can't find any reference actually stating what this default format is.


Solution

  • The default format is located here which is:

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

    The section on formatting in the python docs 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