Skip to main content

Initialize logging from a configuration json

Project description

sag_py_logging

Maintainability Coverage Status Known Vulnerabilities

This library can be used to initialize the python logging by loading a config json. Furthermore it provides a way to log extra fields.

What it does

  • Initialize logging from configuration json
  • Placeholder support for the config json
  • A log filter to log extra fields

How to use

Installation

pip install sag-py-logging

pip install sag-py-logging[jinia] (optional for templating)

pip install sag-py-logging[toml] (optional for toml file support)

Initialize logging from json

Add the following as early as possible to your application code:

from sag_py_logging.log_config_initializer import init_logging
from sag_py_logging.log_config_loader import JsonLoader, TomlLoader
from sag_py_logging.log_config_processors import FormatProcessor, JinjaProcessor

placeholder_container = { "host": "myhost.com", ...}

# For toml config with jinja templating
init_logging(
    "./log_config.toml",
    loader=TomlLoader(),
    processors=[JinjaProcessor(placeholder_container)]
)

# For json config with format templating
init_logging(
    "./log_config.json",
    loader=JsonLoader(),
    processors=[FormatProcessor(placeholder_container)]
)

Init logging returns the log configuration as dictionary if needed for further processing.

The configuration

Json config:

{
    "version": 1,
    "disable_existing_loggers": false,
    "root": {
        "handlers": ["myhandler"],
        "level": "INFO"
    },
    "handlers": {
        "myhandler": {
            "host": "${host}",
            "formatter": "handler_formatter"
        }
    }
}

Toml config:

version = 1
disable_existing_loggers = false

[root]
handlers = ["myhandler"]
level = "INFO"

[handlers.myhandler]
host = "${host}"
formatter = "handler_formatter"

This is a very basic sample on the format of the file including placeholders.

Read the following for a full schema reference: https://docs.python.org/3/library/logging.config.html#configuration-dictionary-schema

Read more on string templating here: https://docs.python.org/3/library/string.html#template-strings

Or if you use jinja templating there: https://jinja.palletsprojects.com/en/3.1.x/templates/#template-designer-documentation

Configure extra field logging

It is possible to add a filter that extends log entries by a field for extra fields.

The filter is added like that if you initialize logging by code:

from sag_py_logging.console_extra_field_filter import ConsoleExtraFieldFilter

console_handler = logging.StreamHandler(sys.stdout)
console_handler.addFilter(ConsoleExtraFieldFilter())

If you init logging by config file the filter is added like that:

{
    "formatters": {
        "myformatter": {
            "format": "s%(asctime)s - %(name)s - %(message)s - %(stringified_extra)s",
        },
    },
    "filters": {
        "console_extra_field_filter": {"()": "sag_py_logging.console_extra_field_filter.ConsoleExtraFieldFilter"}
    },
    "handlers": {
        "myhandler": {
            "formatter": "myformatter",
            "filters": ["console_extra_field_filter"]
        }
    }
}

Afterwards you can use the field "stringified_extra" in your format string.

If you for example log the following:

logging.warning('Watch out!', extra={"keyOne": "valueOne", "keyTwo": 2})

The resulting log entry would look like that if stringified_extra is added to the end of the format string:

Watch out! {"keyOne": "valueOne", "keyTwo": 2}

Note: Internally json.dumps is used to convert the object/data to a string

How to start developing

With vscode

Just install vscode with dev containers extension. All required extensions and configurations are prepared automatically.

With pycharm

  • Install latest pycharm
  • Install pycharm plugin BlackConnect
  • Install pycharm plugin Mypy
  • Configure the python interpreter/venv
  • pip install requirements-dev.txt
  • pip install black[d]
  • Ctl+Alt+S => Check Tools => BlackConnect => Trigger when saving changed files
  • Ctl+Alt+S => Check Tools => BlackConnect => Trigger on code reformat
  • Ctl+Alt+S => Click Tools => BlackConnect => "Load from pyproject.yaml" (ensure line length is 120)
  • Ctl+Alt+S => Click Tools => BlackConnect => Configure path to the blackd.exe at the "local instance" config (e.g. C:\Python310\Scripts\blackd.exe)
  • Ctl+Alt+S => Click Tools => Actions on save => Reformat code
  • Restart pycharm

How to publish

  • Update the version in setup.py and commit your change
  • Create a tag with the same version number
  • Let github do the rest

How to test

To avoid publishing to pypi unnecessarily you can do as follows

  • Tag your branch however you like
  • Use the chosen tag in the requirements.txt-file of the project you want to test this library in, eg. sag_py_logging==<your tag>
  • Rebuild/redeploy your project

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sag_py_logging-0.4.1.tar.gz (10.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sag_py_logging-0.4.1-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file sag_py_logging-0.4.1.tar.gz.

File metadata

  • Download URL: sag_py_logging-0.4.1.tar.gz
  • Upload date:
  • Size: 10.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for sag_py_logging-0.4.1.tar.gz
Algorithm Hash digest
SHA256 7b6354a4d3e39be2c7a0261a6de08bd1cab922fe809f804435bf01f21034ab6c
MD5 57fb26ba6919966c412aa6fefe0fb37f
BLAKE2b-256 2ec55c771e3e896345b49e4c9038fd0a75a1d6a2eb07850b6feaada3cccf45ef

See more details on using hashes here.

File details

Details for the file sag_py_logging-0.4.1-py3-none-any.whl.

File metadata

  • Download URL: sag_py_logging-0.4.1-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for sag_py_logging-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1e02dc402b46fd5773b21e290477bfc5b3e18f7483fa57d1784be831fec717db
MD5 81da8be71bdfea39ea415009770bce86
BLAKE2b-256 11b74494390305c3cf63c2d4c360acc56ac2138ae8ddf1ec47e169d57e055b09

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page