Skip to main content

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

Project description

Tee_Logger

This Python module provides a simple, customizable logger that can direct output to both the console and a log file. It includes utilities for formatting file names, colorizing text output, and performing log maintenance tasks like archiving and deletion of old logs.

Table of Contents


Installation

pip install tee-logger

Dependencies:

  • dateutil (optional)
  • base64, math, functools, subprocess, tarfile, shutil (standard library modules)

Overview

The code offers a logging class called teeLogger that can:

  • Print log messages with or without colors.
  • Write log entries to a file with date-based folder organization.
  • Automatically compress and delete logs based on specified criteria.
  • Provide caller information in log messages (filename, line number).

By default, teeLogger outputs to the console and a timestamped log file. You can disable file logging or console printing as needed.


Key Components

bcolors

A helper class providing ANSI escape codes for colored terminal output.

class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKCYAN = '\033[96m'
    OKGREEN = '\033[92m'
    warning = '\033[93m'
    critical = '\033[91m'
    info = '\033[0m'
    debug = '\033[0m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    UNDERLINE = '\033[4m'

abbreviate_filename

A function that shortens file names with optional advanced transformations (e.g., using base64 or scientific notation for line numbers). It is used internally to display a concise “filename:lineNumber” in log messages.

printWithColor

A helper function that prints a plain text string to the console, wrapped in the appropriate ANSI color codes based on a level:

  • info: bcolors.info
  • debug: bcolors.debug
  • warning: bcolors.warning
  • error: bcolors.warning
  • critical: bcolors.critical
  • ok or okgreen: bcolors.OKGREEN
  • okblue: bcolors.OKBLUE
  • okcyan: bcolors.OKCYAN
  • default: bcolors.info

teeLogger

Primary logger class with the following notable parameters:

  • systemLogFileDir: Directory in which log folders are stored. If /dev/null, logging to a file is suppressed. Note: if the specifiled dir cannot be accessed, will then try to use /tmp
  • programName: Name associated with the logs. Defaults to the caller’s filename.
  • compressLogAfterMonths: Number of months after which logs are automatically compressed.
  • deleteLogAfterYears: Number of years after which logs are deleted.
  • suppressPrintout: If set to True, console printing is suppressed, but logs still appear in the file.
  • fileDescriptorLength: Maximum title span for formatted file name and line number in logs.
  • noLog: If True, no file logging occurs.
  • callerStackDepth: The stack depth for tracing back the caller file path and line number. Default to 2.
  • in_place_compression: Write logs directly as compressed files (gzip, bz2, xz/lzma, zstd).
  • compression_level: Compression tuning value passed to the selected backend (for zstd, this maps to compression.zstd.open(..., level=...)).

Methods include:

  • teeok(msg), ok(msg), info(msg), error(msg), teeerror(msg): Log a message at various levels, optionally printing to console.
  • printTable(data): Nicely formats 2D data or a dictionary as a table before logging.
  • teelog(msg, level): General-purpose logger for any level (e.g. "warning", "critical").
  • cleanup_old_logs(): Called automatically to compress or remove old logs.

Usage Examples

from Tee_Logger import teeLogger

# Create a logger instance
tl = teeLogger(systemLogFileDir='.', programName='MyApp', compressLogAfterMonths=2, deleteLogAfterYears=1)

# Log messages
tl.info("This is an info message. ( only log to file )")
tl.teeerror("This is an error reported to both stdout and file")
tl.teeok("Operation successful.")
tl.teeprint("Normal messages.")
level='critical'
tl.teelog('Tee Log with level as a variable',level)
level='warning'
tl.log('Log with variable level',level)

Terminal output:

Log file: <cwd>/MyApp_log/2025-02-10/MyApp_2025-02-10_01-33-26.log
This is an error reported to both stdout and file
Operation successful.
Normal messages.
Tee Log with level as a variable

example_console_output

Logger file:

2025-02-10 01:33:26,622 [INFO    ] [Tee_Logger:357 ] Starting MyApp at 2025-02-10_01-33-26
2025-02-10 01:33:26,623 [INFO    ] [Tee_Logger:357 ] This is an info message. ( only log to file )
2025-02-10 01:33:26,624 [ERROR   ] [Tee_Logger:362 ] This is an error reported to both stdout and file
2025-02-10 01:33:26,625 [INFO    ] [Tee_Logger:340 ] Operation successful.
2025-02-10 01:33:26,626 [INFO    ] [Tee_Logger:354 ] Normal messages.
2025-02-10 01:33:26,627 [CRITICAL] [Tee_Logger:370 ] Tee Log with level as a variable
2025-02-10 01:33:26,628 [WARNING ] [Tee_Logger:374 ] Log with variable level

Note: by default, the caller trace back level is 2, which usually will be the line calling for log.

Log Maintenance

All logs are stored in programName_log/YYYY-MM-DD directories. Upon initialization, the logger permanently runs cleanup_old_logs() to:

  • Compress logs older than compressLogAfterMonths.
  • Delete logs older than deleteLogAfterYears.

Compression uses tar with xz if available, otherwise uses Python’s built-in tarfile library. The cleanup runs asynchronously using a ProcessPoolExecutor.

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.38.tar.gz (12.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.38-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: tee_logger-6.38.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","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.38.tar.gz
Algorithm Hash digest
SHA256 29f29898ab0bd24735f55b83cd4e4894002e73e438d028f7c48cf1aca76c41b9
MD5 c58a13aaabe6645f8f949bfd56821061
BLAKE2b-256 08c1105b2ede22c34e185520f8a1bbb5aaa648bbd4c7e55238f3f9572db01764

See more details on using hashes here.

File details

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

File metadata

  • Download URL: tee_logger-6.38-py3-none-any.whl
  • Upload date:
  • Size: 10.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Fedora Linux","version":"43","id":"","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.38-py3-none-any.whl
Algorithm Hash digest
SHA256 6b6248b824b1f3ecd2c0a3c503ef9aba7e8938ac1b0ff4065abcbb9b3dd3de87
MD5 7da410a12deb95d010aefd7b97648a79
BLAKE2b-256 9c52406cdace6ee998b3b3570a68e1b28fbcdc2f1a69bddc7e0176ba299908ad

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