Skip to main content

Simple Python logging utilities with custom formatters, daily rotation, and request ID tracking

Project description

linlog

Simple Python logging utilities with daily rotation and UUID tracking.

Features

  • StandardFormatter: Clean, consistent log format [time][level][name:line]message
  • DailyRotatingHandler: Automatic daily log rotation with custom naming (app_2024-12-02.log)
  • UUIDFilter: Track requests across multiple log entries with unique IDs
  • Multi-process safe: File locking prevents race conditions (uwsgi, gunicorn)
  • Zero dependencies: Uses only Python standard library

Installation

pip install linlog

Or install from source:

git clone https://github.com/yourusername/linlog.git
cd linlog
pip install -e .

Quick Start

Basic Usage

import logging
from linlog import StandardFormatter, DailyRotatingHandler

# Create logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

# Create handler with daily rotation
handler = DailyRotatingHandler(
    filename='logs/app.log',
    when='midnight',
    backupCount=30,  # Keep 30 days of logs
    encoding='utf-8'
)

# Set formatter
handler.setFormatter(StandardFormatter())
logger.addHandler(handler)

# Use logger
logger.info("Application started")
logger.error("Something went wrong")

Output:

[2024-12-02 14:23:45][INFO][__main__:15]Application started
[2024-12-02 14:23:46][ERROR][__main__:16]Something went wrong

Django Integration

See USAGE_EXAMPLE.md for complete Django setup with UUID tracking.

settings.py

LOGGING = {
    'version': 1,
    'formatters': {
        'standard': {
            '()': 'linlog.formatters.StandardFormatter',
        },
    },
    'handlers': {
        'file': {
            'class': 'linlog.handlers.DailyRotatingHandler',
            'filename': 'logs/app.log',
            'when': 'midnight',
            'backupCount': 30,
            'formatter': 'standard',
        },
    },
    'loggers': {
        '': {
            'handlers': ['file'],
            'level': 'INFO',
        },
    },
}

Components

StandardFormatter

Formats logs in a clean, consistent style:

[2024-12-02 14:23:45][INFO][module.name:98]message content

DailyRotatingHandler

  • Rotates logs daily at midnight
  • Custom naming: app_2024-12-02.log
  • Auto-cleanup: keeps only N days of logs
  • Multi-process safe with file locking

UUIDFilter

Track all logs from the same request:

from linlog import UUIDFilter, set_request_id
import uuid

# In middleware
set_request_id(str(uuid.uuid4()))

# Add filter to handler
handler.addFilter(UUIDFilter())

All logs will include record.request_id for correlation.

Requirements

  • Python 3.8+
  • No external dependencies

Production Use

Multi-process environments (uwsgi, gunicorn)

linlog uses file locking to prevent race conditions during log rotation:

[uwsgi]
processes=10        #  Safe
threads=4           #  Safe
enable-threads=true

All processes can safely write to the same log file.

API Reference

Formatters

StandardFormatter(datefmt='%Y-%m-%d %H:%M:%S')

Standard log formatter with customizable date format.

JSONFormatter()

JSON format for machine parsing and log analysis tools.

Handlers

DailyRotatingHandler(filename, when='midnight', backupCount=0, ...)

Daily rotating file handler with multi-process safety.

Parameters:

  • filename: Log file path
  • when: Rotation interval ('midnight', 'H', 'D')
  • backupCount: Number of backup files to keep (0 = keep all)
  • encoding: File encoding (default: utf-8)

Filters

UUIDFilter()

Adds request UUID to log records as record.request_id.

Context Management

set_request_id(request_id: str)

Set UUID for current request context.

get_request_id() -> str | None

Get UUID from current request context.

clear_request_id()

Clear UUID after request completes.

License

MIT

Author

Kuan Lin

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

linlog_py-0.1.2.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

linlog_py-0.1.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file linlog_py-0.1.2.tar.gz.

File metadata

  • Download URL: linlog_py-0.1.2.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for linlog_py-0.1.2.tar.gz
Algorithm Hash digest
SHA256 32298a114a44aeb28bc4f04c6c4038c39da571767f3e7b1fd25052aa7e26aeb9
MD5 7426bcbb0f72addffb10ae23d400d9c2
BLAKE2b-256 547783f6795b550e3f9fb74f213c73a493d581b7d17b734d3768842a3f5e4edd

See more details on using hashes here.

File details

Details for the file linlog_py-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: linlog_py-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for linlog_py-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 13bfc321e16acb956498be598f7d7ea70ec2e96fdbb3a6cf6d4a4c02d5ccdd10
MD5 f15bcff9e7b87a16f5473d8811f8b250
BLAKE2b-256 4e75b850707095576f7be394be987937a55c581efa41a2ffe94e11c6f2ed1a7c

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