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

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

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

init_logging("./log_config.json", config.logging_config)

The json configuration

{
    "version": 1,
    "disable_existing_loggers": "true",
    "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

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:

{
    "handlers": {
        "myhandler": {
            "filters": ["console_extra_field_filter"]
        }
    },
    "filters": {
        "console_extra_field_filter": {"()": "sag_py_logging.console_extra_field_filter.ConsoleExtraFieldFilter"}
    }
}

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 publish

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

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.1.0.tar.gz (5.2 kB view hashes)

Uploaded Source

Built Distribution

sag_py_logging-0.1.0-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

Supported by

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