Logging HOWTO¶
- Author:
Vinay Sajip <vinay_sajip at red-dove dot com>
This page contains tutorial information. For links to reference information and a logging cookbook, please see Other resources.
Basic Logging Tutorial¶
Logging is a means of tracking events that happen when some software runs. The software’s developer adds logging calls to their code to indicate that certain events have occurred. An event is described by a descriptive message which can optionally contain variable data (i.e. data that is potentially different for each occurrence of the event). Events also have an importance which the developer ascribes to the event; the importance can also be called the level or severity.
When to use logging¶
You can access logging functionality by creating a logger via logger =
logging.getLogger(__name__), and then calling the logger’s debug(),
info(), warning(), error() and
critical() methods. To determine when to use logging, and to see
which logger methods to use when, see the table below. It states, for each of a
set of common tasks, the best tool to use for that task.
Task you want to perform |
The best tool for the task |
|---|---|
Display console output for ordinary usage of a command line script or program |
|
Report events that occur during normal operation of a program (e.g. for status monitoring or fault investigation) |
A logger’s |
Issue a warning regarding a particular runtime event |
A logger’s |
Report an error regarding a particular runtime event |
Raise an exception |
Report suppression of an error without raising an exception (e.g. error handler in a long-running server process) |
A logger’s |
The logger methods are named after the level or severity of the events they are used to track. The standard levels and their applicability are described below (in increasing order of severity):
Level |
When it’s used |
|---|---|
|
Detailed information, typically of interest only when diagnosing problems. |
|
Confirmation that things are working as expected. |
|
An indication that something unexpected happened, or indicative of some problem in the near future (e.g. ‘disk space low’). The software is still working as expected. |
|
Due to a more serious problem, the software has not been able to perform some function. |
|
A serious error, indicating that the program itself may be unable to continue running. |
The default level is WARNING, which means that only events of this severity and higher
will be tracked, unless the logging package is configured to do otherwise.
Events that are tracked can be handled in different ways. The simplest way of handling tracked events is to print them to the console. Another common way is to write them to a disk file.
A simple example¶
A very simple example is:
import logging
logging.warning('Watch out!') # will print a message to the console
logging.info('I told you so') # will not print anything
If you type these lines into a script and run it, you’ll see:
WARNING:root:Watch out!
printed out on the console. The INFO message doesn’t appear because the
default level is WARNING. The printed message includes the indication of the
level and the description of the event provided in the logging call, i.e.
‘Watch out!’. The actual output can be formatted quite flexibly if you need
that; formatting options will also be explained later.
Notice that in this example, we use functions directly on the logging
module, like logging.debug, rather than creating a logger and calling
functions on it. These functions operate on the root logger, but can be useful
as they will call basicConfig() for you if it has not been called yet, like in
this example. In larger programs you’ll usually want to control the logging
configuration explicitly however - so for that reason as well as others, it’s
better to create loggers and call their methods.
Logging to a file¶
A very common situation is that of recording logging events in a file, so let’s look at that next. Be sure to try the following in a newly started Python interpreter, and don’t just continue from the session described above:
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(filename='example.log', encoding='utf-8', level=logging.DEBUG)
logger.debug('This message should go to the log file')
logger.info('So should this')
logger.warning('And this, too')
logger.error('And non-ASCII stuff, too, like Øresund and Malmö')
Changed in version 3.9: The encoding argument was added. In earlier Python versions, or if not
specified, the encoding used is the default value used by open(). While
not shown in the above example, an errors argument can also now be passed,
which determines how encoding errors are handled. For available values and
the default, see the documentation for open().
And now if we open the file and look at what we have, we should find the log messages:
DEBUG:__main__:This message should go to the log file
INFO:__main__:So should this
WARNING:__main__:And this, too
ERROR:__main__:And non-ASCII stuff, too, like Øresund and Malmö
This example also shows how you can set the logging level which acts as the
threshold for tracking. In this case, because we set the threshold to
DEBUG, all of the messages were printed.
If you want to set the logging level from a command-line option such as:
--log=INFO
and you have the value of the parameter passed for --log in some variable
loglevel, you can use:
getattr(logging, loglevel.upper())
to get the value which you’ll pass to basicConfig() via the level
argument. You may want to error check any user input value, perhaps as in the
following example:
# assuming loglevel is bound to the string value obtained from the
# command line argument. Convert to upper case to allow the user to
# specify --log=DEBUG or --log=debug
numeric_level = getattr(logging, loglevel.upper(), None)
if not isinstance(numeric_level, int):
raise ValueError('Invalid log level: %s' % loglevel)
logging.basicConfig(level=numeric_level, ...)
The call to basicConfig() should come before any calls to a logger’s
methods such as debug(), info(), etc. Otherwise,
that logging event may not be handled in the desired manner.
If you run the above script several times, the messages from successive runs are appended to the file example.log. If you want each run to start afresh, not remembering the messages from earlier runs, you can specify the filemode argument, by changing the call in the above example to:
logging.basicConfig(filename='example.log', filemode='w', level=logging.DEBUG)
The output will be the same as before, but the log file is no longer appended to, so the messages from earlier runs are lost.
Logging variable data¶
To log variable data, use a format string for the event description message and append the variable data as arguments. For example:
import logging
logging.warning('%s before you %s', 'Look', 'leap!')
will display:
WARNING:root:Look before you leap!
As you can see, merging of variable data into the event description message
uses the old, %-style of string formatting. This is for backwards
compatibility: the logging package pre-dates newer formatting options such as
str.format() and string.Template. These newer formatting
options are supported, but exploring them is outside the scope of this
tutorial: see Using particular formatting styles throughout your application for more information.
Changing the format of displayed messages¶
To change the format which is used to display messages, you need to specify the format you want to use:
import logging
logging.basicConfig(format='%(levelname)s:%(message)s', level=logging.DEBUG)
logging.debug('This message should appear on the console')
logging.info('So should this')
logging.warning('And this, too')
which would print:
DEBUG:This message should appear on the console
INFO:So should this
WARNING:And this, too
Notice that the ‘root’ which appeared in earlier examples has disappeared. For a full set of things that can appear in format strings, you can refer to the documentation for LogRecord attributes, but for simple usage, you just need the levelname (severity), message (event description, including variable data) and perhaps to display when the event occurred. This is described in the next section.
Displaying the date/time in messages¶
To display the date and time of an event, you would place ‘%(asctime)s’ in your format string:
import logging
logging.basicConfig(format='%(asctime)s %(message)s')
logging.warning('is when this event was logged.')
which should print something like this:
2010-12-12 11:41:42,612 is when this event was logged.
The default format for date/time display (shown above) is like ISO8601 or
RFC 3339. If you need more control over the formatting of the date/time, provide
a datefmt argument to basicConfig, as in this example:
import logging
logging.basicConfig(format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')
logging.warning('is when this event was logged.')
which would display something like this:
12/12/2010 11:46:36 AM is when this event was logged.
The format of the datefmt argument is the same as supported by
time.strftime().
Next Steps¶
That concludes the basic tutorial. It should be enough to get you up and running with logging. There’s a lot more that the logging package offers, but to get the best out of it, you’ll need to invest a little more of your time in reading the following sections. If you’re ready for that, grab some of your favourite beverage and carry on.
If your logging needs are simple, then use the above examples to incorporate logging into your own scripts, and if you run into problems or don’t understand something, please post a question in the Help category of the Python discussion forum and you should receive help before too long.
Still here? You can carry on reading the next few sections, which provide a slightly more advanced/in-depth tutorial than the basic one above. After that, you can take a look at the Logging Cookbook.
Advanced Logging Tutorial¶
The logging library takes a modular approach and offers several categories of components: loggers, handlers, filters, and formatters.
Loggers expose the interface that application code directly uses.
Handlers send the log records (created by loggers) to the appropriate destination.
Filters provide a finer grained facility for determining which log records to output.
Formatters specify the layout of log records in the final output.
Log event information is passed between loggers, handlers, filters and
formatters in a LogRecord instance.
Logging is performed by calling methods on instances of the Logger
class (hereafter called loggers). Each instance has a name, and they are
conceptually arranged in a namespace hierarchy using dots (periods) as
separators. For example, a logger named ‘scan’ is the parent of loggers
‘scan.text’, ‘scan.html’ and ‘scan.pdf’. Logger names can be anything you want,
and indicate the area of an application in which a logged message originates.
A good convention to use when naming loggers is to use a module-level logger, in each module which uses logging, named as follows:
logger = logging.getLogger(__name__)
This means that logger names track the package/module hierarchy, and it’s intuitively obvious where events are logged just from the logger name.
The root of the hierarchy of loggers is called the root logger. That’s the
logger used by the functions debug(), info(), warning(),
error() and critical(), which just call the same-named method of
the root logger. The functions and the methods have the same signatures. The
root logger’s name is printed as ‘root’ in the logged output.
It is, of course, possible to log messages to different destinations. Support is included in the package for writing log messages to files, HTTP GET/POST locations, email via SMTP, generic sockets, queues, or OS-specific logging mechanisms such as syslog or the Windows NT event log. Destinations are served by handler classes. You can create your own log destination class if you have special requirements not met by any of the built-in handler classes.
By default, no destination is set for any logging messages. You can specify
a destination (such as console or file) by using basicConfig() as in the
tutorial examples. If you call the functions debug(), info(),
warning(), error() and critical(), they will check to see
if no destination is set; and if one is not set, they will set a destination
of the console (sys.stderr) and a default format for the displayed
message before delegating to the root logger to do the actual message output.
The default format set by basicConfig() for messages is:
severity:logger name:message
You can change this by passing a format string to basicConfig() with the
format keyword argument. For all options regarding how a format string is
constructed, see Formatter Objects.
Logging Flow¶
The flow of log event information in loggers and handlers is illustrated in the following diagram.
Loggers¶
Logger objects have a threefold job. First, they expose several
methods to application code so that applications can log messages at runtime.
Second, logger objects determine which log messages to act upon based upon
severity (the default filtering facility) or filter objects. Third, logger
objects pass along relevant log messages to all interested log handlers.
The most widely used methods on logger objects fall into two categories: configuration and message sending.
These are the most common configuration methods:
Logger.setLevel()specifies the lowest-severity log message a logger will handle, where debug is the lowest built-in severity level and critical is the highest built-in severity. For example, if the severity level is INFO, the logger will handle only INFO, WARNING, ERROR, and CRITICAL messages and will ignore DEBUG messages.Logger.addHandler()andLogger.removeHandler()add and remove handler objects from the logger object. Handlers are covered in more detail in Handlers.Logger.addFilter()andLogger.removeFilter()add and remove filter objects from the logger object. Filters are covered in more detail in Filter Objects.
You don’t need to always call these methods on every logger you create. See the last two paragraphs in this section.
With the logger object configured, the following methods create log messages:
Logger.debug(),Logger.info(),Logger.warning(),Logger.error(), andLogger.critical()all create log records with a message and a level that corresponds to their respective method names. The message is actually a format string, which may contain the standard string substitution syntax of%s,%d,%f, and so on. The rest of their arguments is a list of objects that correspond with the substitution fields in the message. With regard to**kwargs, the logging methods care only about a keyword ofexc_infoand use it to determine whether to log exception information.Logger.exception()creates a log message similar toLogger.error(). The difference is thatLogger.exception()dumps a stack trace along with it. Call this method only from an exception handler.Logger.log()takes a log level as an explicit argument. This is a little more verbose for logging messages than using the log level convenience methods listed above, but this is how to log at custom log levels.
getLogger() returns a reference to a logger instance with the specified
name if it is provided, or root if not. The names are period-separated
hierarchical structures. Multiple calls to getLogger() with the same name
will return a reference to the same logger object. Loggers that are further
down in the hierarchical list are children of loggers higher up in the list.
For example, given a logger with a name of foo, loggers with names of
foo.bar, foo.bar.baz, and foo.bam are all descendants of foo.
Loggers have a concept of effective level. If a level is not explicitly set
on a logger, the level of its parent is used instead as its effective level.
If the parent has no explicit level set, its parent is examined, and so on -
all ancestors are searched until an explicitly set level is found. The root
logger always has an explicit level set (WARNING by default). When deciding
whether to process an event, the effective level of the logger is used to
determine whether the event is passed to the logger’s handlers.
Child loggers propagate messages up to the handlers associated with their
ancestor loggers. Because of this, it is unnecessary to define and configure
handlers for all the loggers an application uses. It is sufficient to
configure handlers for a top-level logger and create child loggers as needed.
(You can, however, turn off propagation by setting the propagate
attribute of a logger to False.)
Handlers¶
Handler objects are responsible for dispatching the
appropriate log messages (based on the log messages’ severity) to the handler’s
specified destination. Logger objects can add zero or more handler
objects to themselves with an addHandler() method. As an example
scenario, an application may want to send all log messages to a log file, all
log messages of error or higher to stdout, and all messages of critical to an
email address. This scenario requires three individual handlers where each
handler is responsible for sending messages of a specific severity to a specific
location.
The standard library includes quite a few handler types (see
Useful Handlers); the tutorials use mainly StreamHandler and
FileHandler in its examples.
There are very few methods in a handler for application developers to concern themselves with. The only handler methods that seem relevant for application developers who are using the built-in handler objects (that is, not creating custom handlers) are the following configuration methods:
The
setLevel()method, just as in logger objects, specifies the lowest severity that will be dispatched to the appropriate destination. Why are there twosetLevel()methods? The level set in the logger determines which severity of messages it will pass to its handlers. The level set in each handler determines which messages that handler will send on.setFormatter()selects a Formatter object for this handler to use.addFilter()andremoveFilter()respectively configure and deconfigure filter objects on handlers.
Application code should not directly instantiate and use instances of
Handler. Instead, the Handler class is a base class that
defines the interface that all handlers should have and establishes some
default behavior that child classes can use (or override).
Formatters¶
Formatter objects configure the final order, structure, and contents of the log
message. Unlike the base logging.Handler class, application code may
instantiate formatter classes, although you could likely subclass the formatter
if your application needs special behavior. The constructor takes three
optional arguments – a message format string, a date format string and a style
indicator.
- logging.Formatter.__init__(fmt=None, datefmt=None, style='%')¶
If there is no message format string, the default is to use the raw message. If there is no date format string, the default date format is:
%Y-%m-%d %H:%M:%S
with the milliseconds tacked on at the end. The style is one of '%',
'{', or '$'. If one of these is not specified, then '%' will be used.
If the style is '%', the message format string uses
%(<dictionary key>)s styled string substitution; the possible keys are
documented in LogRecord attributes. If the style is '{', the message
format string is assumed to be compatible with str.format() (using
keyword arguments), while if the style is '$' then the message format string
should conform to what is expected by string.Template.substitute().
Changed in version 3.2: Added the style parameter.
The following message format string will log the time in a human-readable format, the severity of the message, and the contents of the message, in that order:
'%(asctime)s - %(levelname)s - %(message)s'
Formatters use a user-configurable function to convert the creation time of a
record to a tuple. By default, time.localtime() is used; to change this
for a particular formatter instance, set the converter attribute of the
instance to a function with the same signature as time.localtime() or
time.gmtime(). To change it for all formatters, for example if you want
all logging times to be shown in GMT, set the converter attribute in the
Formatter class (to time.gmtime for GMT display).
Configuring Logging¶
Programmers can configure logging in three ways:
Creating loggers, handlers, and formatters explicitly using Python code that calls the configuration methods listed above.
Creating a logging config file and reading it using the
fileConfig()function.Creating a dictionary of configuration information and passing it to the
dictConfig()function.
For the reference documentation on the last two options, see Configuration functions. The following example configures a very simple logger, a console handler, and a simple formatter using Python code:
import logging
# create logger
logger = logging.getLogger('simple_example')
logger.setLevel(logging.DEBUG)
# create console handler and set level to debug
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
# add formatter to ch
ch.setFormatter(formatter)
# add ch to logger
logger.addHandler