Skip to content
Snippets Groups Projects
Brandon Rodriguez's avatar
Brandon Rodriguez authored
7513aacd

Python - Custom Logger

Description

Custom logging file to be imported into other projects.

Improves logging handling above and beyond Python's default definitions.

References

See documents/references.md for various references used for project.

Includes some helper links for expanding/customizing logging above the defaults this project provides.

Usage

Basic Usage

Copy the logging.py file (found at project root) into the desired project subfolder.

Then import and initialize with:

from <log_file_path> import init_logging

...

logger = init_logging(__name__)

Default Handling

With default settings, logging levels and corresponding level numbers are:

  • 10 - Debug
  • 20 - Info
  • 30 - Warning
  • 40 - Error

The debug level only logs to file. All other levels log to both console and file.

Logging will create a file for each level, and all logs of that level and higher are saved to the file.

For more on defaults, see Modifying Defaults below.

Modifying Logging Defaults

Optional arguments exist in the init_logging method to overriding of default values:

  • logging_dir - The directory to save logs to.
    • Default is <project_root>/src/logs.
  • handler_class - The type of logging handler to process logs with.
    • Default is 'logging.handlers.RotatingFileHandler'.
  • max_file_bytes - The maximum file size for each file, before rotating out and creating a new file.
    • Default is 10 MB.
  • log_backup_count - The maximum number of files to keep of each log type, before overriding oldest.
    • Default is 10 files per log file type.

Note: If these values are modified after logging is initialized, then it will raise an error. Only change these defaults wherever your project calls logging for the first time.

Modifying the Logging Settings Dictionary

The logging "settings dictionary" is where a majority of log settings are defined. This is located in the get_logging_settings function.

See documents/references.md for links on general modification of a Python logging settings dictionary.

Adding Additional Log Levels

To define additional log levels, add a single line under the set_new_log_levels function (line 115):

add_logging_level(<log_level_name>, <log_level_num>, <optional_log_method_name>)

For example, if you want a new log level named DATA that is higher than INFO, but lower than WARNING, you could do this:

add_logging_level('DATA', 25)