Python logger which prints colored output and in optional a file will be generated
Find a file
2026-02-24 19:48:20 +01:00
colored_logger add python files and documentation 2026-02-24 19:27:05 +01:00
docs fix problems and update documentation 2026-02-24 19:48:20 +01:00
tests fix problems and update documentation 2026-02-24 19:48:20 +01:00
.gitignore update documentation and .gitignore 2026-02-24 19:28:11 +01:00
CHANGELOG.md add python files and documentation 2026-02-24 19:27:05 +01:00
LICENSE Initial commit 2026-02-23 17:49:30 +00:00
pyproject.toml add python files and documentation 2026-02-24 19:27:05 +01:00
README.md update documentation and .gitignore 2026-02-24 19:28:11 +01:00

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