Logging
The Logging Extension includes the LoggingLogHandler, and provides log handling based on the standard
logging
library.Documentation References:
API References:
- No external dependencies
This handler honors the following settings under a
[log.logging]
section of the configuration:Setting | Description |
level | The logging to display logs for. One of INFO, WARNING, ERROR, FATAL, DEBUG . Default: INFO |
file | The filesystem path of the log file. Default: None |
to_console | Whether or not to log to console. Default: True |
rotate | Whether or not rotate the log file. Default: False |
max_bytes | Maximum file size (in bytes) until the log file is rotated (if rotation is enabled). Default: 512000 |
max_files | Maximum number of files to keep when rotating is enabled. Default: 4 |
A sample config section might look like:
~/.myapp.conf
[log.colorlog]
file = /path/to/config/file
level = info
to_console = true
rotate = true
max_bytes = 512000
max_files = 4
This extension supports an optional feature to add a command-line argument to toggle the log level. This feature is not enabled by default for one specific reason: the log level will not be modified until after argument parsing happens. This can lead to a lot of confusion for developers who might not see their debug logs from a
pre_setup
hook, or anything that happens before argument parsing completes. For this reason, you should use this feature with caution and thus we disable it by default.You can enable the log level argument by setting via
App.Meta.meta_defaults
for the log.logging
handler:Example: Enabling Log Level Argument
cli
from cement import App, init_defaults
META = init_defaults('log.logging')
META['log.logging']['log_level_argument'] = ['-l', '--level']
class MyApp(App):
class Meta:
label = 'myapp'
meta_defaults = META
$ python myapp.py --help
usage: myapp [-h] [--debug] [--quiet] [-l {info,warning,error,debug,fatal}]
optional arguments:
-h, --help show this help message and exit
--debug full application debug mode
--quiet suppress all output
-l {info,warning,error,debug,fatal}
logging level
$ python myapp.py
INFO: This is an info message
WARNING: This is an warning message
ERROR: This is an error message
CRITICAL: This is a fatal message
$ python myapp.py -l error
ERROR: This is an error message
CRITICAL: This is a fatal message
Last modified 5yr ago