Skip to main content

Advanced configurable logger for Python with file rotation and CLI support

Project description

DivineGift Logger

A flexible and configurable logging package for Python applications with file rotation and command-line configuration support.

Features

  • ✅ Easy configuration via code or command-line arguments
  • ✅ File rotation with customizable intervals
  • ✅ Both file and console logging support
  • ✅ Thread-safe implementation
  • ✅ Type hints for better IDE support
  • ✅ Exception logging with stack traces

Installation

pip install dglog

Basic Usage

Simple console logging

from dglog import get_logger

logger = get_logger()
logger.info("Application started")
logger.warning("This is a warning")

Configure logger programmatically

from dglog import configure_logger

# Configure with daily file rotation
configure_logger(
    log_level="DEBUG",
    log_name="app.log",
    log_dir="./logs",
    when="D",
    backup_count=14
)

log_info("This will be logged to file")

Command-line configuration

Run your script with logging parameters:

python your_script.py --log_level DEBUG --log_name app.log --log_dir ./logs

The logger will automatically pick up these parameters.

API Reference

Core Functions

Function Description
get_logger() Returns the global logger instance
configure_logger(**kwargs) Configures the global logger
log_debug(*args, separator=" ") Logs debug message
log_info(*args, separator=" ") Logs info message
log_warning(*args, separator=" ") Logs warning message
log_error(*args, separator=" ") Logs error message
log_critical(*args, separator=" ") Logs critical message
log_exception(*args, separator=" ") Logs exception with stack trace

Logger Class Methods

logger = get_logger()

# Configuration
logger.configure_logger(
    log_level="INFO",          # Logging level
    log_name=None,             # File name (None for console)
    log_dir="./logs",          # Log directory
    when="midnight",          # Rotation interval
    interval=1,               # Rotation frequency
    backup_count=7,           # Number of backups
    formatter=None            # Custom formatter
)

# Logging methods
logger.debug("Debug message")
logger.info("Information")
logger.warning("Warning message")
logger.error("Error occurred")
logger.critical("Critical failure")
logger.exception("Exception occurred")  # With stack trace

Advanced Examples

Custom Formatter

from logging import Formatter

custom_format = Formatter('%(name)s - %(levelname)s - %(message)s')
configure_logger(log_level="DEBUG", formatter=custom_format)

Exception Handling

try:
    1 / 0
except Exception:
    log_exception("Division failed")
    # Output includes the stack trace

Multiple Arguments

log_info("User", "logged in", "from IP", "192.168.1.1", separator=" | ")
# Output: User | logged in | from IP | 192.168.1.1

Dynamic Configuration

import sys
from dglog import get_logger

logger = get_logger()

if '--debug' in sys.argv:
    logger.configure_logger(log_level="DEBUG")
else:
    logger.configure_logger(log_level="INFO")

Configuration Parameters

Parameter Default Description
log_level "INFO" Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)
log_name None Log file name (None for console logging)
log_dir "./logs" Directory for log files
when "midnight" Rotation interval ('S', 'M', 'H', 'D', 'midnight', etc.)
interval 1 Rotation frequency
backup_count 7 Number of backup files to keep
formatter Default format Custom log message formatter

Best Practices

  1. Early Configuration: Configure the logger at the start of your application
  2. Proper Log Levels: Use appropriate levels for different situations
  3. Structured Data: Include context in your log messages
  4. Exception Handling: Always use log_exception() for errors
  5. Rotation Policy: Choose appropriate rotation settings based on your needs

License

MIT License - Free for commercial and personal use.

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

dglog-1.0.0.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

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

dglog-1.0.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file dglog-1.0.0.tar.gz.

File metadata

  • Download URL: dglog-1.0.0.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dglog-1.0.0.tar.gz
Algorithm Hash digest
SHA256 64e6d9fa26b5960cee410e108353dedc2b2b029a3e705be17bc56c59ee1d4653
MD5 b2b5fd5b3a9b087a4ffb000b385ddf1c
BLAKE2b-256 bae7c5f3479428d5a8f578a62b4348ba4b2d80ed6c64d6d86bced5eb404e5043

See more details on using hashes here.

File details

Details for the file dglog-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: dglog-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for dglog-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8bfdecb3f54dd016eab70c11e9fcb6e836b07b6d261b58793019f3b7e3071b0e
MD5 4409200c4635794a4f0bc0f1238d31eb
BLAKE2b-256 1f37b7a9fc0cd872adb6a88b8dece7d2302622ad9bb26a2e98dd7ec6c4fd8507

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