custom-python-logger
A powerful and flexible Python logger with colored output, custom log levels, and advanced configuration options.
Easily integrate structured, readable, and context-rich logging into your Python projects for better debugging and monitoring.
🚀 Features
- ✅ Colored Output: Beautiful, readable logs in your terminal using
colorlog. - ✅ Custom Log Levels: Includes
STEP(for process steps) andEXCEPTION(for exception tracking) in addition to standard levels. - ✅ Flexible Output: Log to console, file, or both. Supports custom log file paths and automatic log directory creation.
- ✅ Contextual Logging: Add extra fields (like user, environment, etc.) to every log message.
- ✅ UTC Support: Optionally log timestamps in UTC for consistency across environments.
- ✅ Pretty Formatting: Built-in helpers for pretty-printing JSON and YAML data in logs.
- ✅ Short Path Display: Automatically trims log file paths to project-relative or
.venv-relative format for cleaner output. - ✅ Easy Integration: Simple API for getting a ready-to-use logger anywhere in your codebase.
📦 Installation
pip install custom-python-logger
🔧 Usage
Here's a quick example of how to use custom-python-logger in your project:
import logging
from custom_python_logger import build_logger, CustomLoggerAdapter
logger: CustomLoggerAdapter = build_logger(
project_name='Logger Project Test',
log_level=logging.DEBUG,
log_file=True,
)
logger.debug("This is a debug message.")
logger.info("This is an info message.")
logger.step("This is a step message.")
logger.warning("This is a warning message.")
try:
_ = 1 / 0
except ZeroDivisionError:
logger.exception("This is an exception message.")
logger.critical("This is a critical message.")
Advanced Usage
-
Log to a file:
from custom_python_logger import build_logger logger = build_logger(project_name='MyApp', log_file=True)
-
Use UTC timestamps:
from custom_python_logger import build_logger logger = build_logger(project_name='MyApp', log_file=True, utc=True)
-
Add extra context:
from custom_python_logger import build_logger logger = build_logger(project_name='MyApp', log_file=True, utc=True, extra={'user': 'alice'})
-
Pretty-print JSON or YAML:
from custom_python_logger import build_logger, json_pretty_format, yaml_pretty_format logger = build_logger(project_name='MyApp', utc=True, log_file=True) logger.info(json_pretty_format({'foo': 'bar'})) logger.info(yaml_pretty_format({'foo': 'bar'}))
-
Use an existing logger with a custom name:
from custom_python_logger import get_logger logger = get_logger('some-name') logger.debug("This is a debug message.") logger.info("This is an info message.") logger.step("This is a step message.")
-
Use a custom log format:
from custom_python_logger import build_logger, LOG_FORMAT_FILENAME, LOG_FORMAT_SHORTPATH # Default — shows project-relative or .venv-relative path: # 2026-05-18 | INFO | l.20 | my_app | my_project/app/main.py:42 | message logger = build_logger(project_name='MyApp', log_format=LOG_FORMAT_SHORTPATH) # Classic — shows filename only (no path): # 2026-05-18 | INFO | l.20 | my_app | main.py:42 | message logger = build_logger(project_name='MyApp', log_format=LOG_FORMAT_FILENAME)
🗂️ Short Path Display
By default, build_logger uses LOG_FORMAT_SHORTPATH, which trims the file path in every log line:
| Path type | Raw record.pathname |
Displayed as |
|---|---|---|
| Project file | /home/user/my_project/app/main.py |
my_project/app/main.py |
Dependency in .venv |
/home/user/my_project/.venv/lib/python3.13/site-packages/urllib3/pool.py |
.venv/lib/python3.13/site-packages/urllib3/pool.py |
| Unrecognised path | /tmp/some_script.py |
/tmp/some_script.py (full path) |
Setting your project name
The short-path logic uses the PROJECT_NAME environment variable to identify your project root.
Set it in your .env file (loaded automatically on import) or export it before running:
# .env
PROJECT_NAME=my_project
# or inline
PROJECT_NAME=my_project python main.py
Note:
custom-python-loggercallsload_dotenv()on import, which reads your.envfile automatically. If you setPROJECT_NAMEprogrammatically, do so before importingcustom_python_loggerto ensure it takes effect.
🤝 Contributing
If you have a helpful tool, pattern, or improvement to suggest:
Fork the repo
Create a new branch
Submit a pull request
I welcome additions that promote clean, productive, and maintainable development.
📄 License
MIT License — see LICENSE for details.
🙏 Thanks
Thanks for exploring this repository!
Happy coding!
Metadata
Release files for custom-python-logger 4.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| custom_python_logger-4.2.0.tar.gz | 9.3 kB | Without provenance | Details for custom_python_logger-4.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| custom_python_logger-4.2.0-py3-none-any.whl | Python 3 | none | any | Details for custom_python_logger-4.2.0-py3-none-any.whl |
Total release size: 16.7 kB
Release files / custom_python_logger-4.2.0.tar.gz
| Download URL | custom_python_logger-4.2.0.tar.gz |
|---|---|
| Size | 9.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
dc5e7434c92bd805e3cb77b2886e7ca3eb6ae910a6a6150b9587822ad8efeba4
|
|
BLAKE2b-256 checksum How to use checksums |
9d7d4cc6356a792bef219c178c31b8f01d4454086bd6b23875e871ce3fbe6524
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Release files / custom_python_logger-4.2.0-py3-none-any.whl
| Download URL | custom_python_logger-4.2.0-py3-none-any.whl |
|---|---|
| Size | 7.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
0768281ec0e1404df6025d4c2f1fcd1b81804fa1f595ba0353ae7e6a5aeeb240
|
|
BLAKE2b-256 checksum How to use checksums |
08bfa243731a8df4808c08df507c6e8716244e03e87b8a7da97004f2cb97bd9d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/7.0.0 CPython/3.13.16
|
Security for custom-python-logger 4.2.0
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.