Skip to main content

A simple wrapper for python logger that also selectively tee logs to stdout

Project description

Tee_Logger

A Python logger that writes to a dated log file and optionally mirrors output to stdout with ANSI colors. Supports in-place compression, automatic log archival, and smart caller line attribution.

Features

  • Tee logging — file-only (info, error, …) or stdout+file (teeprint, teeerror, teeok, …)
  • Dated layout{programName}_log/YYYY-MM-DD/{programName}_YYYY-MM-DD_HH-MM-SS.log
  • Caller attribution — log lines include abbreviated file:line of the direct caller
  • Compression — write .gz/.bz2/.xz/.zst logs directly, or tar.xz archive old day-folders
  • Maintenance — auto-compress and delete logs by age on startup

Installation

pip install tee-logger

Optional runtime dependency: python-dateutil (improves log-folder date parsing during cleanup).

Requires Python 3.6+. In-place zstd compression requires Python 3.14+ with zstd support in the build; otherwise it falls back to xz.

Quick start

from Tee_Logger import teeLogger

tl = teeLogger(systemLogFileDir='.', programName='MyApp')
tl.info('file only')
tl.teeprint('stdout and file')
tl.teeerror('error to both')

Compressed logs:

tl = teeLogger(
    programName='MyApp',
    in_place_compression='zstd',  # or 'gzip', 'bz2', 'xz'
    compression_level=3,
)

Disable file logging:

tl = teeLogger(noLog=True)

Configuration

Parameter Default Description
systemLogFileDir '.' Root directory for log folders; falls back to /tmp on failure
programName caller file name Logger name and log file prefix
suppressPrintout not stdout.isatty() Hide console output from tee* methods
callerStackDepth -1 Auto-resolve direct caller; use 0+ for manual stack offset
in_place_compression None gzip, bz2, xz/lzma, zstd, or True (xz)
binary_mode True Binary append mode for log files
compressLogAfterMonths 2 Archive day-folders older than N months (0 = off)
deleteLogAfterYears 2 Delete day-folders older than N years (0 = off)
noLog False Disable file logging
fileDescriptorLength 15 Width of file:line in log records
encoding 'utf-8' Log file encoding
collapse_single_day_logs auto One file per day when compressing in-place

Per-call override: tl.info('msg', callerStackDepth=3).

Log layout

MyApp_log/
├── MyApp_latest.log          # symlink to active log (Unix)
└── 2025-02-10/
    └── MyApp_2025-02-10_01-33-26.log

Log line format:

2025-02-10 01:33:26,623 [INFO    ] [MyApp:42      ] message text

Caller stack depth

With the default callerStackDepth=-1, teeLogger skips its own frames and records the direct caller — your code or your wrapper, not internal Tee_Logger methods.

def my_wrapper(msg):
    tl.info(msg)          # log shows this line

def business():
    my_wrapper('hello')   # log shows my_wrapper, not business

Use a non-negative callerStackDepth or a per-call override for advanced tuning.

Log maintenance

On initialization, cleanup_old_logs() scans {programName}_log/ for YYYY-MM-DD folders:

  1. Delete folders older than deleteLogAfterYears
  2. Compress folders older than compressLogAfterMonths to .tar.xz

Compression uses system tar+xz when available, otherwise Python's tarfile. Work runs in a background process only when there are eligible folders.

API reference

Full docstrings and examples live in the module:

python -c "import Tee_Logger; help(Tee_Logger.teeLogger)"

Public helpers: abbreviate_filename, pretty_format_table, printWithColor, getCallerInfo, teeLogger.

Testing

Run doctests embedded in the module:

python -m doctest src/Tee_Logger.py -v
python src/Tee_Logger.py --test

Run unit tests:

python -m unittest discover -s tests -v

License

GPL-3.0-or-later

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

tee_logger-6.39.tar.gz (14.6 kB view details)

Uploaded Source

Built Distribution

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

tee_logger-6.39-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file tee_logger-6.39.tar.gz.

File metadata

  • Download URL: tee_logger-6.39.tar.gz
  • Upload date:
  • Size: 14.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tee_logger-6.39.tar.gz
Algorithm Hash digest
SHA256 2162a84579fa2d6ff0d21a1359efd71862360037a87f1fa763f952141b3b0291
MD5 74e5ccf6a4aa2995e5c02f89b8285cac
BLAKE2b-256 3e459bc3ef7034cb09139a0afe8d5aa66a6221a2e6777bfaa463ec998850c8fa

See more details on using hashes here.

File details

Details for the file tee_logger-6.39-py3-none-any.whl.

File metadata

  • Download URL: tee_logger-6.39-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.23 {"installer":{"name":"uv","version":"0.11.23","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Debian GNU/Linux","version":"13","id":"trixie","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for tee_logger-6.39-py3-none-any.whl
Algorithm Hash digest
SHA256 8fb4ccf761b2ed5f2c0cb5b1335cee2610af0697a0a5603d8605475e19e1bfc0
MD5 69108fe0ae6b259f52bef0a239e29a79
BLAKE2b-256 a61e9b01af866dcb77a44c557d7763b8cadac55819ded34e4c751a331b0358bc

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