Logging
Introduction to the Log Interface
Cement defines the Log Interface, as well as the default LoggingLogHandler that implements the interface. This handler is built on top of the Logging module which is included in the Python standard library.
Cement often includes multiple handler implementations of an interface that may or may not have additional features or functionality than the interface requires. The documentation below only references usage based on the interface and default handler (not the full capabilities of an implementation).
Cement Extensions That Provide Log Handlers
API References:
Configuration
Application Meta Options
The following options under App.Meta
modify configuration handling:
Option | Description |
log_handler | The handler that implements the log interface. |
Logging Messages
The following shows logging to each of the defined log levels.
The above is displayed in order of severity. If the log level is set to INFO
, you will receive all info messages and above including WARNING
, ERROR
, and FATAL
. However, you will not receive DEBUG
level messages. The same goes for a log level of WARNING
, where you will receive WARNING
, ERROR
, and FATAL
but you will not receive INFO, or DEBUG
level messages.
Changing Log Level
The log level defaults to INFO
, but can be set via LoggingLogHandler.Meta.config_defaults
or setting the level
under the log handlers section of the application configuration:
Cement also includes a --debug
command line option by default. This triggers App.Meta.debug
and sets the log level to DEBUG
.
Logging to Console
The default log handler configuration enables logging messages to console.
Console logging can be disabled by setting to_console
to False
in either the application defaults, or under the [log.logging]
section of the applications configuration file.
Logging to File
File logging is disabled by default but can be enabled by setting the file
setting under the [log.logging]
section of the application configuration.
Tips on Debugging
The following is specific to the default LoggingLogHandler
, and is not an requirement of the logging interface.
Logging to app.log.debug()
is pretty straightforward. However, adding an additional parameter for the namespace
can greatly increase insight into where that log is happening. The namespace
defaults to the application name which you will see in every log like this:
For debugging, it might be more useful to change this to __name__
:
Which looks like:
Or even more verbose, you might prefer adding something like __file__
and the function or class method:
Which would look like:
Creating a Log Handler
All interfaces in Cement can be overridden with your own implementation. This can be done either by sub-classing LogHandler
itself, or by sub-classing an existing extension's handlers in order to alter their functionality.
Last updated