Error model

import logging

log = logging.getLogger()
log.setLevel(logging.INFO)
log.info("INFO INFO INFO")
log.warning("It's a WARN")
Copy the code
D:/workspace/python-common/basic/logging_test.py = WARNCopy the code

No log level for output INFO. The default log level is WARN. If WARN is not displayed, the setting is not successful.

Set up a handler

import logging

if __name__ == '__main__':
    log = logging.getLogger()
    handler = logging.StreamHandler()
    log.addHandler(handler)
    log.setLevel(logging.INFO)
    log.info("This is the INFO - 1")
Copy the code

Now you can output INFO normally

Logging. GetLogger () and logging. GetLogger (name)

import logging

if __name__ == '__main__':
    log = logging.getLogger()
    handler = logging.StreamHandler()  StreamHandler is output to the console
    log.addHandler(handler)
    log.setLevel(logging.INFO)
    log.info("This is the INFO - 1")

    logger = logging.getLogger()
    logger.info("This is the INFO - 2")
Copy the code

Now the second logger is set to INFO.

You can see that a name of None returns a root object, which is why the second Logger was also modified.

Log Output Location

Common output to console and file

import logging

if __name__ == '__main__':
    log = logging.getLogger("console-logger")
    handler = logging.StreamHandler()
    log.addHandler(handler)
    log.setLevel(logging.INFO)
    log.info("This is the INFO - 1")

    log2 = logging.getLogger("file-logger")
    file_handler = logging.FileHandler("E:/test.log")
    log2.addHandler(file_handler)
    log2.setLevel(logging.WARNING)
    log2.info("This will not be recorded.")
    log2.warning("Warning warning ~~~~")
Copy the code

Set the format

By default, it simply prints message, which is better than using print() directly. So it also needs to be formatted:

To set up the formatting, use logging.handler

import logging


def print_name(logger, name) :
    logger.info("name={}".format(name))


if __name__ == '__main__':
    log = logging.getLogger("console-logger")
    handler = logging.StreamHandler()
    handler.setFormatter(logging.Formatter("%(asctime)s %(filename)s %(funcName)s[line:%(lineno)d]%(levelname)s - %(message)s"))
    log.addHandler(handler)
    log.setLevel(logging.INFO)
    log.info("This is the INFO - 1")
    print_name(log, "happyjava")
Copy the code

Output effect:

The formatter parameters:

The format string that may be used in the # format argument
# %(name) the name of the S Logger
# %(levelno)s Specifies the log level in digital format
# %(levelName)s Log level in text format
# %(pathName)s The full pathname of the module that called the logging function, possibly not
# %(filename)s specifies the filename of the module that calls the logging function
# %(module)s The name of the module that calls the logging function
# %(funcName)s The function name that calls the log output function
# %(lineno)d The line of code where the statement calling the logging function is located
# %(created)f Specifies the current time, which is represented by a UNIx-standard number of float points for time
# %(relativeCreated)d The number of milliseconds since Logger creation when the log message is output
# %(asctime)s Specifies the current time in the string format. The default format is 2003-07-08 16:49:45.896. The milliseconds follow the comma
# %(thread)d Specifies the ID of the thread. There may be no
# %(threadName)s specifies the threadName. There may be no
# %(process)d Indicates the ID of the process. There may be no
# %(message)s Indicates the message output by the user
Copy the code