Skip to main content

A simple logger for tbint projects

Project description

TBint Logger Python

TBIntLogger is a Python-based logging library designed to simplify logging messages and data to Datadog.

It supports both synchronous and asynchronous logging, providing flexibility for various application needs.

Features

  • Log messages at different levels: debug, info, warn, error.
  • Support for both synchronous and asynchronous logging.
  • Customizable through environment variables.
  • Easy integration with Datadog for centralized logging and monitoring.

Installation

You can install tbint-logger from PyPI:

pip install tbint-logger

Getting Started

Environment Variables

Before using TBIntLogger, set the following environment variables:

  • DD_SERVICE_NAME: The name of the service (default: unknown).
  • DD_SOURCE: The source of the logs (default: unknown, usually this is production or development).
  • DD_TAGS: Tags to associate with the logs (or empty).
  • DD_API_ENDPOINT: The Datadog API endpoint for log ingestion.
  • DD_APP_KEY: Your Datadog API key.
  • LOG_LEVEL: The log level threshold (default: debug). Possible values: debug, info, warn, error.

Example .env file:

DD_SERVICE_NAME=my-service
DD_SOURCE=production
DD_TAGS=env:production,team:backend
DD_API_ENDPOINT=https://http-intake.logs.datadoghq.com/api/v2/logs
DD_APP_KEY=your-datadog-api-key
LOG_LEVEL=info

Load the environment variables using python-dotenv:

Basic Usage

Synchronous Logging

from tbint_logger import Logger, LoggerData

# Init with default values
logger = Logger(
    system="my-system",
    component="auth",
    class_name="AuthService",
)

# Default values can be overridden
# on each call to the logger
data = LoggerData(
    system="my-system2",
    event="user-login",
    correlation_id="abc123",
    component="auth2",
    class_name="AuthService2",
    method="login",
    description="User successfully logged in",
    duration_ms=120,
    context={"user_id": 42}
)

logger.info_sync(data)

Asynchronous Logging

import asyncio
from tbint_logger import Logger, LoggerData

# Init with default values
logger = Logger(
    system="my-system",
    component="auth",
    class_name="AuthService",
)

# Default values can be overridden
# on each call to the logger
data = LoggerData(
    system="my-system2",
    event="user-login",
    correlation_id="abc123",
    component="auth2",
    class_name="AuthService2",
    method="login",
    description="User successfully logged in",
    duration_ms=120,
    context={"user_id": 42}
)

async def log_event():
    await logger.info(data)

asyncio.run(log_event())

Logging Levels

  • Debug: Use for detailed diagnostic information.

    logger.debug_sync(data)
    await logger.debug(data)
    
  • Info: Use for general informational messages.

    logger.info_sync(data)
    await logger.info(data)
    
  • Warn: Use for warnings that don't require immediate attention.

    logger.warn_sync(data)
    await logger.warn(data)
    
  • Error: Use for errors that require attention.

    logger.error_sync(data)
    await logger.error(data)
    

LoggerData Class

The LoggerData class is used to structure log messages. It accepts the following attributes:

Attribute Type Description
system str The system generating the log.
event str The event type (e.g., "user-login").
correlation_id str A unique identifier for correlating logs.
component str The system component generating the log.
class_name str The class name where the log originates.
method str The method where the log originates.
description str A description of the log event.
duration_ms int Duration of the event in milliseconds.
context dict Additional context data to include in the log.

How It Works

  1. Environment Configuration: Reads environment variables for Datadog configuration.
  2. Log Message Construction: Formats log messages with metadata and timestamp.
  3. Datadog Integration: Sends logs to Datadog via API.
  4. Sync/Async Options: Offers both synchronous and asynchronous logging for flexible use cases.

License

TBIntLogger is licensed under the MIT License. See the LICENSE file for details.

Development

python3 -m venv venv
source venv/bin/activate
python3 -m pip install --upgrade build
python3 -m build
python3 -m pip install --upgrade twine
python3 -m twine upload --repository pypi dist/*

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

tbint_logger-0.0.10.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

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

tbint_logger-0.0.10-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file tbint_logger-0.0.10.tar.gz.

File metadata

  • Download URL: tbint_logger-0.0.10.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for tbint_logger-0.0.10.tar.gz
Algorithm Hash digest
SHA256 9f4c3bfa091f71b57ad9514778cf5f9e436763c2e43e04324f615eb3df122473
MD5 80af43f8ae694373df4c39ace06b9c48
BLAKE2b-256 8cf06ae94556da5d835ed2b54760e47e6dd0e8f29cb54b1318ae630261eebc3d

See more details on using hashes here.

File details

Details for the file tbint_logger-0.0.10-py3-none-any.whl.

File metadata

  • Download URL: tbint_logger-0.0.10-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.14

File hashes

Hashes for tbint_logger-0.0.10-py3-none-any.whl
Algorithm Hash digest
SHA256 72c469b80a69fa4900cabde715c270119e661d4dc7fb7b9aa8dd963c11c3e864
MD5 3d5f53c727201f8f739c9a9641600cd3
BLAKE2b-256 f99f1fe8917487d54c11edd7c8637cb2640d642776d75660db213828bbcd4e85

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