Scenario
Logging is a common functionality used to record program’s execution. It provides important information for operation, health check, defect analysis, auditing and so forth. As a standard library in Python, logging module provides a full range of features from configuration through writing logs.
We’ll work on an example in this blog. Before start, let’s walk through basic knowledge of it. First, logging classifies logs into DEBUG, INFO, WARNING, ERROR, and CRITICAL based on their severity. Logging sets WARNING as the default level. As a result, debugging and informational logs are suppressed, on the contrary, warning and more severe logs such as erroneous and critical ones are allowed to be output. Logs can be written to a file, sent to a console or sockets or datagrams etc.
Example
In this example, logging.basicConfig is first called to perform basic configuration for the logging system.
Subsequently, a file logger and a stream logger are declared. Each of them is assigned a name when logging.getLogger is called. So we can find them with the proper names when we need to use them later. As for the stream logger, the messages will be directed to sys.stderr.
Below is the source code.
import logging
if __name__ == '__main__':
format = '%(asctime)s %(levelname)s [%(threadName)s] [%(module)s] [%(funcName)s] %(message)s'
#
# logging basic configuration
logging.basicConfig(filename='basic.log', filemode='a', \
format=format, \
level=logging.INFO)
logging.info('basiclogging: starts')
logging.debug('basiclogging: debug level log')
logging.info('basiclogging: finishes')
#
# configure a file logger
filelogger = logging.getLogger("mylogger")
handler = logging.FileHandler(filename='my.log', mode='w')
fmt = logging.Formatter(format)
handler.setFormatter(fmt=fmt)
filelogger.addHandler(handler)
filelogger.setLevel(logging.DEBUG)
filelogger.info('filelogger : starts')
filelogger.debug('filelogger : debug level log')
filelogger.info('filelogger : finishes')
#
# configure a stream logger
streamlogger = logging.getLogger('streamlogger')
streamhandler = logging.StreamHandler()
streamhandler.setFormatter(fmt=fmt)
streamhandler.setLevel(level=logging.WARNING)
streamlogger.addHandler(streamhandler)
streamlogger.warning('streamlogger: starts')
streamlogger.debug('streamlogger: debug level log')
streamlogger.info('streamlogger: information level log')
streamlogger.warning('streamlogger: finishes')
Outputs are pasted below. As you can see, logs output to the file and the console are also written to basic.log file.
basic.log
2023-08-03 16:43:21,417 INFO [MainThread] [main] [uselogging] basiclogging: finishes
2023-08-03 16:43:21,417 INFO [MainThread] [main] [uselogging] filelogger : starts
2023-08-03 16:43:21,417 DEBUG [MainThread] [main] [uselogging] filelogger : debug level log
2023-08-03 16:43:21,417 INFO [MainThread] [main] [uselogging] filelogger : finishes
2023-08-03 16:43:21,418 WARNING [MainThread] [main] [uselogging] streamlogger: starts
2023-08-03 16:43:21,418 INFO [MainThread] [main] [uselogging] streamlogger: information level log
2023-08-03 16:43:21,418 WARNING [MainThread] [main] [uselogging] streamlogger: finishes
file.log
2023-08-03 16:43:21,417 DEBUG [MainThread] [main] [uselogging] filelogger : debug level log
2023-08-03 16:43:21,417 INFO [MainThread] [main] [uselogging] filelogger : finishes
Console:
No comments:
Post a Comment