Python Learning: Common Modules (3)

Keywords: Python Unix

Common Modules (3)

logging module

The information contained in the log should be accessed by a normal program, and may be output with information such as errors, warnings, etc.

python's logging module provides a standard log interface through which you can store logs in various formats. Logging logs can be divided into five levels: debug (), info (), warning (), error (), critical ().

 

Eg. Simple Logging

import logging
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')
>>> WARNING:root:warning message
    ERROR:root:error message
    CRITICAL:root:critical message
# By default, the logging module prints the logs to standard output (i.e. on the screen) and displays only logs greater than or equal to the WARNING level.
# The default log level is set to WARNING (log level level CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET)
# The default log format is log level: Logger name: user output message.

 

Eg. Configuration log level, log format, output location

import logging
logging.basicConfig(level=logging.DEBUG,  # Adjust the level at which the log starts to display
                    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
                    datefmt='%a, %d %b %Y %H:%M:%S',
                    filename='test.log',  # Create log files in the current directory
                    filemode='w')  # The way to open the file is w
logging.debug('debug message')
logging.info('info message')
logging.warning('warning message')
logging.error('error message')
logging.critical('critical message')

 

Formatting strings that can be used in format parameters:

%(name)s       # Name of Logger
%(levelno)s    # Log level in digital form
%(levelname)s  # Log Level in Text Form
%(pathname)s   # The full path name of the module that calls the log output function may not be
%(filename)s   # File name of module calling log output function
%(module)s     # Module name for calling log output function
%(funcName)s   # Function name calling log output function
%(lineno)d     # The line of code where the statement calling the log output function is located
%(created)f    # Current time, expressed in UNIX standard floating point numbers for time
%(relativeCreated)d # The number of milliseconds since Logger was created when log information was output
%(asctime)s    # The current time in string form. The default format is "2003-07-08 16:49:45,896". Behind the comma is milliseconds.
%(thread)d     # Thread ID. Probably not.
%(threadName)s # Thread name. Probably not.
%(process)d    # Process ID. Probably not.
%(message)s    # User Output Message

 

Logger object: Use logger object to let files and screens output logs at the same time

import logging
logger = logging.getLogger()

# Create a handler to write to the log file
fh = logging.FileHandler('test.log')  # File Output Stream Object
# Create a handler for output to the console
ch = logging.StreamHandler()  # Standard Output Stream Object (Screen Output)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

fh.setFormatter(formatter)  # Get the standard format for output
ch.setFormatter(formatter)  # Get the standard format for output

logger.addHandler(fh)  # logger objects can add multiple fh and ch objects
logger.addHandler(ch)

logger.setLevel(logging.DEBUG) # Setting the level of the log

# Finally, set up the different information that the log wants to record
logger.debug('logger debug message')
logger.info('logger info message')
logger.warning('logger warning message')
logger.error('logger error message')
logger.critical('logger critical message')

Posted by pbase on Wed, 23 Jan 2019 14:42:13 -0800