Simple use Python's logging module
If you deploy servers, often running in the background. When the program specific error occurred, I want to query the log. So here are familiar with the use of the logging module.
logging module defines a standard API report errors and status information.
logging components
Logging system has four components interact with each other. We need to use the Logger instance to add information to the log. It creates a log trigger the LogRecord , memory for storing information. There may be many Logger Handler object to receive and process logging. Handler using Formatter output logging.
Enter the log to a file
Most applications are entered into the log file. Use basicConfig () function can set the default handler, so that the input to the log file.
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import logging 4 5 LOG_FILENAME = 'log.txt' 6 logging.basicConfig( 7 filename=LOG_FILENAME, 8 level=logging.DEBUG, 9 ) 10 11 logging.debug('hello logging!') 12 13 with open(LOG_FILENAME, 'rt') as f: 14 body = f.read() 15 16 print('FILE: ') 17 print(body)
After running the script output is as follows:
FILE:
DEBUG:root:hello logging!
Recycle log files
Each time the program runs to make, create a new file, it is necessary to basicConfig () filemode pass a parameter value of w. There is also a more convenient way is to use RotatingFileHandler , you can simultaneously create a file and saves the old file.
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import glob 4 import logging.handlers 5 6 LOG_FILENAME = 'log.txt' 7 8 my_logger = logging.getLogger('SpecificLogger') 9 my_logger.setLevel(logging.DEBUG) 10 11 # Add the log message handler to the logger 12 handler = logging.handlers.RotatingFileHandler( 13 LOG_FILENAME, 14 maxBytes=20, 15 backupCount=5, 16 ) 17 my_logger.addHandler(handler) 18 19 # Log some messages 20 for i in range(20): 21 my_logger.debug(f'i = {i}') 22 23 # See what files are created 24 log_files = glob.glob(f'{LOG_FILENAME}*') 25 for filename in sorted(log_files): 26 print(filename)
After running the script output is as follows:
log.txt log.txt.1 log.txt.2 log.txt.3 log.txt.4 log.txt.5
Can back now, log.txt stored contents are up to date, logging will automatically rename these files.
Level information displayed
logging have different log levels.
Level (level) | Value (value) |
CRITICAL | 50 |
ERROR | 40 |
WARNING | 30 |
INFO | 20 |
DEBUG | 10 |
UNSET | 0 |
Logs can only cases above a certain level will be triggered.
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import logging 4 import sys 5 6 level = int(sys.argv[1]) 7 logging.basicConfig( 8 level=level 9 ) 10 11 logging.debug('debug message') 12 logging.info('info message') 13 logging.warning('warning message') 14 logging.error('error message') 15 logging.critical('critical message')
$ python logging_level.py 10 DEBUG:root:debug message INFO:root:info message WARNING:root:warning message ERROR:root:error message CRITICAL:root:critical message
$ python logging_level 40 ERROR:root:error message CRITICAL:root:critical message
Named logging example
1 #!/usr/bin/env python 2 # -*- coding: utf-8 -*- 3 import logging 4 5 logging.basicConfig( 6 level=logging.WARNING 7 ) 8 9 logger1 = logging.getLogger('package1.module1') 10 logger2 = logging.getLogger('package2.module2') 11 12 logger1.warning('hello 1') 13 logger2.warning('hello 2')
Run script output:
WARNING:package1.module1:hello 1 WARNING:package2.module2:hello 2