Skip to main content

A flexible logging system with colorized output and file rotation support.

Project description

ExLog

Python License: MIT

A lightweight, colorful, customizable Python logging utility with support for terminal output, file rotation, and asynchronous logging; built for the Ex Projects, and YOURs too!

Table of Contents

  1. Overview
  2. Installation
  3. Usage Examples
  4. Feature Demonstrations
  5. Configuration
  6. Available Colors
  7. Contributing
  8. License

Overview

ExLog is a flexible logging utility that supports both console and file-based logging with:

  • Customizable log levels (debug, info, warning, error, critical).
  • Colored output for better readability.
  • File rotation by time (daily, hourly) and size.
  • Asynchronous logging with minimal overhead.

Installation

Ensure that termcolor is installed for colored console output:

pip install termcolor

Alternatively, you can include it in your requirements.txt.

To clone the repository:

git clone https://github.com/onedavidwilliams/ExLog.git
cd ExLog

Minimum Python Version

ExLog requires Python 3.7 or higher. for asyncio.run() to work correctly


How It Works:

  • When you instantiate ExLog, you can set the minimum log_level. Only messages with a numeric value greater than or equal to the set level will be printed.
  • You can specify the log_level using a string (e.g., "debug", "info", etc.) or number (1, 2, etc.).

Usage Examples:

1. Log Level: info (1)

logger = ExLog(log_level="info")  # Same as log_level=1
logger.dprint("Info message", level="info")  # Printed
logger.dprint("Debug message", level=2)  # Not printed - Same as "debug"

2. Log Level: debug (2)

logger = ExLog(log_level="debug")  # Same as log_level=2
logger.dprint("Debug message", level="debug")  # Printed
logger.dprint("Info message", level="info")  # Not Printed

3. Log Level: warning (3)

logger = ExLog(log_level="warning")  # Same as log_level=3
logger.dprint("Warning message", level="warning")  # Printed
logger.dprint("Info message", level="info")  # Not Printed
logger.dprint("Debug message", level="debug")  # Not printed

4. Basic Console Logging

from ExLog import ExLog

logger = ExLog()  # Default log level: info, console-only
logger.dprint("Hello, World!", level="info")

Output:

[03:15:20 PM] [INFO] Hello, World!

5. Logging to File with Daily Rotation

logger = ExLog(log_dir="my_logs", rotation="daily")
logger.dprint("Logging to file and terminal.", level="debug")
  • Logs are saved in the my_logs/ directory.
  • New files are created daily.

6. Async Logging

import asyncio
from ExLog import ExLog

async def main():
    logger = ExLog(log_dir="my_logs", rotation="hourly")
    await logger.adprint("Async log message", level="info")

asyncio.run(main())
  • Async-friendly logging for concurrent applications.

Feature Demonstrations

1. Size-Based Log File Rotation

logger = ExLog(log_dir="my_logs", max_file_size=1024 * 5)  # 5 KB max size
for i in range(100):
    logger.dprint(f"Message {i}", level="info")
  • Automatically creates new log files when the size exceeds 5 KB.

2. Custom Color Formatting

logger = ExLog(custom_colors={
    "info": {"color": ExLog.color.magenta},
    "warning": {"color": ExLog.color.blue, "background_color": ExLog.bg_color.yellow}
})
logger.dprint("Custom color for info.", level="info")
logger.dprint("Custom color for warning.", level="warning")

3. Critical Log with Program Exit

def critical_exit_example(error=None):
    logger = ExLog()
    error = error if error else "No error specified"
    logger.dprint(f"Critical failure! Exiting program...\nError: {error}", level="critical")
    exit(1)

critical_exit_example("Test")
  • Prints a critical log message and the error if one is passed and exits the program.

4. Different Log Levels in Loop

log_levels = ["debug", "info", "warning", "error", "critical"]
logger = ExLog(log_dir="my_logs")

for i, level in enumerate(log_levels):
    logger.dprint(f"This is a {level.upper()} message", level=level)
  • Cycles through all log levels to demonstrate their output.

Configuration

Initialization Parameters

Parameter Type Default Description
log_level int 1 Minimum log level to display (1 for "info", 2 for "debug", etc.).
log_dir str None Directory for log files. If None, logs only print to the console.
log_file_prefix str "log" Prefix for log filenames.
rotation str "daily" Log rotation type: "daily", "hourly", or "none".
max_file_size int None Maximum log file size (in bytes) before rotating to a new file.
custom_colors dict None Dictionary for custom foreground and background colors.

Available Colors

You can set colors using ExLog.color (foreground) and ExLog.bg_color (background):

Foreground Colors (ExLog.color) Background Colors (ExLog.bg_color)
black on_black
red on_red
green on_green
yellow on_yellow
blue on_blue
magenta on_magenta
cyan on_cyan
white on_white
grey on_grey

Contributing

  1. Fork the repository.
  2. Create a feature branch:
    git checkout -b my-feature-branch
    
  3. Commit your changes:
    git commit -m "Added new feature"
    
  4. Push your branch:
    git push origin my-feature-branch
    
  5. Open 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

exlog-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

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

exlog-0.1.0-py3-none-any.whl (5.9 kB view details)

Uploaded Python 3

File details

Details for the file exlog-0.1.0.tar.gz.

File metadata

  • Download URL: exlog-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for exlog-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8bb91bcaebb61d31c52e1e570e34a24a3c9dfabfa1405ea3a9c651f8cef82890
MD5 bb54e02c0a24a84ad5d75e037fb02881
BLAKE2b-256 0e73fb07b472e6ebe73e33dcdc57672f7190b58e7e86e131fdd8e71b9c876fc8

See more details on using hashes here.

File details

Details for the file exlog-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: exlog-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 5.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.10.12

File hashes

Hashes for exlog-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e86c1c2d60f4e242d0c94cb641e68c70e3cb9667e26d4f0aee0aa1a1956d7de2
MD5 0e037fa06c0a1e22d83de03fd15e93e6
BLAKE2b-256 8f58df0ef4891d28785be07b3ca605acaf6e8035fcedd3f8c19c9124fce9281a

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