Python logger which prints colored output and in optional a file will be generated
- Python 100%
| colored_logger | ||
| docs | ||
| tests | ||
| .gitignore | ||
| CHANGELOG.md | ||
| LICENSE | ||
| pyproject.toml | ||
| README.md | ||
ColoredLogger
A lightweight Python logger with ANSI-colored console output and optional plain-text file logging. No third-party dependencies — only the Python standard library.
[[TOC]]
How to create a Python package
Detailed description to create a python package
Color Scheme
| Level | Color |
|---|---|
DEBUG |
Purple |
INFO |
Light Blue |
WARNING |
Yellow |
ERROR |
Red |
CRITICAL |
Orange |
File output is always written without color codes.
Log Format
2024-01-15 12:34:56,789 | INFO | Server started on port 8080 - (main.py:42)
%(asctime)s | %(levelname)8s | %(message)s - (%(filename)s:%(lineno)d)
Installation
From source
git clone https://github.com/yourname/ColoredLogger.git
cd ColoredLogger
pip install .
Editable install (development)
pip install -e .
Usage
Basic — console only
from colored_logger import get_logger
log = get_logger(__name__)
log.debug("Connecting to database …")
log.info("Server started on port 8080")
log.warning("Config file not found, using defaults")
log.error("Failed to open file: data.csv")
log.critical("Out of memory — shutting down")
With file output
from colored_logger import get_logger
log = get_logger(__name__, log_file="app.log")
log.info("This appears in color on the console …")
log.info("… and as plain text in app.log")
Custom log level
import logging
from colored_logger import get_logger
# Only WARNING and above
log = get_logger(__name__, level=logging.WARNING)
API Reference
get_logger(name, level, log_file)
| Parameter | Type | Default | Description |
|---|---|---|---|
name |
str |
__name__ |
Logger name |
level |
int |
logging.DEBUG |
Minimum log level |
log_file |
str | None |
None |
Path to log file (plain text, no colors) |
ColoredFormatter
A logging.Formatter subclass that wraps each log line with the appropriate ANSI escape code. Can be used directly with any StreamHandler if you want to integrate it into an existing logging setup.
Colors
A namespace class exposing the raw ANSI escape strings: RESET, LIGHT_BLUE, YELLOW, RED, PURPLE, ORANGE.
Running the Tests
# From the project root
python -m pytest