Skip to main content

Advanced console logging with color support and minimal configuration interface

Project description

PyFlashLogger banner

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 scheme
  • color_scheme_bw.json: Black and white scheme
  • log_levels_en.json: English log level labels
  • log_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 message
    • log_info(message): Log info message
    • log_warning(message): Log warning message
    • log_error(message): Log error message
    • log_fatal(message): Log fatal error
    • log_custom0(message): Log custom level 0-9
  • Runtime Configuration:
    • set_output_format(format): Set output format for all channels
    • set_color_scheme(scheme): Set color scheme for all channels
    • add_channel(channel): Add a log channel with duplicate prevention
    • get_channel(selector): Get channel by ID, name, or instance

OutputFormat

  • HUMAN_READABLE: Default human-readable format
  • JSON_PRETTY: Pretty-printed JSON with indentation
  • JSON_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 string
    • save_to_json(path): Save configuration to JSON
    • set_level_color(level, foreground, background): Runtime color customization

LogChannelABC

  • Methods:
    • set_output_format(format): Set output format for this channel
    • is_loggable(level): Check if level is loggable
    • do_log(level, *args, **kwargs): Log a message

LogChannelConsole

  • Inherits from LogChannelABC
  • Methods:
    • set_color_scheme(scheme): Set color scheme for console output
    • set_output_format(format): Set output format with formatter updates
    • set_level_color(level, foreground, background): Runtime level color changes

LogLevel

  • Standard levels: DEBUG, INFO, WARNING, etc.
  • Custom levels: CUSTOM0 through CUSTOM9 (with fixed numeric assignments)
  • Methods: set_str_repr(level, label), load_str_reprs_from_json(path)

Releasing to PyPI

  1. Bump the package version in flashlogger/__init__.py (__version__).
  2. Clean old build artifacts:
    rm -rf build dist *.egg-info
    
  3. Build distributions:
    python -m build
    
  4. Upload to TestPyPI first (recommended):
    python -m twine upload --repository testpypi dist/*
    
  5. Upload to PyPI:
    python -m twine upload dist/*
    
  6. 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.

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes
  4. Add tests if applicable
  5. Submit a pull request

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

kingkybel_pyflashlogger-2.2.0.tar.gz (48.5 kB view details)

Uploaded Source

Built Distribution

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

kingkybel_pyflashlogger-2.2.0-py3-none-any.whl (42.7 kB view details)

Uploaded Python 3

File details

Details for the file kingkybel_pyflashlogger-2.2.0.tar.gz.

File metadata

  • Download URL: kingkybel_pyflashlogger-2.2.0.tar.gz
  • Upload date:
  • Size: 48.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for kingkybel_pyflashlogger-2.2.0.tar.gz
Algorithm Hash digest
SHA256 565684a67255e0d25ab818f83e046ac44f7ccb9abc15fa2cb1813cda143b94e9
MD5 44203f04100f76b91ad497a5730536a8
BLAKE2b-256 264606664dea7b348cc440bb8893da0b045a14fc72cbd8055eb3b99d10e53dc1

See more details on using hashes here.

File details

Details for the file kingkybel_pyflashlogger-2.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for kingkybel_pyflashlogger-2.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1fae532c17d84a11c443cd4f1dec04850bf8a60a51d34526ec12eea65949e548
MD5 82b32928828ff396ed6373f8f05ae505
BLAKE2b-256 15ac7b61aad6884e6284ab0b8dd722244756a11067e6bc1f3e381c2e173afa81

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