8.3.3 Complex Log Formatting
Here is a list of useful formatting variables for the format string. Note that this list is
not complete, consult the ofﬁcial documentation of thelogging package for a full list.
Text logging level for the message (’DEBUG’, ’INFO’, ’WARNING’,
Full pathnameofthesourceﬁlewherethelogging call was issued
Filename portion of pathname.
Module (name portion of ﬁlename).
Name of function containing the logging call.
Source line number where the logging call was issued (if avail-
Human-readable time when the LogRecord‘ was created. By de-
fault this is of the form "2003-07-08 16:49:45,896" (the num-
bers after the comma are millisecond portion of the time). This
can be changed by subclassing the formatter and overriding the
The logged message, computed as msg % args
If you want to further customize the formatting, you can subclass the formatter. The
formatter has three interesting methods:
format():handlestheactualformatting. Itispasseda LogRecordobjectandhasto
return the formatted string.
can override this method.
has to return a string. The default is usually ﬁne, you don’t have to override it.
For more information, head over to the ofﬁcial documentation.
8.4 Other Libraries
So far we only conﬁgured the logger your application created itself. Other libraries
might log themselves as well. For example, SQLAlchemy uses logging heavily in its
core. While there is a method to conﬁgure all loggers at once in thelogging package, I
would not recommend using it. There might be a situation in which you want to have
multiple separateapplications running side by side inthe same Pythoninterpreter and
then it becomes impossible to have different logging setups for those.
Instead, I would recommend ﬁguring out which loggers you are interested in, getting
the loggers with thegetLogger() function and iterating over them to attach handlers:
from logging import t getLogger
loggers = [app.logger, getLogger(sqlalchemy),