Skip to main content

A python logger for CHIME/FRB with Loki support.

Project description

CHIME Logger

Chime Logger is a Python logging extension that provides emitters for sending logs to Grafana Loki and a file, and custom logging filters for pipeline and event tagging. It is designed for easy integration with Python applications and supports advanced log shipping and tagging use cases. It runs asynchronously, making it suitable for high-performance applications.

Features

  • Loki Emitters: Send logs to Loki using different API versions (v0, v1, v2), with support for custom tags and HTTP headers.
  • File Emitter: Write logs to a local file for debugging and redundancy purposes.
  • Custom Filters: Automatically add default resource_name, resource_type, pipeline and site attributes to log records for consistent tagging.

Installation

Install the package using your favourite package manager:

pip

pip install chime_logger

poetry

poetry add chime_logger

uv

uv add chime_logger

Usage

Once you have installed the package, you can use it in your Python application as follows:

import chime_logger, logging
from chime_logger.context import LoggerContext, DynamicLoggerAdapter

chime_logger.setup_logging()

chime_log = logging.getLogger("CHIME")

context = LoggerContext(
    resource_name="a",
    resource_type="N2_Acquisition",
    pipeline="datatrail-registration",
    site="chime",
)

chime_log = DynamicLoggerAdapter(chime_log, context)

chime_log.info("Testing")

context.resource_name = "b"

chime_log.info("Testing take two")

Use of the DynamicLoggerAdapter adds context to your log messages. This allows you to dynamically change the context for different parts of your application without modifying the logger configuration.

The context fields are validated to ensure that they match the expected types and formats. If a field is set to an invalid value, a ValidationError is raised. Eg.

In [10]: context.resource_type = "Fake type"
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
Cell In[10], line 1
----> 1 context.resource_type = "Fake type"

File ~/Library/Caches/pypoetry/virtualenvs/chime-logger-5IKBwNMQ-py3.13/lib/python3.13/site-packages/pydantic/main.py:394, in BaseModel.__setattr__(self, name, value)
    392 value, error_ = known_field.validate(value, dict_without_original_value, loc=name, cls=self.__class__)
    393 if error_:
--> 394     raise ValidationError([error_], self.__class__)
    395 else:
    396     new_values[name] = value

ValidationError: 1 validation error for LoggerContext
resource_type
  unexpected value; permitted: 'event', 'n2_acquisition', 'raw_adc' (type=value_error.const; given=fake type; permitted=('event', 'n2_acquisition', 'raw_adc'))

Configuration

If you want to send logs to a custom Loki instance or modify the default parameters, configure the logger using these environment variables:

  • CHIME_LOGGER_PIPELINE_NAME: Name of the pipeline to attach to log records, by default it is set to "unknown_pipeline".
    export CHIME_LOGGER_PIPELINE_NAME="your-pipeline-name"
    
    Default: "unknown_pipeline"
  • CHIME_LOGGER_LOKI_URL: URL of your Loki instance
    export CHIME_LOGGER_LOKI_URL="http://your-loki-instance:3100"
    
    Default: "https://frb.chimenet.ca/loki/loki/api/v1/push"
  • CHIME_LOGGER_LOKI_TENANT: (Optional) Tenant ID for multi-tenancy
    export CHIME_LOGGER_LOKI_TENANT="your-tenant-id"
    
    Default: "CHIME". Note that this is ignored in Loki deployments that don't use multi-tenancy.
  • CHIME_LOGGER_LOKI_USER and CHIME_LOGGER_LOKI_PASSWORD: (Optional) Credentials for authentication
    export CHIME_LOGGER_LOKI_USER="your-username"
    export CHIME_LOGGER_LOKI_PASSWORD="your-password"
    
    Default: None (no authentication)
  • CHIME_LOGGER_FILE_LOG_PATH: (Optional) Path to a file where logs will be written. This is useful for local debugging.
    export CHIME_LOGGER_FILE_LOG_PATH="/path/to/your/logfile.log"
    
    Default: "logs/my_app.log"

License

See LICENSE for details.

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

chime_logger-0.4.2.tar.gz (11.2 kB view details)

Uploaded Source

Built Distribution

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

chime_logger-0.4.2-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file chime_logger-0.4.2.tar.gz.

File metadata

  • Download URL: chime_logger-0.4.2.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.9.25 Linux/6.17.0-1010-azure

File hashes

Hashes for chime_logger-0.4.2.tar.gz
Algorithm Hash digest
SHA256 956450b064c2823054f862263160ee370b6f8023ef98bd150bcba1f10a99f78a
MD5 e7731aa2ed2ccc2e85bc9629ca4f121b
BLAKE2b-256 832bd86d6e0e00847a86810dbdcf82b02933f4143307b4f0f216f4e4fbaf8e41

See more details on using hashes here.

File details

Details for the file chime_logger-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: chime_logger-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.9.25 Linux/6.17.0-1010-azure

File hashes

Hashes for chime_logger-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 4dfbac1fc73cbb4579e96a68bb590405b8fdf7917d8fbe06c7d17c60487da533
MD5 a121314b670fe00674f0486bdd9e3b22
BLAKE2b-256 77c9a089f42ba10e4b3b49613ec4affe487f7d54ade9de78c46ff0b355c9984b

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