Skip to main content

logging_ext_ramwin

Extended logging handlers with date-based filename formatting for Python.

Features

  • Date-based filename patterns: Automatically create log files with names based on current date/time
  • Automatic cleanup: Keep only the most recent log files with configurable backup_count
  • Multi-process safe: Concurrent file operations with proper error handling
  • Flexible patterns: Support any strftime format codes (e.g., %Y-%m-%d, %Y-%m-%d_%H, etc.)
  • Easy integration: Drop-in replacement for standard logging handlers

Installation

pip install logging_ext_ramwin

Quick Start

Basic Daily Logging

import logging
from logging_ext import DateBasedFileHandler

# Create handler that creates daily log files
handler = DateBasedFileHandler(
    filename_pattern="logs/app-%Y-%m-%d.log",
    backup_count=7  # Keep last 7 days
)

formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)

logger = logging.getLogger("my_app")
logger.setLevel(logging.INFO)
logger.addHandler(handler)

# Log some messages
logger.info("Application started")
logger.error("Something went wrong")

Hourly Log Rotation

from logging_ext import DateBasedFileHandler

# Create hourly log files
handler = DateBasedFileHandler(
    filename_pattern="logs/app-%Y-%m-%d_%H.log",
    backup_count=24  # Keep last 24 hours
)

Multi-Process Safe Logging

from logging_ext import ConcurrentDateBasedFileHandler

# Thread-safe version for multi-process applications
handler = ConcurrentDateBasedFileHandler(
    filename_pattern="logs/app-%Y-%m-%d.log",
    backup_count=7
)

Filename Patterns

Pattern Example Output Description
%Y-%m-%d 2024-01-15 Daily logs
%Y-%m-%d_%H 2024-01-15_14 Hourly logs
%Y-%m-%d_%H-%M 2024-01-15_14-30 Minute-based logs
%Y-%W 2024-03 Weekly logs
%Y-%m 2024-01 Monthly logs

Advanced Usage

Custom Cleanup Logic

handler = DateBasedFileHandler(
    filename_pattern="logs/app-%Y-%m-%d.log",
    backup_count=30  # Keep 30 days of logs
)

Combining with Other Handlers

import logging
from logging_ext import DateBasedFileHandler

# Create logger
logger = logging.getLogger("my_app")
logger.setLevel(logging.DEBUG)

# Console handler for immediate feedback
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO)

# File handler for persistent logging
file_handler = DateBasedFileHandler(
    filename_pattern="logs/app-%Y-%m-%d.log",
    backup_count=7
)
file_handler.setLevel(logging.DEBUG)

# Formatters
console_formatter = logging.Formatter('%(levelname)s - %(message)s')
file_formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')

console_handler.setFormatter(console_formatter)
file_handler.setFormatter(file_formatter)

# Add handlers
logger.addHandler(console_handler)
logger.addHandler(file_handler)

API Reference

DateBasedFileHandler

DateBasedFileHandler(
    filename_pattern: str,      # Pattern for log filenames using strftime format
    backup_count: int = 0,      # Number of old log files to keep (0 = no cleanup)
    encoding: str = "utf-8",    # File encoding
    delay: bool = False,        # Delay file opening until first log message
    mode: str = "a"             # File opening mode
)

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

MIT License

Usage 使用说明(Chinese)

这个库提供了以下主要功能:

  1. DateBasedFileHandler: 基础的日期文件日志处理器
  2. ConcurrentDateBasedFileHandler: 支持基本文件锁定的并发安全版本

主要特性:

  • ✅ 支持任意strftime格式模式(如%Y-%m-%d, %Y-%m-%d_%H等)
  • ✅ 自动清理旧的日志文件(通过backup_count参数)
  • ✅ 多进程并发安全(处理文件被其他进程删除的情况)
  • ✅ 自动创建必要的目录
  • ✅ 健壮的错误处理
  • ✅ 使用简单,与标准logging模块完美集成

安装和使用:

# 安装
pip install logging_ext_ramwin

# 使用
from logging_ext import DateBasedFileHandler

handler = DateBasedFileHandler(
    filename_pattern="logs/app-%Y-%m-%d.log",
    backup_count=7  # 保留最近7天的日志
)

Metadata

Release files for logging_ext_ramwin 0.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logging_ext_ramwin 0.2.2
File Size Uploaded
logging_ext_ramwin-0.2.2.tar.gz 10.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for logging_ext_ramwin 0.2.2
File Interpreter ABI Platform
logging_ext_ramwin-0.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.5 kB

Release files / logging_ext_ramwin-0.2.2.tar.gz

Download URL logging_ext_ramwin-0.2.2.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
9e9e911a57ea37d0e47608943c74edf27710272ecc8a91e34091770e8ffa4d62
BLAKE2b-256 checksum
How to use checksums
67839b4d5e76a9ae58606e675c3ccc6c0a261129ca53cf48e828d7215ac50b92
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release files / logging_ext_ramwin-0.2.2-py3-none-any.whl

Download URL logging_ext_ramwin-0.2.2-py3-none-any.whl
Size 7.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
efc3233c2611cf2dc3ed380be03170e7b631eba72fd8121a1afa92279a3111ce
BLAKE2b-256 checksum
How to use checksums
bb1f46445920acce440606c4adde21994f7e4a81df974e10d36c16974aa6c510
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.28.1

Release history Release notifications | RSS feed

This release

0.2.2 This release

2 release files

0.2.1

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page