A customizable terminal logger with color schemes, grouping, and file output
Project description
beauty-log
A lightweight, customizable Python logger with color support, tree-based message formatting, grouping, and file output.
Installation
pip install beauty-log
Requirements
- Python 3.12+
- colorama
Quick Start
from beautylog import Logger
logger = Logger(
tree=["{time}", "{level}", "{message}"],
)
logger.console("Application started")
logger.console("Something went wrong", level="ERROR")
Output:
12:00:01 INFO Application started
12:00:01 ERROR Something went wrong
Usage
Basic Setup
from beautylog import Logger
logger = Logger(
tree=["{time}", "{level}", "{message}"], # defines the output format
)
Log Levels
| Level | Color |
|---|---|
DEBUG |
Cyan |
INFO |
Blue |
SUCCESS |
Green |
WARNING |
Yellow |
ERROR |
Red |
CRITICAL |
Magenta |
logger.console("Loaded config", level="DEBUG")
logger.console("Server started", level="SUCCESS")
logger.console("Disk space low", level="WARNING")
logger.console("Connection failed", level="ERROR")
logger.console("System failure", level="CRITICAL")
Custom Color Scheme
Override the default colors or add custom levels entirely:
from colorama import Fore
logger = Logger(
tree=["{time}", "{level}", "{message}"],
levelColorScheme={
"INFO": Fore.WHITE,
"SUCCESS": Fore.GREEN,
"DB": Fore.CYAN, # custom level
"AUTH": Fore.MAGENTA, # custom level
}
)
logger.console("Query executed", level="DB")
logger.console("User logged in", level="AUTH")
Tree Formatting
The tree parameter defines the structure of each log line. Each element is a segment that supports built-in placeholders:
| Placeholder | Value |
|---|---|
{message} |
The log message |
{time} |
Current time (HH:MM:SS) |
{level} |
Colored level label |
logger = Logger(tree=["[{time}]", "[{level}]", ">>", "{message}"])
# [12:00:01] [INFO] >> Server is running
Variables
Pass custom variables to be substituted anywhere in the tree:
logger = Logger(
tree=["{time}", "[{module}]", "{level}", "{message}"],
variables={"{module}": "database"}
)
logger.console("Connected successfully", level="SUCCESS")
# 12:00:01 [database] SUCCESS Connected successfully
Grouping
Use setGroupLevel() to indent related log messages and visually group them:
logger.console("Starting app", level="INFO")
logger.setGroupLevel(1)
logger.console("Loading config", level="DEBUG")
logger.console("Connecting to DB", level="DEBUG")
logger.setGroupLevel(2)
logger.console("Running migrations", level="DEBUG")
logger.setGroupLevel(1)
logger.console("Ready", level="SUCCESS")
logger.setGroupLevel(0)
logger.console("App started", level="SUCCESS")
Output:
12:00:01 INFO Starting app
12:00:01 DEBUG Loading config
12:00:01 DEBUG Connecting to DB
12:00:01 DEBUG Running migrations
12:00:01 SUCCESS Ready
12:00:01 SUCCESS App started
Saving to File
logger.save("Server started", level="INFO", logFile="app.log")
ANSI color codes are automatically stripped from file output.
log()
Log to both the terminal and a file in one call:
logger.log("User signed up", level="SUCCESS", logFile="app.log")
Timer
Measure and log execution time using the context manager:
with logger.timer("Database query"):
results = db.execute(query)
# logs: Database query took 42.30ms
# Automatically escalate to WARNING if a threshold is exceeded
with logger.timer("API call", warnAfter=200):
response = requests.get(url)
# logs as WARNING if it takes over 200ms
API Reference
Logger(tree, levelColorScheme, variables)
| Parameter | Type | Default | Description |
|---|---|---|---|
tree |
list |
None |
Output format segments |
levelColorScheme |
dict |
Built-in colors | Map of level names to colorama colors |
variables |
dict |
None |
Custom substitution variables |
console(message, level, useBuiltIns)
Print a formatted message to the terminal.
save(message, level, logFile, useBuiltIns)
Write a message to a log file (colors stripped automatically).
log(message, level, logFile)
Print to terminal and write to file.
setGroupLevel(groupLevel)
Set the current indent depth. Accepts any non-negative integer. Raises ValueError for negative values.
timer(label, level, warnAfter)
Context manager that measures execution time and logs the result.
License
This project is licensed under the GNU General Public License v3.0 — see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
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 beauty_log-0.1.0.tar.gz.
File metadata
- Download URL: beauty_log-0.1.0.tar.gz
- Upload date:
- Size: 32.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2f0607e2505121c8b14411c4b272fb27a9311283239bb7a89e23cfe16d42bca
|
|
| MD5 |
18f468777943d94358949159236722d6
|
|
| BLAKE2b-256 |
31a1103ff0261ca30e5c156627433de69b28e2728db03d17ec48f1956e26a57d
|
Provenance
The following attestation bundles were made for beauty_log-0.1.0.tar.gz:
Publisher:
python-publish.yml on feketefh/beauty-log
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
beauty_log-0.1.0.tar.gz -
Subject digest:
b2f0607e2505121c8b14411c4b272fb27a9311283239bb7a89e23cfe16d42bca - Sigstore transparency entry: 1274979876
- Sigstore integration time:
-
Permalink:
feketefh/beauty-log@75f4369e1e84e32fac8122fe8423ef4161aceffe -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/feketefh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@75f4369e1e84e32fac8122fe8423ef4161aceffe -
Trigger Event:
release
-
Statement type:
File details
Details for the file beauty_log-0.1.0-py3-none-any.whl.
File metadata
- Download URL: beauty_log-0.1.0-py3-none-any.whl
- Upload date:
- Size: 29.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b0fb5bf89dfaacc8bc06915804c8f54c39f954e05508c7b0e499734f034c6a7d
|
|
| MD5 |
55351e2532dd654f3e6391f7024fc655
|
|
| BLAKE2b-256 |
d9b6b5e166dbc7c57ae482e066bfe1ddd0bc0c6438d5df2b23a009007c2b8356
|
Provenance
The following attestation bundles were made for beauty_log-0.1.0-py3-none-any.whl:
Publisher:
python-publish.yml on feketefh/beauty-log
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
beauty_log-0.1.0-py3-none-any.whl -
Subject digest:
b0fb5bf89dfaacc8bc06915804c8f54c39f954e05508c7b0e499734f034c6a7d - Sigstore transparency entry: 1274979904
- Sigstore integration time:
-
Permalink:
feketefh/beauty-log@75f4369e1e84e32fac8122fe8423ef4161aceffe -
Branch / Tag:
refs/tags/v0.1.0 - Owner: https://github.com/feketefh
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@75f4369e1e84e32fac8122fe8423ef4161aceffe -
Trigger Event:
release
-
Statement type: