Skip to main content

AppLogging

Python Status PyPI - Version

A drop-in, singleton logging module for Python applications. Wraps Python's standard logging library with session tracking, timed log rotation, optional console output, and safe defaults — configured once at startup and accessible anywhere in your project via get_logger().


Installation

pip install AppLogging

PyPI: AppLogging


Philosophy

AppLogging is intentionally scoped. It is not a feature-complete wrapper around everything Python's logging module offers. It is designed for developers who want a consistent, reliable logger they can drop into any project with minimal configuration — and get out of the way.

If your use case requires fine-grained handler control, custom filters, or complex logging topologies, Python's logging module is the right tool. AppLogging is for everyone else.


Requirements


Overview

AppLogging is a singleton class, meaning only one logger instance exists for the lifetime of your application. You call setup_logger() once at your entry point to initialize and configure it — setup_logger() does not return a logger instance, it only initializes one. After that, any module can call get_logger(__name__) to retrieve a named child logger that inherits the root configuration.

Log files are written to a configurable directory, named by date, and rotated automatically. Each time your application starts, a session header is written to the log file so you can visually distinguish runs within the same day's file.


Quick Start

Call setup_logger() once, near the top of your entry point:

from AppLogging import AppLogging

AppLogging.setup_logger(name="my_app")

Then in any module, call get_logger() to retrieve a logger instance:

from AppLogging import AppLogging

logger = AppLogging.get_logger(__name__)

logger.debug("Detailed trace info")
logger.info("Application started")
logger.warning("Something looks off")
logger.error("Something went wrong")
logger.critical("Unrecoverable failure")

API Reference

AppLogging.setup_logger(...) — classmethod

Initializes the logger. Must be called before any other method. Raises RuntimeError if called after initialization — call reset() first if you need to reinitialize. Returns None.

Parameter Type Default Description
name str (required) Root logger name. Used as the log file name prefix and parent namespace.
file_level str "DEBUG" Minimum level written to the log file. Accepts: DEBUG, INFO, WARNING, ERROR, CRITICAL.
console_level str "INFO" Minimum level written to the console (if enabled). Same accepted values.
handlers str "FILE" Which handlers to activate. Accepts: "FILE", "CONSOLE", "BOTH".
dir_log str | Path ./logs Directory where log files are written. Created automatically if it doesn't exist.
backup_count_log int 15 Number of rotated log files to retain before deletion.
rotation_log str "midnight" When to rotate the log file. Accepts: "s", "m", "h", "d", "midnight", "w0"–"w6" (Monday–Sunday).
interval_log int 1 How many units of rotation_log between rotations. Ignored when rotation_log is "midnight".
format_file_log str See below logging-style format string for file output.
format_console_log str See below logging-style format string for console output.
format_date_log str "%Y-%m-%d %H:%M:%S" strftime-style format string for timestamps.
sanitized bool True Sanitizes log messages against log injection by escaping newline characters.
queue bool False Routes all log records through a multiprocessing.Queue and QueueListener. For multi-thread/process or high log volume

Default file format: %(asctime)s - %(levelname)-8s - %(name)s - %(message)s
Default console format: %(levelname)-8s - %(name)s - %(message)s


AppLogging.get_logger(name) — classmethod

Returns a logging.Logger instance. If name is provided and doesn't already start with the root logger name, it is automatically namespaced as root.name. If name is None, returns the root logger directly.

Raises RuntimeError if called before setup_logger().


AppLogging.set_levels(file_level, console_level) — classmethod

Adjusts the log levels on the active handlers without reinitializing. Accepts the same level strings as setup_logger(). Invalid values emit a UserWarning and fall back to defaults.


AppLogging.enable_console() — classmethod

Activates console output at the configured console_level. If a console handler is already attached, it restores it to the active level. If none exists, one is created and added.


AppLogging.disable_console() — classmethod

Removes the console handler entirely. To restore console output, call enable_console(), which will create and attach a new handler at the configured console_level.


AppLogging.reset() — classmethod

Closes and removes all handlers, then resets the singleton state. After calling this, setup_logger() can be called again with new configuration.


AppLogging.get_queue() — classmethod

Returns the active multiprocessing.Queue instance if queue=True was passed to setup_logger(), otherwise returns None. Use this to pass the queue into worker processes so they can attach their own QueueHandler and route records back through the listener.


Log Files

Log files are created in the configured directory using the naming pattern:

YYYY-MM-DD_<name>.log

Each application session writes a header block to the file:

==============================
    Session #3 - 14:22:07
------------------------------

This makes it easy to distinguish separate runs within the same day's log file without needing to parse timestamps.

Files are rotated on the configured schedule (midnight by default) and the configured number of backups is retained before older files are deleted.


Environment Variable Override

The file log level can be overridden at runtime via the LOGLEVEL environment variable:

LOGLEVEL=WARNING python main.py

This takes precedence over the file_level argument passed to setup_logger().


Input Validation

All parameters passed to setup_logger() are validated before being applied. If an invalid value is detected, a UserWarning is emitted via Python's warnings module and the corresponding default is used instead. This applies to:

  • Log level strings (must match a known level name)
  • Handler value (must be FILE, CONSOLE, or BOTH)
  • Log directory path (validated via pathvalidate)
  • Backup count (must be a positive integer)
  • Rotation and interval (rotation must be an accepted string; interval must be a positive integer)
  • Log format strings (validated by constructing a formatter against a dummy log record)
  • Date format strings (validated via strftime)

Because warnings.warn is used, warning behavior can be controlled programmatically via Python's warnings filter system if needed.


Notes

  • AppLogging is a singleton — setup_logger() can only be called once per process lifecycle without an intervening reset().
  • setup_logger() returns None — use get_logger() to retrieve a logger instance.
  • Child loggers retrieved via get_logger(__name__) are automatically namespaced under the root logger and inherit its handler configuration through Python's standard logger hierarchy.
  • get_logger() raises RuntimeError if called before setup_logger(). Initialize at your entry point before any module calls it.
  • disable_console() fully removes the console handler. enable_console() reconstructs and re-attaches it at the configured console_level.
  • When sanitized=True (default), newline characters in log messages are escaped before writing, preventing log injection from external or user-supplied data.
  • When queue=True, real handlers are owned by the QueueListener and detached from the root logger. Calling enable_console() or disable_console() will throw an intentional warning about not being supported.

Metadata

Release files for AppLogging 1.0.3

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

Source distribution (sdist)

Source distribution for AppLogging 1.0.3
File Size Uploaded Provenance Actions
applogging-1.0.3.tar.gz 9.3 kB Without provenance Details for applogging-1.0.3.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for AppLogging 1.0.3
File Interpreter ABI Platform Actions
applogging-1.0.3-py3-none-any.whl Python 3 none any Details for applogging-1.0.3-py3-none-any.whl

Total release size: 19.1 kB

Release files / applogging-1.0.3.tar.gz

Download URL applogging-1.0.3.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5903ecfee9ffeb1af8ec767c128a69e16058330c265635c2fddff1b5404e82d6
BLAKE2b-256 checksum
How to use checksums
59fd2d94b581a2133d5cce7500188b53f85340c508dc448ef4183b53552fc60a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Release files / applogging-1.0.3-py3-none-any.whl

Download URL applogging-1.0.3-py3-none-any.whl
Size 9.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dcd8fc95577c953e6d94539856fe808b74637084622d7dc200bbd5965ce1a12d
BLAKE2b-256 checksum
How to use checksums
650089c9595dd8bd126380e8653c99aca3ab4cb59ba83dc43b19b5650719f903
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.3

Security for AppLogging 1.0.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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