Skip to main content

A custom logger with color support and additional features.

Project description

PyPI version Python Development Status Maintenance PyPI License


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) and EXCEPTION (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-logger calls load_dotenv() on import, which reads your .env file automatically. If you set PROJECT_NAME programmatically, do so before importing custom_python_logger to 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!

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

custom_python_logger-4.0.1.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

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

custom_python_logger-4.0.1-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file custom_python_logger-4.0.1.tar.gz.

File metadata

  • Download URL: custom_python_logger-4.0.1.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for custom_python_logger-4.0.1.tar.gz
Algorithm Hash digest
SHA256 239dcd9b43b260c4ac25bb9fe63e2b9cedd92082e9ee8db991f6641e8a29002e
MD5 b283d1bf2c06ab01f3d3c768d4748a6b
BLAKE2b-256 076d4704a2b9ae8335b022cff8095a427e0cfbdfd6e01b2907b18fe53f87edb7

See more details on using hashes here.

File details

Details for the file custom_python_logger-4.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for custom_python_logger-4.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c62c718ade9abc4e2ec09e89445703ceab655f33610bdbd40454ed36189fd87b
MD5 23b8c18d8a84472b139b322c906897fa
BLAKE2b-256 483ca9328a9740c1f5a5514f078c8107e28ce8dfb72f519236682c5e568fc871

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