Skip to main content

A custom logging package with multiline formatting

Project description

THCustomLogger

A feature-rich Python logging package that extends the standard logging functionality with colored output, multiline formatting, and Git integration.

Features

  • 🎨 Colored console output
  • 📝 Smart multiline message formatting
  • 🔄 Automatic log rotation
  • 🔍 Git integration (commit hash and tag information)
  • 🔒 Thread-safe logging
  • ⚙️ Configurable logging settings
  • 📂 File and console handlers

Table of Contents

Installation

pip install THCustomLogger

Quick Start

Basic Configuration

from THCustomLogger import LoggerFactory
# Get a logger instance
logger = LoggerFactory.get_logger(__name__)

# Basic logging
logger.info("Hello, World!")
logger.debug("Debug message") 
logger.warning("Warning message") 
logger.error("Error message")

# Multiline logging with custom formatting
logger.info("Multiple\nline\nmessage")

# Git information
logger.info(f"Current commit: {logger.get_commit_hash()}") 
logger.info(f"Latest tag: {logger.get_latest_tag()}")

# Logging with break lines
logger.info("Message with break line", extra={'msg_break': '*'})

Output

2025-04-29 11:00:00.625 | Line: 186 logger_setup.<module>             | INFO    : Hello, World!
2025-04-29 11:00:00.625 | Line: 188 logger_setup.<module>             | WARNING : Warning message
2025-04-29 11:00:00.625 | Line: 189 logger_setup.<module>             | ERROR   : Error message
2025-04-29 11:00:00.625 | Line: 192 logger_setup.<module>             | INFO    : Multiple
                                                                                  line
                                                                                  message
2025-04-29 11:00:00.632 | Line: 195 logger_setup.<module>             | INFO    : Current commit: afba168c52d65a621139c3b3e072a1fd991b26bd
2025-04-29 11:00:00.639 | Line: 107 logger_setup.get_latest_tag       | ERROR   : Error getting latest tag: fatal: No names found, cannot describe anything.

2025-04-29 11:00:00.640 | Line: 196 logger_setup.<module>             | INFO    : Latest tag: unknown
2025-04-29 11:00:00.640 | Line: 199 logger_setup.<module>             | INFO    : Message with break line
**********************************************************************************

2025-04-29 11:05:17.340 | Line: 172 logger_setup.example              | ERROR   : division by zero
Traceback (most recent call last):
  File "/Users/tylerhaunreiter/Desktop/Python/CustomLogger/src/THCustomLogger/logger_setup.py", line 170, in example
    1 / 0
    ~~^~~
ZeroDivisionError: division by zero

Configuration

You can configure the logger using the LoggerFactory.configure() method:

from THCustomLogger import LoggerFactory
LoggerFactory.configure(console_enabled=True, 
                        file_enabled=True, 
                        log_level="INFO")

Configuration Options

Option Description Default
console_enabled Enable console output True
file_enabled Enable file output True
log_level Logging level "INFO"
rotation_when Log rotation interval type (S/M/H/D/W) "D"
rotation_interval Number of intervals before rotation 1
backup_count Number of backup files to keep 7
encoding Log file encoding "utf-8"

Features in Detail

Colored Output

Console output is automatically colored based on log level:

  • DEBUG: Green
  • INFO: Light White
  • WARNING: Light Yellow
  • ERROR: Light Red
  • CRITICAL: Bold Light Purple

Multiline Formatting

Messages with multiple lines are automatically formatted with proper indentation:

2025-04-29 11:00:00.622 | Line: 165 logger_setup.main                 | INFO    : Commit hash: afba168c52d65a621139c3b3e072a1fd991b26bd
                                                                                  Latest tag:unknown

Git Integration

Built-in methods to get Git information:

# Get current commit hash
commit_hash = logger.get_commit_hash()
# Get latest tag
latest_tag = logger.get_latest_tag()

Advanced Usage

Custom Formatting

logger.info("Message with custom break", extra={'msg_break': '*'}) 
logger.info("No indent message\nSecond line", extra={'no_indent': True})
2025-04-29 11:05:17.343 | Line: 185 logger_setup.<module>             | INFO    : Message with custom break
**********************************************************************************
2025-04-29 11:05:17.343 | Line: 186 logger_setup.<module>             | INFO    : No indent message
Second line

Thread Safety

The logger is thread-safe and can be used in multi-threaded applications:

python import threading def worker(): logger = LoggerFactory.get_logger(name) logger.info("Working in thread") threads = [threading.Thread(target=worker) for _ in range(3)] for thread in threads: thread.start()

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Author

Tyler Haunreiter

Support

For support, please open an issue on the GitHub repository.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

thcustomlogger-0.2.2.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

thcustomlogger-0.2.2-py3-none-any.whl (7.7 kB view details)

Uploaded Python 3

File details

Details for the file thcustomlogger-0.2.2.tar.gz.

File metadata

  • Download URL: thcustomlogger-0.2.2.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for thcustomlogger-0.2.2.tar.gz
Algorithm Hash digest
SHA256 39911e74b722de9f730754e51a1c50ae882625698be8ef85b372c3e3924a6d0f
MD5 dc30a10a893270afdddbf0df4eb87f19
BLAKE2b-256 735624b447e1790f3ac2d29a686792dcebbeed2a0bf2d01a8086cab43c8604ae

See more details on using hashes here.

File details

Details for the file thcustomlogger-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: thcustomlogger-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 7.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for thcustomlogger-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 62fb184e14bc55e271b52d8c5e3a445710e38fe46b2dae20443ae9c255f6d292
MD5 427bbad4cc395c0c9673e2e0450fab0b
BLAKE2b-256 fc89eea1ec37b77f32e5a78dd569272d52e39d4903478823552c55a088fb103a

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