Logger

Module for logging configuration.

class AuditFilter(name='')[source]

Filter to separate audit logs from regular logs.

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

filter(record)[source]

Filter records based on the presence of audit attribute.

Return type:

bool

Parameters:

record (LogRecord) – The log record to check

Returns:

True if the record should be processed, False otherwise

Return type:

bool

class AuditFormatter(fmt=None, datefmt=None)[source]

Custom formatter that handles user-specific logging configuration.

Initialize the formatter with config.

Parameters:
  • config (Config) – The application configuration

  • fmt (Optional[str], optional) – Format string. Defaults to None.

  • datefmt (Optional[str], optional) – Date format string. Defaults to None.

_get_extra_fields(record)[source]

Extract additional fields from the record.

Return type:

dict[str, Any]

Parameters:

record (LogRecord) – The log record

Returns:

Dictionary of extra fields

Return type:

dict[str, Any]

_get_syslog_priority(levelno)[source]

Convert Python logging levels to syslog priorities.

Return type:

int

Parameters:

levelno (int) – Python logging level number

Returns:

Corresponding syslog priority

Return type:

int

format(record)[source]

Format the record as JSON for journald consumption.

Return type:

str

Parameters:

record (logging.LogRecord) – The log record to format

Returns:

JSON formatted log message

Return type:

str

DEFAULT_DATE_FORMATTER: str = '%m/%d/%Y %I:%M:%S %p'

Default date formatter string for systemd/terminal

DEFAULT_FORMATTER: str = '[%(asctime)s] [%(filename)s:%(lineno)d] %(levelname)s: %(message)s'

Default formatter string for systemd/terminal

EXTRAS_TO_SKIP = ('args', 'asctime', 'created', 'exc_info', 'exc_text', 'filename', 'funcName', 'levelname', 'levelno', 'lineno', 'taskName', 'module', 'server', 'thread', 'process', 'processName', 'msecs', 'msg', 'name', 'pathname', 'relativeCreated', 'stack_info', 'threadName', 'audit', 'user_id')

Set of keys to skip during auditting. If any of those needs to be in the audit log, simply remove them from this list.

LOGGING_CONFIG_DICTIONARY = {'disable_existing_loggers': False, 'filters': {'audit_only': {'()': 'command_line_assistant.logger.AuditFilter'}, 'non_audit_only': {'()': 'command_line_assistant.logger.NonAuditFilter'}}, 'formatters': {'audit': {'()': 'command_line_assistant.logger.AuditFormatter', 'datefmt': '%m/%d/%Y %I:%M:%S %p', 'format': '[%(asctime)s] [%(filename)s:%(lineno)d] %(levelname)s: %(message)s'}, 'systemd': {'datefmt': '%m/%d/%Y %I:%M:%S %p', 'format': '[%(asctime)s] [%(filename)s:%(lineno)d] %(levelname)s: %(message)s'}, 'terminal': {'datefmt': '%m/%d/%Y %I:%M:%S %p', 'format': '\x1f[%(asctime)s] [%(filename)s:%(lineno)d] %(levelname)s: %(message)s'}}, 'handlers': {'audit': {'class': 'logging.StreamHandler', 'filters': ['audit_only'], 'formatter': 'audit', 'stream': 'ext://sys.stdout'}, 'systemd': {'class': 'logging.StreamHandler', 'filters': ['non_audit_only'], 'formatter': 'systemd', 'stream': 'ext://sys.stdout'}, 'terminal': {'class': 'logging.StreamHandler', 'filters': ['non_audit_only'], 'formatter': 'terminal', 'stream': 'ext://sys.stdout'}}, 'level': 'INFO', 'loggers': {'root': {'handlers': [], 'level': 'INFO'}}, 'version': 1}

Define the dictionary configuration for the logger instance

class NonAuditFilter(name='')[source]

Filter to separate regular logs from audit logs.

Initialize a filter.

Initialize with the name of the logger which, together with its children, will have its events allowed through the filter. If no name is specified, allow every event.

filter(record)[source]

Filter records based on the absence of audit attribute.

Return type:

bool

Parameters:

record (LogRecord) – The log record to check

Returns:

True if the record should be processed, False otherwise

Return type:

bool

_setup_logging(logging_level, handlers)[source]

Internal method to handle logging configuration and initialization.

Return type:

None

Parameters:
  • logging_level (str) – The mininaml level to enable

  • handlers (list[str]) – A list of handlers to add to the root loger.

setup_client_logging()[source]

Setup basic logging functionality.

Return type:

None

Note

This is intended to be called by the client to initialize their logging routine.

setup_daemon_logging(config)[source]

Setup basic logging functionality.

Return type:

None

Note

This is intended to be called by the daemon to initialize their logging routine.

Parameters:

config (Config) – Instance of a config class.