Skip to main content

Privilege-aware, auto-rotating daily logger for Linux daemons & CLI tools

Project description

ChronicleLogger

A robust, POSIX-compliant logging utility for Python applications, supporting Python 2.7 and 3.x with optional Cython compilation for enhanced performance. It handles daily log rotation, automatic archiving (tar.gz for logs >7 days), removal (>30 days), privilege-aware paths (/var/log for root, ~/.app for users), and structured output with timestamps, PIDs, levels, and components. No external dependencies beyond the standard library; semantic version 1.1.1.

Features

  • Daily log files: <app>-YYYYMMDD.log with format [YYYY-MM-DD HH:MM:SS] pid:<PID> [<LEVEL>] @<COMPONENT> :] <MESSAGE>.
  • Rotation on date change; archive via tarfile.open("w:gz"); remove via os.remove().
  • Privilege detection via _Suroot: is_root() (os.geteuid()==0), can_sudo_without_password() (subprocess.Popen(["sudo", "-n", "true"])).
  • Lazy evaluation for paths (baseDir(), logDir()), debug (os.getenv("DEBUG")=="show"), and context (inPython() checks sys.executable).
  • Environment detection: Supports venv (inVenv()), pyenv (inPyenv(), pyenvVenv()), Conda/Anaconda/Miniconda (inConda(), condaPath()) for isolated paths (e.g., <env>/.app/<app>/log).
  • Byte/string handling with UTF-8 via strToByte()/byteToStr(); io.open(encoding='utf-8') for writes.
  • Console output: stdout for INFO/DEBUG, stderr for ERROR/CRITICAL/FATAL via print(..., file=sys.stderr).
  • Compatibility: __future__ imports, no f-strings (use .format()), sys.exc_info() for exceptions, DEVNULL shim.

Installation

Install via pip for system-wide or virtual environment use:

pip install ChronicleLogger

For isolated environments (recommended for development):

  • venv: python -m venv myenv && source myenv/bin/activate
  • pyenv: pyenv install 3.12.0 && pyenv shell 3.12.0
  • Conda/Miniconda/Anaconda: conda create -n myenv python=3.12 && conda activate myenv

ChronicleLogger automatically detects these environments and places logs/configs inside (e.g., myenv/.app/test-app/log for non-root).

For testing dependencies (optional):

pytest==4.6.11  # For Py2 compat
mock  # For Py2 unittest.mock fallback

Install: pip install -r requirements.txt.

Installation by Building from Source (Cython .so)

For performance-critical setups, build the Cython .so from source:

# Install build tools
pip install cython setuptools hatchling

# Build extensions in-place (compiles .pyx to .c and .so)
python setup.py build_ext --inplace

# Or full packaging: sdist and wheel
python setup.py sdist bdist_wheel

# Editable install (for development)
pip install -e .

Using pyproject.toml (hatchling backend):

[build-system]
requires = ["hatchling >= 1.18"]
build-backend = "hatchling.build"

[project]
name = "chroniclelogger"
version = "1.1.1"
description = "POSIX-compliant logger with rotation and privilege paths"
readme = "README.md"
requires-python = ">=2.7"
license = {text = "MIT"}
dependencies = []  # No external deps

Build: hatch build. Install wheel: pip install dist/chroniclelogger-1.1.1-py3-none-any.whl.

Demo Application: LoggedExample

LoggedExample is a simple, practical demonstration application that uses ChronicleLogger as its logging backend. It shows real-world usage patterns including:

  • Initialization with custom app name
  • Logging at different levels (INFO, DEBUG, ERROR)
  • Debug mode activation via environment variable
  • Integration with environment-aware path resolution
  • Basic command-line interface

PyPI URL: https://pypi.org/project/LoggedExample/

How to Install and Run LoggedExample

# Install the demo application (automatically pulls ChronicleLogger as dependency)
pip install LoggedExample

# Run the demo
LoggedExample --help

# Example with debug mode enabled
DEBUG=show LoggedExample run

This will create log files in your current environment's isolated path (e.g. ~/.app/logged-example/log/ or inside your venv/conda/pyenv environment), showing structured, timestamped logs with rotation and archiving in action.

Great for beginners who want to see ChronicleLogger in a complete, runnable application!

Project Structure

The project adopts a clean, maintainable layout for Cython utilities:

./
├── build/          # Build artifacts (bdist.linux-x86_64, lib/)
├── dist/           # Wheels/tar.gz (e.g., chroniclelogger-1.1.1-py3-none-any.whl)
├── docs/           # CHANGELOG.md, ChronicleLogger-spec.md, folder-structure.md, update-log.md
├── src/
│   ├── ChronicleLogger/  # Package: ChronicleLogger.py, Suroot.py, __init__.py (__version__="1.1.1")
│   ├── ChronicleLogger.pyx  # Cython source
│   ├── ChronicleLogger.c    # Compiled
│   └── setup.py             # Packaging
├── test/           # test_chronicle_logger.py, __pycache__/
├── .gitignore      # Ignore __pycache__/, .env, *.pyc, dist/, build/
├── README.md
├── pyproject.toml  # Optional, for hatchling
├── setup.py
└── requirements.txt  # Testing deps only

.gitignore example:

.env
__pycache__/
*.pyc
*.pyo
*.pyd
.Python
build/
dist/
*.egg-info/
.coverage
htmlcov/

Initialize Git: git init && git add . && git commit -m "Initial commit".

Usage

Import and instantiate:

from ChronicleLogger import ChronicleLogger

Default (privilege- and environment-aware paths, kebab-case in Python mode):

logger = ChronicleLogger(logname="TestApp")
print(logger.logName())  # "test-app" (in Python mode)
print(logger.logDir())   # E.g., "/home/user/miniconda3/envs/myenv/.app/test-app/log" if in Conda env

# Custom paths
logger = ChronicleLogger(logname="MyApp", logdir="/custom/logs", basedir="/opt/myapp")

# Log (auto-rotates, writes to file + console)
logger.log_message("Started", level="INFO", component="main")
logger.log_message("Error occurred", level="ERROR")  # To stderr

# Debug mode (set env DEBUG=show)
if logger.isDebug():
    logger.log_message("Debug info", level="DEBUG")

# Environment checks
print(logger.inVenv())     # True if in venv
print(logger.inPyenv())    # True if in pyenv
print(logger.inConda())    # True if in Conda

# Version
print(ChronicleLogger.class_version())  # "ChronicleLogger v1.1.1"

Path Examples (for "TestApp"):

  • In venv /path/to/venv: baseDir="/path/to/venv/.app/test-app", logDir="/path/to/venv/.app/test-app/log"
  • In pyenv venv /home/user/.pyenv/versions/3.12/envs/myenv: Similar with /.app/test-app
  • In Conda /home/user/miniconda3/envs/myenv: Similar
  • Fallback non-root: ~/.app/test-app/log
  • Root (no env): /var/log/test-app

Building and Cython

For performance: python setup.py build_ext --inplace.

build.sh example:

#!/bin/sh
python setup.py build_ext --inplace
python setup.py sdist bdist_wheel

Run: chmod +x build.sh && ./build.sh.

Testing

pip install -r requirements.txt
pytest test/ -v

Changelog

See docs/CHANGELOG.md for updates: v1.1.1 - Added environment support, Py2 shims, etc.

For issues: Ensure isolated env; run which python to confirm.

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

chroniclelogger-1.2.1.tar.gz (17.6 kB view details)

Uploaded Source

Built Distribution

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

chroniclelogger-1.2.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file chroniclelogger-1.2.1.tar.gz.

File metadata

  • Download URL: chroniclelogger-1.2.1.tar.gz
  • Upload date:
  • Size: 17.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for chroniclelogger-1.2.1.tar.gz
Algorithm Hash digest
SHA256 b28256d2c449a014867e1407fb5a47e7e4bfbf82b0387f9f93cb100a012091c3
MD5 ff77c474486b156f025e773b955d1c71
BLAKE2b-256 01d01006aa779d4b26c5d1390e109c5ea8173517e21cfd19947ba5034fd9ca26

See more details on using hashes here.

File details

Details for the file chroniclelogger-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for chroniclelogger-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 98d50d0a9fecf9c7362650a6e8f920be5a7c876676a717f9ea1db701430a482c
MD5 824ebe5bb4678548d944b2920d99917d
BLAKE2b-256 ebf7a3c671f7baf1b7bec63c912646028e412cd91bccb274da98efb301f4933a

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