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_TAGS: Tags to associate with the logs (or empty).DD_API_ENDPOINT: The Datadog API endpoint for log ingestion. (default:https://http-intake.logs.datadoghq.eu/api/v2/logs)DD_API_KEY: Your Datadog API key.LOG_LEVEL: The log level threshold (default:error). Possible values:debug,info,warn,error.LOG_ENVIRONMENT: The logging environment (default:development, usually this isproduction,stagingordevelopment).
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.eu/api/v2/logs
DD_API_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",
# NOTE:
# This will obfuscate the context (list or dict) fields
# recursively, with the character '*'.
# Matches are case-insensitive.
# INFO: This is completely optional.
obfuscate_context_fields=["password", "email", "cc_number", "cvv"],
obfuscate_context_character="*"
)
# 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,
"email": "foo@bar.de",
"password": "secret",
"cc_number": "1234567890",
"cvv": "123"
}
)
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
- Environment Configuration: Reads environment variables for Datadog configuration.
- Log Message Construction: Formats log messages with metadata and timestamp.
- Datadog Integration: Sends logs to Datadog via API.
- 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
rm -rf dist/*
python3 -m pip install -r requirements.txt
python3 -m build
python3 -m twine upload --repository pypi dist/*
Update Requirements
python3 -m pip freeze > requirements.txt
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tbint_logger-0.3.2.tar.gz.
File metadata
- Download URL: tbint_logger-0.3.2.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
73f7b0581eaec75976b83fcb7962f31c5d80d571330ea23b973fa29c563d4b5f
|
|
| MD5 |
444751542d9c653b5c01e1002ac27394
|
|
| BLAKE2b-256 |
823cc40f232fd206951d20ed6203087da16dc356ea9b8b8bfbb83e56b4181a27
|
File details
Details for the file tbint_logger-0.3.2-py3-none-any.whl.
File metadata
- Download URL: tbint_logger-0.3.2-py3-none-any.whl
- Upload date:
- Size: 7.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.11
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
810d6647f5b94757aa4600b5751d9a384122ca80fc9e8c8634832f4af6bd3f59
|
|
| MD5 |
81bd56988e7932f8d8500a22eb086371
|
|
| BLAKE2b-256 |
9265c0612ce9adbd3eb05817a3ee7357fb3b8e6ccb31fdd1e5da9d2a41639b9c
|