Advanced console logging with color support and minimal configuration interface
Project description
kingkybel-pyflashlogger
Advanced console logging with color support and minimal configuration interface.
Features
- 🎨 Color-Coded Logging: Automatic ANSI color coding for different log levels with flexible configuration
- 🏷️ Custom Log Levels: Support for custom log levels with configurable labels and fixed numeric assignments
- 🔄 Minimal Interface: Simple API for logging without complex setup
- 🌈 Special Color Support: Configurable colors for timestamps, process IDs, brackets, operators, etc.
- 📋 Field Ordering: Base class configurable field ordering (level, timestamp, pid, tid, message) for all log channels
- 💾 JSON Configuration: Save and load color/label configurations
- 🔗 Multiple Channels: Console and file logging implementations with unified configuration
- 🧪 Format Flexibility: Different formats for standard logs, commands, and std streams
- 📤 Output Formats: Multiple output formats (Human readable, JSON pretty, JSON lines) available to all channels with runtime switching
- 🔧 Runtime Configuration: Dynamically set color schemes and output formats at runtime for individual channels or all channels
- 📊 Structured JSON Output: Automatic parsing of message arguments into JSON with support for direct object serialization
- 🎯 Enhanced ColorScheme API: Flexible color retrieval supporting LogLevel enums, Field enums, and string keys
- ✅ Comprehensive Testing: Full test coverage including all new functionality
- 🚨 Effortless Error Handling: Functions to log a message and exit the application with a single call
Installation
pip install kingkybel-pyflashlogger
Or from source:
git clone https://github.com/kingkybel/FlashLogger.git
cd FlashLogger
pip install -e .
Tools
FlashLogger includes interactive configuration tools:
Color & Label Configurator (tools/color_configurator.py)
Interactive tool for customizing logging colors and labels with live preview:
cd tools
python color_configurator.py # Use default configs
python color_configurator.py colors.json labels.json # Load custom configs
# Interactive commands:
# 4 RED _ DIM # Change WARNING level colors
# load labels DE # Switch to German labels
# s # Save configuration
# q # Quit
Features:
- Visual preview of all colors and levels
- Tab completion for color names
- Load/save configurations
- Support for predefined schemes (COLOR, BW, PLAIN)
- International label support (EN, DE)
Logger Testing Tool (tools/try_out_logger.py)
Sample script demonstrating FlashLogger capabilities with runtime configuration:
python tools/try_out_logger.py
Shows examples of:
- Runtime color scheme changes
- Output format switching
- Structured JSON output
- Custom log levels
Quick Start
from flashlogger import FlashLogger, ColorScheme, LogLevel
# Basic usage
logger = FlashLogger()
logger.log_info("This is an info message")
logger.log_warning("This is a warning")
# With custom colors
scheme = ColorScheme.default_color_scheme()
console_logger = FlashLogger(console=scheme)
console_logger.log_error("Colorized error message")
# Custom log levels
logger.log_custom0("Custom level message")
Error Handling
The flashlogger.error module provides a simple way to log a message and then exit the application. This can be useful for handling critical errors that should terminate the program.
from flashlogger.error import fatal
def do_something_critical():
# ...
fatal("Something went terribly wrong!")
# This will log a fatal error message and then exit with a non-zero status code.
do_something_critical()
Advanced Usage
Runtime Color Scheme Configuration
from flashlogger import ColorScheme, LogChannelConsole
# Create logger with initial scheme
logger = FlashLogger()
channel = logger.get_channel(LogChannelConsole.__name__)
# Dynamically change color schemes at runtime
channel.set_color_scheme(ColorScheme.Default.BLACK_AND_WHITE)
logger.set_color_scheme(ColorScheme.Default.COLOR) # Affects all channels
# Use custom color schemes
custom_scheme = ColorScheme(color_scheme_json="my_colors.json")
channel.set_color_scheme(custom_scheme)
Runtime Output Format Configuration
from flashlogger import FlashLogger, OutputFormat
logger = FlashLogger()
# Set output format for all channels
logger.set_output_format(OutputFormat.JSON_PRETTY)
# Set format for individual channels
channel = logger.get_channel('LogChannelConsole')
channel.set_output_format('JSON_LINES') # Override for this channel
# JSON output now includes structured arguments
logger.log_info("Simple message", {"complex": "data"}, arg1="value")
# Output: {"timestamp": "...", "level": "info", "message": "Simple message",
# "message0": {"complex": "data"}, "arg1": "value"}
Structured JSON Output
FlashLogger automatically structures logging arguments for JSON output:
# Dict as first argument gets merged directly (no "message" wrapper)
logger.log_custom0({"user_id": 123, "action": "login"})
# JSON: {"user_id": 123, "action": "login", "level": "custom0", ...}
# Multiple args get indexed as message0, message1, etc.
logger.log_info("Operation", completed=True, duration=1.5)
# JSON: {"message": "Operation", "message0": true, "duration": 1.5, "level": "info", ...}
Enhanced ColorScheme API
from flashlogger import ColorScheme, LogLevel
from flashlogger.color_scheme import Field
scheme = ColorScheme(ColorScheme.Default.COLOR)
# Flexible color retrieval methods
color_str = scheme.get("warning") # String key
color_enum = scheme.get(LogLevel.WARNING) # LogLevel enum
field_color = scheme.get(Field.TIMESTAMP) # Field enum
# With style and inverse options
bright_color = scheme.get("error", style=Style.BRIGHT)
inverse_color = scheme.get("debug", inverse=True)
Color Configuration
from flashlogger import ColorScheme
# Load predefined schemes
color_scheme = ColorScheme(ColorScheme.Default.COLOR)
bw_scheme = ColorScheme(ColorScheme.Default.BLACK_AND_WHITE)
# Create custom scheme from JSON
custom_scheme = ColorScheme(color_scheme_json="my_colors.json")
# Runtime color customization
scheme.set_level_color(LogLevel.WARNING, foreground="RED", background="YELLOW")
Field Ordering
# Customize log field display order
scheme.field_order = ["level", "timestamp", "message"] # Omit pid/tid
logger = FlashLogger(console=scheme)
# Output: [WARNING] [2025-10-26 00:00:00.000] This is a message
Custom Labels
from flashlogger import LogLevel
# Define custom labels
LogLevel.set_str_repr(LogLevel.custom0, "NETWORK_IO")
LogLevel.set_str_repr(LogLevel.custom1, "DB_QUERY")
logger = FlashLogger()
logger.log_custom0("Network I/O operation") # Shows as "NETWORK_IO"
Log Levels
- DEBUG: Debugging information
- INFO: General information
- WARNING: Warning conditions
- ERROR: Error conditions
- FATAL: Fatal errors
- CRITICAL: Critical conditions
- COMMAND: Command execution
- COMMAND_OUTPUT: Command stdout capture
- COMMAND_STDERR: Command stderr capture
- CUSTOM0-9: Custom user-defined levels
Configuration Files
FlashLogger includes default configuration files for color schemes and log level labels:
color_scheme_color.json: Full color schemecolor_scheme_bw.json: Black and white schemelog_levels_en.json: English log level labelslog_levels_de.json: German log level labels
Channels
Console Channel
from flashlogger import LogChannelConsole
channel = LogChannelConsole(color_scheme=my_scheme, minimum_log_level="WARNING")
channel.do_log("ERROR", "This error will be logged")
File Channel
from flashlogger import LogChannelFile
channel = LogChannelFile(filename="app.log")
channel.do_log("INFO", "This goes to file")
Custom Channels
Extend LogChannelABC for custom logging destinations:
from flashlogger import LogChannelABC
class MyChannel(LogChannelABC):
def do_log(self, log_level, *args, **kwargs):
# Your custom logging logic
pass
API Reference
FlashLogger
- Logging Methods:
log_debug(message): Log debug messagelog_info(message): Log info messagelog_warning(message): Log warning messagelog_error(message): Log error messagelog_fatal(message): Log fatal errorlog_custom0(message): Log custom level 0-9
- Runtime Configuration:
set_output_format(format): Set output format for all channelsset_color_scheme(scheme): Set color scheme for all channelsadd_channel(channel): Add a log channel with duplicate preventionget_channel(selector): Get channel by ID, name, or instance
OutputFormat
HUMAN_READABLE: Default human-readable formatJSON_PRETTY: Pretty-printed JSON with indentationJSON_LINES: Compact single-line JSON
ColorScheme
- Constructor:
ColorScheme(default_scheme_or_path) - Methods:
get(level, inverse=False, style=None): Get colors for LogLevel, Field, or stringsave_to_json(path): Save configuration to JSONset_level_color(level, foreground, background): Runtime color customization
LogChannelABC
- Methods:
set_output_format(format): Set output format for this channelis_loggable(level): Check if level is loggabledo_log(level, *args, **kwargs): Log a message
LogChannelConsole
- Inherits from LogChannelABC
- Methods:
set_color_scheme(scheme): Set color scheme for console outputset_output_format(format): Set output format with formatter updatesset_level_color(level, foreground, background): Runtime level color changes
LogLevel
- Standard levels:
DEBUG,INFO,WARNING, etc. - Custom levels:
CUSTOM0throughCUSTOM9(with fixed numeric assignments) - Methods:
set_str_repr(level, label),load_str_reprs_from_json(path)
Releasing to PyPI
- Bump the package version in
flashlogger/__init__.py(__version__). - Clean old build artifacts:
rm -rf build dist *.egg-info
- Build distributions:
python -m build
- Upload to TestPyPI first (recommended):
python -m twine upload --repository testpypi dist/*
- Upload to PyPI:
python -m twine upload dist/*
- Verify install:
pip install -U kingkybel-pyflashlogger
For token-based authentication with Twine:
- Username:
__token__ - Password: your PyPI API token (starts with
pypi-) - Or set env vars:
export TWINE_USERNAME=__token__ export TWINE_PASSWORD='pypi-...'
Quick release script:
./release.sh # clean + build + TestPyPI + PyPI
./release.sh --testpypi-only # clean + build + TestPyPI only
./release.sh --skip-upload # clean + build only
./release.sh --yes # non-interactive (CI-friendly)
License
GPLv2 - See the LICENSE file for details.
Contributing
Contributions welcome! Please open issues for bugs or feature requests.
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
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 kingkybel_pyflashlogger-2.3.0.tar.gz.
File metadata
- Download URL: kingkybel_pyflashlogger-2.3.0.tar.gz
- Upload date:
- Size: 43.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
69dc461ae195e20fc4341e90300fc510af059f1fad62ffec6d2a87a96f337e65
|
|
| MD5 |
f5226dbd3cf6cf8e08d411a93d387677
|
|
| BLAKE2b-256 |
7b65bdf89d7ed22eee125b2ab1b79eb1e2324c95d287a892bd91c9df69194be9
|
File details
Details for the file kingkybel_pyflashlogger-2.3.0-py3-none-any.whl.
File metadata
- Download URL: kingkybel_pyflashlogger-2.3.0-py3-none-any.whl
- Upload date:
- Size: 31.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
94d69694d4b216c2f3cd2fbc34b151a5e1613c135ac55ca4656b2f4e53f43d97
|
|
| MD5 |
a6fe867711c1e6e092ca9311ae9273cf
|
|
| BLAKE2b-256 |
28d1367bf2fd6413ca7819e8f14d5cb60d4f26d1bf68c0144e70c135c65152d5
|