Skip to main content

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.

Overriding loggers during runtime

If you need, you can provide an additional 'override_config_file'.

init_logging(
    "./log_config.toml",
    loader=TomlLoader(),
    processors=[JinjaProcessor(placeholder_container)],
    override_config_file="path/to/override.toml"
)

If you do so, a watcher will be started that checks in an interval whether the override_config_file got any updates. If it did, it will override all of the provided loggers from the base config file.

For example:

#base config file
[loggers."access"]
level = "INFO"

[loggers."kubernetes.client.rest"]
level = "ERROR"

#override_config_file
[loggers."access"]
level = "ERROR"

The watcher will set the access logger to ERROR and keep the kubernetes one at ERROR. Upon removing the access one from the override file, the access logger will be reset to INFO.

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 Mypy
  • Configure the python interpreter/venv
  • pip install requirements-dev.txt
  • 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

Metadata

Release files for sag-py-logging 0.6.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sag-py-logging 0.6.0
File Size Uploaded Provenance Actions
sag_py_logging-0.6.0.tar.gz 12.4 kB Without provenance Details for sag_py_logging-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for sag-py-logging 0.6.0
File Interpreter ABI Platform Actions
sag_py_logging-0.6.0-py3-none-any.whl Python 3 none any Details for sag_py_logging-0.6.0-py3-none-any.whl

Total release size: 21.5 kB

Release files / sag_py_logging-0.6.0.tar.gz

Download URL sag_py_logging-0.6.0.tar.gz
Size 12.4 kB
Tags Source
SHA-256 checksum
How to use checksums
ae3e1035428cc4cbb20dc8c4b62588118c0c51288464debb8dac75d534e7d822
BLAKE2b-256 checksum
How to use checksums
c8b4662607a8bcb974b49ddc80dd0bece0378e179a4571e72508f882e0d72db1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Release files / sag_py_logging-0.6.0-py3-none-any.whl

Download URL sag_py_logging-0.6.0-py3-none-any.whl
Size 9.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
85dd34504b6fe5402e1e3533184b40e373b3b92bc9e5e6939f64ea8652e6c1e4
BLAKE2b-256 checksum
How to use checksums
cb936f838f285e02ac9dce002e267646d5a357ef877d3d698f790d8edcc68875
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.7

Security for sag-py-logging 0.6.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page