Skip to main content

Lightweight Python logger with log levels, rotation, colorization, async support, and working-directory-scoped file utilities

Project description

ntnlog

Coverage

Lightweight Python logger with timestamped file output, caller stack tracing, and working-directory-scoped file utilities. Next-To-Nothing setup — just import and log.

Features

  • Log levels: TRACE, DEBUG, INFO, WARNING, ERROR, CRITICAL with per-instance and global thresholds
  • Caller stack tracing: Automatically records where in your code a log came from
  • Timestamped file output: Logs written to daily files with no configuration required
  • Log rotation: Size-based rotation with configurable backup count
  • Console colorization: ANSI color output per log level, customizable per instance
  • Exception capturing: exception() attaches the active traceback automatically
  • Async support: alog() and aexception() for async/await code
  • File utilities: Safe, working-directory-scoped file operations with error handling
  • Thread-safe: File writes are protected by a per-instance lock

Installation

pip install ntnlog

Quick Start

from ntnlog import Logger, Level

app_log = Logger()

app_log("Application started")
app_log("Important message", console_message="")      # also prints to stdout
app_log("Something suspicious", Level.WARNING)         # WARNING level, no console

# Exception capturing — attaches the active traceback automatically
try:
    do_something()
except Exception:
    app_log.exception("Something went wrong")

# Async logging
await app_log.alog("Async message")
await app_log.aexception("Async error")

# Enable detailed call-stack tracing
app_log.enable_log_tracing(True)
app_log("Traced message")

# Named loggers — useful when multiple instances log to the same file
app_logger = Logger(name="app")
worker_logger = Logger(name="worker")
# Output: [2026-05-13 15:46:34][INFO][app][main.py:12] message
#         [2026-05-13 15:46:34][INFO][worker][worker.py:8] message

# Level filtering — string or enum, both work
app_log = Logger(level=Level.WARNING)
app_log = Logger(level="warning")

# Log rotation
app_log = Logger(max_bytes=5_000_000, backup_count=3)

# Console colorization
app_log = Logger(colorize=True)

# Custom log directory
app_log = Logger(log_dir="my_logs")

# Project-aware frame filtering
app_log = Logger(project_dir="/path/to/project")

Configuration

from ntnlog.ntn_config import (
    GLOBAL_LOGGING_ENABLED,
    GLOBAL_LOG_TRACING_ENABLED,
    GLOBAL_LOG_LEVEL,
    GLOBAL_MAX_BYTES,
    GLOBAL_BACKUP_COUNT,
    GLOBAL_LOG_COLORS,
)
Global Default Description
GLOBAL_LOGGING_ENABLED True Master on/off switch for all loggers
GLOBAL_LOG_TRACING_ENABLED True Master tracing switch
GLOBAL_LOG_LEVEL Level.INFO Default minimum level when instance level is None
GLOBAL_MAX_BYTES 10_000_000 Default rotation threshold (10 MB)
GLOBAL_BACKUP_COUNT 1 Default number of backup files kept on rotation
GLOBAL_LOG_COLORS see config ANSI color code per level

Per-instance controls:

app_log.enable_logging(False)      # disable this logger
app_log.enable_log_tracing(True)   # enable tracing for this logger

Modules

Logger (ntn_logging.py)

Main logging class. Writes timestamped, level-tagged entries to ./logs/<date>_logging.txt and optionally traces the full call stack to show exactly which file and line triggered the log.

File Utilities (ntn_file_utils.py)

Path and file verification scoped to the working directory:

  • file_verify_path(base, directory) — confirms a directory exists within the working dir
  • file_verify_file(base, filename, operator) — validates a file for read/write/execute

Utils (ntn_utils.py)

Working-directory-scoped path helpers with a clean exception-based API:

  • ntnlog.utils.get_working_dir() — returns the current working directory
  • ntnlog.utils.resolve_path(path, must_exist=False) — resolves a path within the working directory, raises ValueError if it escapes

Configuration (ntn_config.py)

Global flags and defaults controlling logging behaviour across all logger instances.

Development

pip install -e ".[dev]"
pytest
pytest -v
pytest tests/test_ntn_logging.py

License

MIT

Author

Nathan T Nguyen

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

ntnlog-0.5.1.tar.gz (22.0 kB view details)

Uploaded Source

Built Distribution

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

ntnlog-0.5.1-py3-none-any.whl (12.7 kB view details)

Uploaded Python 3

File details

Details for the file ntnlog-0.5.1.tar.gz.

File metadata

  • Download URL: ntnlog-0.5.1.tar.gz
  • Upload date:
  • Size: 22.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ntnlog-0.5.1.tar.gz
Algorithm Hash digest
SHA256 60d6c9db03e3f3c209ce78023417893c9e2cf266aa69876ce19a8e1d416cd064
MD5 f0d5c6f1fc0b9bf85b0e150b591a7625
BLAKE2b-256 ac93f796572da6a5dfc0ba0229e16831e36cd48a70e8fd726d4c519e2daafb0d

See more details on using hashes here.

Provenance

The following attestation bundles were made for ntnlog-0.5.1.tar.gz:

Publisher: publish.yml on NhatNguyen5/ntnlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ntnlog-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: ntnlog-0.5.1-py3-none-any.whl
  • Upload date:
  • Size: 12.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for ntnlog-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 963c8c57310dbb7265832e6cedc8ffdc3d01a9947fecafb6ec0740293a82a2a7
MD5 54d97fbdecd8d112aa9dbc5fc4ab0d8c
BLAKE2b-256 b6b73e88a2247a0d0151f7ab25493587710b4e6ec5a06ceff3c2391d8750e62c

See more details on using hashes here.

Provenance

The following attestation bundles were made for ntnlog-0.5.1-py3-none-any.whl:

Publisher: publish.yml on NhatNguyen5/ntnlog

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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