Skip to main content

A customizable logger and traceback handler for Python applications

Project description

FlexiLogger

FlexiLogger is a customizable Python logging library that provides enhanced features for handling logs, including colorized console outputs, structured JSON logging, log rotation, and context binding.

Features

  • Structured JSON Logging: Production-ready format for ELK/Datadog/Splunk.
  • Context Binding: Add contextual data (user_id, request_id) to logs easily.
  • Automatic Log Rotation: Prevents log files from growing indefinitely.
  • Colorized Console: Better readability during development.
  • Dynamic Configuration: Configure via environment variables.
  • Timezone Support: UTC, Local, or custom offsets.

Installation

You can install FlexiLogger using pip:

pip install FlexiLogger

Usage

Basic Usage

To use FlexiLogger in your project:

from FlexiLogger import Logger

# Standard usage (defaults to UTC)
logger = Logger(__file__, log_file_path="app.log")
logger.info("This is an info message")

# Custom timezone and date format
logger_tz = Logger("CustomLogger", timezone="UTC+1", date_format="%Y-%m-%d %H:%M:%S")
logger_tz.info("This message uses UTC+1 and ISO format")

JSON Logging and Context Binding (Production Mode)

Enable JSON logging for machine-readable output and bind context to track requests:

# Enable JSON format (or set env LOGGER_JSON_FORMAT=true)
logger = Logger("AppLogger", log_file_path="app.json", json_format=True)

# Bind context (e.g., at the start of a request)
log = logger.bind(request_id="req-123", user_id=42)

log.info("Processing payment")
# Output: {"timestamp": "...", "level": "INFO", "message": "Processing payment", "request_id": "req-123", "user_id": 42, ...}

log.warning("Transaction slow", duration_ms=500)
# Output: {"timestamp": "...", "level": "WARNING", "message": "Transaction slow", "request_id": "req-123", "user_id": 42, "duration_ms": 500, ...}

Log Rotation

Prevent disk overflow by setting max size and backup count:

logger = Logger(
    "RotationLogger",
    log_file_path="app.log",
    max_bytes=10 * 1024 * 1024, # 10 MB
    backup_count=5
)

Advanced Traceback Handling

FlexiLogger provides a GetTraceback class for managing exceptions:

import os
from FlexiLogger import Logger, GetTraceback

logger = Logger(__file__)
traceback_handler = GetTraceback(logger)

try:
    1 / 0
except Exception as e:
    traceback_handler.error("An error occurred", print_full_exception=True)

Environment Variables

FlexiLogger uses several environment variables to customize its behavior:

Variable Name Description Default Value
LOG_PATH Specifies the path to the log file. If not set, logging to a file is disabled. None
LOGGER_JSON_FORMAT Enables JSON output format. Values: true/1 or false/0. false
LOG_TRACEBACK_PATH Specifies the path where traceback will be saved. If not set, the file defined in the Logger will be used. None
LOGGER_CONSOLE_LOG_LEVEL Sets the console log level. Acceptable values: DEBUG, INFO, WARNING, ERROR, CRITICAL. DEBUG
LOGGER_FILE_LOG_LEVEL Sets the file log level. Acceptable values: DEBUG, INFO, WARNING, ERROR, CRITICAL. DEBUG
LOGGER_TIME_INFO Enables or disables timestamps in log messages. Values: true/1 or false/0. true
LOGGER_TIMEZONE Sets the timezone for timestamps. Values: UTC (default), LOCAL, UTC+3, UTC-05:00, etc. UTC

Example

Set the environment variables before running your script:

export LOG_PATH="app.log"
export LOGGER_JSON_FORMAT="true"
export LOGGER_CONSOLE_LOG_LEVEL="INFO"
export LOGGER_TIMEZONE="UTC+3"

License

FlexiLogger is licensed under the MIT License. See the LICENSE file 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

flexilogger-1.0.6.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

flexilogger-1.0.6-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file flexilogger-1.0.6.tar.gz.

File metadata

  • Download URL: flexilogger-1.0.6.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for flexilogger-1.0.6.tar.gz
Algorithm Hash digest
SHA256 168d539a11d6677dae5bd525bf9b782883cf4eb9ff1a0a85ce6cff2daf2c499d
MD5 244bbfdf5e953eaa1dbf828e651e887d
BLAKE2b-256 51e85086e3ca81df8074ec4a8072a8996e961e55ee3ff9bc5fdda1d111f5040d

See more details on using hashes here.

File details

Details for the file flexilogger-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: flexilogger-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.26 {"installer":{"name":"uv","version":"0.9.26","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for flexilogger-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 ecc454d1c99c644924433a3e5fa9755dfe66c30912da2a1e4def010a438b81ad
MD5 bf94ddd189056430565227ae5234bc03
BLAKE2b-256 ea89270e73fce4a90c0e8773b18a9d9b1ce2767ae0ab24d925833dfc4d208372

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