This Gist summarizes some information about using the logging
module in Python 2.7. The examples here are
taken from the references.
The logging
module makes it easy to track what's going on in your code in a more graceful way than just putting
print statements during debugging. What we can do with the logging
module is create handlers which will
be responsible for sending log messages to a particular destination (a file, the terminal, etc).
logging
can report information at multiple levels:
DEBUG
INFO
WARNING
ERROR
CRITICAL
Different handlers can be configured to report information at above a certain level. For example, we may want
to log DEBUG
messages to the terminal, but only care about saving messages at least as important as WARNING
to a logfile.
To log messages at different levels, just use the logging
function for the desired level:
logging.debug('A message used for debugging')
logging.critical('An important message')
At its most basic level, we can configure a logging
to work in a single file like so:
import logging
logging.basicConfig(filename='example.log',level=logging.DEBUG)
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
Now if we look at the file example.log
, we will see:
DEBUG:root:This message should go to the log file
INFO:root:So should this
WARNING:root:And this, too
For more info, see Basic Logging Tutorial
With a little bit of extra work we can configure the logger in our main script and have code in multiple different modules all log to the same destinations with their module name, the time, etc.
Essentially, we use Logging handlers to add a location where logs should be written. Each handler has a Formatter to control how the log message is printed out.
Thus, in our main script, we can configure logging
by adding handlers:
logger = logging.getLogger('module_name')
logger.setLevel(logging.DEBUG)
formatter = logging.Formatter(fmt='%(asctime)s - %(name)s - %(levelname)s: %(message)s',
datefmt='%m/%d/%Y %H:%M:%S')
# Log to output file 'experiment.log'
filehandler = logging.FileHandler(output_dir+'experiment.log')
filehandler.setLevel(logging.DEBUG)
filehandler.setFormatter(formatter)
# Log to console
streamhandler = logging.StreamHandler()
streamhandler.setLevel(logging.DEBUG)
streamhandler.setFormatter(formatter)
# Add handlers to the logger
logger.addHandler(filehandler)
logger.addHandler(streamhandler)
In order to get code in a different module to access the same logger with the same handlers.
we need to call logging.getLogger('first_module_name.other_module_name
).
For example,
other_module_logger = logging.getLogger('module_name.other')
For more info, see Logging Cookbook