This is the documentation for the built-in-logging functionality.
Its a subclass of the standard logging library included in python.
Click here to get back to the main page.
Table of Contents
logging.init()
To enable the syslog, filelog or streamlog, call the logging.init() function with the appropriate parameters.
Initialises the logging system with the specified options.
level
: int
, (optional)
The logging level (e.g., logging.DEBUG
, logging.INFO
). Defaults to logging.DEBUG
.
handler_type
: str
, (optional)
Type of logging handler ('syslog'
, 'file'
, 'stream'
). Defaults to 'syslog'
.
facility
: int
, (optional)
Syslog facility
if handler_type
is 'syslog'
. Defaults to SysLogHandler.LOG_DAEMON
.
address
: str
, (optional)
Address for syslog logging if handler_type
is ‘syslog’. Defaults to '/dev/log'
.
log_file_path
: str
, (optional)
Path to the log file if handler_type
is 'file'
. Required if handler_type
is 'file'
.
mode
: str
, (optional)
Mode for opening the log file if handler_type
is 'file'
. Defaults to 'a'
(append mode).
max_bytes
: int
, (optional)
Maximum size of the log file before rotation (used with 'file'
handler). Defaults to 10485760
bytes (10 MB).
backup_count
: int
, (optional)
Number of backup log files to keep (used with 'file'
handler). Defaults to 5
.
stream
: file-like object
, (optional)
Stream to log to if handler_type
is 'stream'
(e.g., sys.stdout
). Defaults to None
.
fmt
: str
, (optional)
Log message format. Defaults to "%(asctime)s - %(filename)s:%(funcName)s:%(lineno)d %(levelname)s - '%(message)s'"
.
datefmt
: str
, (optional)
Date/time format for log messages. Defaults to "%Y-%m-%d %H:%M:%S"
.
Examples
import cli
def test_logging():
try:
# Configure logging to a file
cli.logging.init(level=cli.logging.DEBUG, mode='w', handler_type='file', log_file_path='testing/test.log')
cli.info('CLI started\nYo')
cli.warn('Sup')
cli.debug('I am a debug')
cli.critical('Critical')
except Exception as e:
print(f"Exception occurred: {str(e)}")
if __name__ == "__main__":
test_logging()
Terminal output:
[01:34:47 / INFO] CLI started
[01:34:47 / INFO] Yo
[01:34:47 / WARN] Sup
[01:34:47 / DEBUG] I am a debug
[01:34:47 / CRITICAL] Critical
File output under testing/test.log
:
2024-07-09 01:34:47 - core.py:log:120 INFO - 'CLI started'
2024-07-09 01:34:47 - core.py:log:120 INFO - 'Yo'
2024-07-09 01:34:47 - core.py:log:124 WARNING - 'Sup'
2024-07-09 01:34:47 - core.py:log:122 DEBUG - 'I am a debug'
2024-07-09 01:34:47 - core.py:log:128 CRITICAL - 'Critical'