Skip to main content

Advanced logging configuration with support for multi-threading and multi-processing.Provides structured logging, context management, and automatic rotation.

Project description

使用示例

from advenced_logger import get_default_logger, exception_decorator

# Get the default logger
logger = get_default_logger()

@exception_decorator
def basic_logging_example():
    """Demonstrate basic logging functionality."""
    # Basic log levels
    logger.debug("Debug message - detailed information for debugging")
    logger.info("Info message - general information about program execution")
    logger.warning("Warning message - indicate a potential problem")
    logger.error("Error message - indicate a serious problem")
    logger.critical("Critical message - indicate a fatal error")

    # Structured logging
    logger.info(
        "User logged in",
        extra={
            "user_id": "12345",
            "ip_address": "192.168.1.1",
            "login_time": "2024-01-08 10:30:00"
        }
    )

    # Using context
    with logger.contextualize(request_id="abc-123"):
        logger.info("Processing request")
        logger.info("Request completed", extra={"duration_ms": 150})

    # Exception handling
    try:
        result = 1 / 0
    except Exception:
        logger.exception("An error occurred during calculation")

if __name__ == "__main__":
    basic_logging_example()

设计多线程和多进程的示例

from advenced_logger import setup_logger, exception_decorator
import threading
import multiprocessing
import time
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor

# Create custom logger with specific configuration
logger = setup_logger(
    log_dir="custom_logs",
    console_level="DEBUG",
    file_level="INFO",
    rotation="100 MB",
    retention="7 days"
).get_logger()

@exception_decorator
def worker_task(task_id: int, sleep_time: float = 0.1):
    """Example worker function for demonstrating parallel logging."""
    thread_name = threading.current_thread().name
    process_name = multiprocessing.current_process().name
    
    with logger.contextualize(task_id=task_id, thread=thread_name, process=process_name):
        logger.info(f"Starting task {task_id}")
        time.sleep(sleep_time)
        
        # Structured logging in parallel execution
        logger.info(
            "Task progress", 
            extra={
                "progress": "50%",
                "memory_usage": "100MB",
                "duration": sleep_time
            }
        )
        
        # Simulate occasional errors
        if task_id % 5 == 0:
            raise ValueError(f"Simulated error in task {task_id}")
            
        logger.info(f"Completed task {task_id}")
        return task_id

@exception_decorator
def threaded_execution_example():
    """Demonstrate thread-safe logging with thread pool."""
    logger.info("Starting threaded execution example")
    
    with ThreadPoolExecutor(max_workers=3) as executor:
        futures = [executor.submit(worker_task, i) for i in range(5)]
        for future in futures:
            try:
                result = future.result()
                logger.debug(f"Thread task result: {result}")
            except Exception:
                logger.exception("Thread task failed")

@exception_decorator
def multiprocess_execution_example():
    """Demonstrate process-safe logging with process pool."""
    logger.info("Starting multiprocess execution example")
    
    with ProcessPoolExecutor(max_workers=3) as executor:
        futures = [executor.submit(worker_task, i) for i in range(5)]
        for future in futures:
            try:
                result = future.result()
                logger.debug(f"Process task result: {result}")
            except Exception:
                logger.exception("Process task failed")

if __name__ == "__main__":
    # Configure logging context for the entire application
    with logger.contextualize(app_name="AdvancedExample", environment="development"):
        logger.info("Application starting")
        
        # Run threading example
        threaded_execution_example()
        
        # Run multiprocessing example
        multiprocess_execution_example()
        
        logger.info("Application finished")

自定义的实例

from advanced_logger import setup_logger
import sys

def email_alert(message):
    """Mock function to simulate sending email alerts."""
    print(f"Email Alert: {message}")

# Example of custom configuration with additional handlers
custom_logger = setup_logger(
    log_dir="enterprise_logs",
    app_log_filename="app_{time:YYYY-MM-DD}.log",
    console_level="INFO",
    file_level="DEBUG",
    rotation="1 day",
    retention="30 days",
    compression="zip",
    extra_handlers={
        # Custom handler for critical errors
        "critical_handler": {
            "sink": "critical_errors.log",
            "level": "CRITICAL",
            "rotation": "100 MB",
            "format": "{time} - {message}",
            "serialize": True  # JSON format
        },
        # Custom handler for email notifications
        "email_handler": {
            "sink": email_alert,
            "level": "ERROR",
            "format": "Alert: {message}",
            "backtrace": False
        }
    }
).get_logger()

def demonstrate_custom_logging():
    """Show custom logging configuration features."""
    # Basic logging
    custom_logger.info("Application initialized with custom configuration")
    
    # Structured logging with custom format
    custom_logger.info("System status check", extra={
        "memory_usage": "2.5GB",
        "cpu_usage": "75%",
        "disk_space": "120GB",
        "active_users": 150
    })
    
    # Demonstrate error handling with email notification
    try:
        raise RuntimeError("Critical system error detected")
    except Exception:
        custom_logger.exception("System error occurred")

if __name__ == "__main__":
    demonstrate_custom_logging()

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

advenced_logger-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

advenced_logger-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file advenced_logger-0.1.1.tar.gz.

File metadata

  • Download URL: advenced_logger-0.1.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for advenced_logger-0.1.1.tar.gz
Algorithm Hash digest
SHA256 c1b489518911661a3521f9b13ab4dee10e43298dba38007a009409888a99d789
MD5 cd3ebe97acf69b86928a24640691baa0
BLAKE2b-256 63e4f86fdf4d9db7287fc0277ebddfe390d2a31cc1b86f7a1d7a491f4899c892

See more details on using hashes here.

File details

Details for the file advenced_logger-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for advenced_logger-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 06cfcb999b852ee44d898ec021035afb07a0ec0e635f8e737ae9ee33c86586ea
MD5 813dc22aca596591530a544ce00bbe46
BLAKE2b-256 af5698aded140d4bef10883ea7e51c9cf057d939267496dcf4ce4cec8974905e

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