A simple, zero-dependency lazy-initialization logging utility
Project description
lfp-logging
A simple, zero-dependency logging utility for Python that provides lazy-initialization and automatic configuration.
Features
- Lazy Initialization: Logging is only configured when the first log message is actually handled. It uses a patching mechanism that stays out of the way until a log is emitted.
- Automatic Name Discovery: Automatically determines logger names based on the caller's class, module, or file name.
- Smart Default Handlers:
INFOmessages are sent tostderr(along with all other levels) by default.- Detailed formatting including timestamps, levels, and line numbers.
- ANSI Colors: Automatic color support for terminals, with overrides for popular IDEs (VSCode, PyCharm) and CI environments.
- Explicit Override Support: If you call
logging.basicConfig()yourself,lfp-loggingwill automatically back off and let your configuration take priority. - Flexible Configuration: Supports configuration via environment variables.
- Multi-platform Support: Supports macOS (ARM/x64), Linux (ARM/x64), and Windows (x64/ARM).
Installation
You can install lfp-logging directly from GitHub using pip:
pip install git+https://github.com/regbo/lfp-logging-py.git
Or add it to your pyproject.toml dependencies:
dependencies = [
"lfp_logging @ git+https://github.com/regbo/lfp-logging-py.git"
]
Quick Start
from lfp_logging import logger
# The logger name is automatically discovered as the class name "MyService"
class MyService:
def __init__(self):
self.log = logger()
def do_something(self):
self.log.info("Starting task...")
self.log.warning("Something might be wrong.")
# You can specify one or more potential names.
# The first valid name (non-empty, not "__main__") will be used.
log = logger(None, "__main__", "my_app")
log.info("Hello World!") # Uses "my_app"
Configuration
The logging level and formats can be controlled using environment variables.
Environment Variables
LOG_LEVEL: Set the global log level. Accepts names (e.g.,DEBUG,INFO) or numeric values (e.g.,10,20).LOG_FORMAT: Custom log format string (standard Python logging format). (Default:%(asctime)s %(levelname)s %(name)s:%(lineno)d %(message)s)LOG_FORMAT_DATE: Custom date format (default:%Y-%m-%d %H:%M:%S).LOG_FORMAT_COLOR: Global ANSI color code for all levels (e.g.,\x1b[32mfor green).LOG_FORMAT_COLOR_<LEVEL>: Level-specific ANSI color code (e.g.,LOG_FORMAT_COLOR_DEBUG).LOG_CONFIG_LAZY: Defer logging configuration until the first log message is emitted (default:false). Set totrue,1,yes, oronto enable. When set tofalse, logging is configured immediately whenlogger()is called.
Automatic Back-off
lfp-logging is designed to be non-intrusive:
- Pre-existing Config: If you call
logging.basicConfig()or otherwise configure the root logger before anylfp-loggingactivity, it will detect this and skip its own configuration. - Post-initialization Override: If you call
logging.basicConfig()afterlfp-logginghas already configured itself,lfp-loggingwill automatically remove its default handler to allow your new configuration to take full effect.
System Arguments
The --log-level argument is no longer supported directly by the core configuration, but can be implemented by the user by setting the LOG_LEVEL environment variable before the first log call.
Development
This project uses uv for dependency management and pytest for testing.
Running Tests
uv run pytest
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file lfp_logging-0.0.8.tar.gz.
File metadata
- Download URL: lfp_logging-0.0.8.tar.gz
- Upload date:
- Size: 7.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2b23e602d7b5a5fb948b9322d2ca66d62c931261c8a0de633eb630a3d9bbb148
|
|
| MD5 |
d7c821fa330a68c3ca0f070bc8ef6dd7
|
|
| BLAKE2b-256 |
83bd4515c17f107c43e95f686516c35ab5636afdc52955e0e08c2c24b015e44c
|
Provenance
The following attestation bundles were made for lfp_logging-0.0.8.tar.gz:
Publisher:
publish.yml on regbo/lfp-logging-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lfp_logging-0.0.8.tar.gz -
Subject digest:
2b23e602d7b5a5fb948b9322d2ca66d62c931261c8a0de633eb630a3d9bbb148 - Sigstore transparency entry: 1050364620
- Sigstore integration time:
-
Permalink:
regbo/lfp-logging-py@201966a547a313091ccf48058ab6ba15535ea6bc -
Branch / Tag:
refs/tags/v0.0.8 - Owner: https://github.com/regbo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@201966a547a313091ccf48058ab6ba15535ea6bc -
Trigger Event:
push
-
Statement type:
File details
Details for the file lfp_logging-0.0.8-py3-none-any.whl.
File metadata
- Download URL: lfp_logging-0.0.8-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60866706a2c4f3231181fe324fd6cc97702d2bd5dc320659f99ef6fa7b469bb1
|
|
| MD5 |
f81b5c772382e9d191157ba08c7a39a4
|
|
| BLAKE2b-256 |
60829bbaa029a8768d6f2279b34da65929dc9443caff985b6a5fcc619d04a153
|
Provenance
The following attestation bundles were made for lfp_logging-0.0.8-py3-none-any.whl:
Publisher:
publish.yml on regbo/lfp-logging-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
lfp_logging-0.0.8-py3-none-any.whl -
Subject digest:
60866706a2c4f3231181fe324fd6cc97702d2bd5dc320659f99ef6fa7b469bb1 - Sigstore transparency entry: 1050364623
- Sigstore integration time:
-
Permalink:
regbo/lfp-logging-py@201966a547a313091ccf48058ab6ba15535ea6bc -
Branch / Tag:
refs/tags/v0.0.8 - Owner: https://github.com/regbo
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@201966a547a313091ccf48058ab6ba15535ea6bc -
Trigger Event:
push
-
Statement type: