Claude Code for Loguru: Modern Python Logging — Claude Skills 360 Blog
Blog / AI / Claude Code for Loguru: Modern Python Logging
AI

Claude Code for Loguru: Modern Python Logging

Published: December 1, 2027
Read time: 5 min read
By: Claude Skills 360

Loguru is a Python logging library that makes logging simple and powerful with zero configuration. pip install loguru. from loguru import logger. Basic: logger.info("Server started on port {port}", port=8080). Levels: logger.debug/info/warning/error/critical("msg"). Exception: logger.exception("DB failed") — auto-captures stack. File sink: logger.add("app.log", rotation="10 MB", retention="30 days", compression="gz"). Daily rotation: logger.add("logs/{time:YYYY-MM-DD}.log", rotation="00:00"). JSON logs: logger.add("logs/json.log", serialize=True). Format: logger.add(sys.stderr, format="{time:HH:mm:ss} | {level:<8} | {name}:{line} | {message}"). Bind context: log = logger.bind(request_id="abc", user_id=42), log.info("Processing"). Opt: logger.opt(lazy=True).debug("Heavy: {x}", x=lambda: expensive()). logger.opt(depth=1).info("Called from caller"). Catch decorator: @logger.catch — wraps function, logs exceptions. Context: with logger.catch(): risky_code(). Filter: logger.add("errors.log", level="ERROR"). Filter fn: logger.add(sink, filter=lambda r: "DB" in r["name"]). Remove: logger.remove() — removes default stderr sink. Intercept stdlib: subclass logging.Handler, call logger.opt(depth=6).log(record.levelname, record.getMessage()). enqueue: logger.add("app.log", enqueue=True) — thread-safe, async-safe. Colorize: logger.add(sys.stderr, colorize=True). Claude Code generates Loguru structured logging setups, JSON log pipelines, and exception capture handlers.

CLAUDE.md for Loguru

## Loguru Stack
- Version: loguru >= 0.7
- Import: from loguru import logger — singleton, ready to use
- Sinks: logger.add(path/sink, level, format, rotation, retention, serialize)
- Context: logger.bind(key=value) — returns scoped logger with fields
- Exceptions: @logger.catch | with logger.catch() | logger.exception()
- JSON: logger.add(path, serialize=True) for structured log output
- Intercept: subclass logging.Handler to redirect stdlib logs to Loguru
- Async: logger.add(..., enqueue=True) for non-blocking writes

Loguru Logging Pipeline

# observability/loguru_pipeline.py — structured logging with Loguru
from __future__ import annotations
import logging
import sys
import time
import traceback
from contextlib import contextmanager
from functools import wraps
from pathlib import Path
from typing import Any, Callable

from loguru import logger


# ── 0. Setup functions ────────────────────────────────────────────────────────

def setup_logging(
    log_dir:        str = "logs",
    app_name:       str = "app",
    level:          str = "INFO",
    json_enabled:   bool = True,
    stderr_enabled: bool = True,
    rotation:       str = "50 MB",
    retention:      str = "14 days",
    compression:    str = "gz",
    enqueue:        bool = True,
) -> None:
    """
    Configure Loguru with stderr + rotating file + optional JSON sink.
    Call once at application startup.
    enqueue=True makes all sinks non-blocking and async-safe.
    """
    # Remove default stderr handler so we control format
    logger.remove()

    log_path = Path(log_dir)
    log_path.mkdir(parents=True, exist_ok=True)

    # Human-readable stderr sink
    if stderr_enabled:
        logger.add(
            sys.stderr,
            level=level,
            colorize=True,
            format=(
                "<green>{time:YYYY-MM-DD HH:mm:ss.SSS}</green> | "
                "<level>{level:<8}</level> | "
                "<cyan>{name}</cyan>:<cyan>{line}</cyan> | "
                "{message}"
            ),
            backtrace=True,
            diagnose=True,
        )

    # Human-readable rotating file
    logger.add(
        log_path / f"{app_name}.log",
        level=level,
        format="{time:YYYY-MM-DD HH:mm:ss.SSS} | {level:<8} | {name}:{line} | {message}",
        rotation=rotation,
        retention=retention,
        compression=compression,
        backtrace=True,
        diagnose=False,    # Don't expose local vars in file logs (security)
        enqueue=enqueue,
    )

    # JSON structured sink for log aggregation (Datadog, Loki, ELK)
    if json_enabled:
        logger.add(
            log_path / f"{app_name}.json.log",
            level=level,
            serialize=True,               # Each line is a JSON object
            rotation=rotation,
            retention=retention,
            compression=compression,
            enqueue=enqueue,
        )

    # Separate error-only sink for alerting
    logger.add(
        log_path / f"{app_name}.errors.log",
        level="ERROR",
        format="{time} | {level} | {name}:{function}:{line} | {message}",
        rotation="10 MB",
        retention="90 days",
        backtrace=True,
        enqueue=enqueue,
    )

    logger.info(
        "Logging configured: level={level}, sinks=[stderr, {app}.log, {app}.json.log, errors.log]",
        level=level, app=app_name,
    )


# ── 1. Intercepting stdlib logging ────────────────────────────────────────────

class InterceptHandler(logging.Handler):
    """
    Redirect all stdlib logging calls (uvicorn, SQLAlchemy, etc.) to Loguru.
    Usage: logging.basicConfig(handlers=[InterceptHandler()], level=0, force=True)
    """
    def emit(self, record: logging.LogRecord) -> None:
        # Map stdlib level to Loguru level
        try:
            level = logger.level(record.levelname).name
        except ValueError:
            level = record.levelno

        # Walk up call stack to find the real caller (skip logging internals)
        frame, depth = logging.currentframe(), 2
        while frame and frame.f_code.co_filename == logging.__file__:
            frame = frame.f_back
            depth += 1

        logger.opt(depth=depth, exception=record.exc_info).log(
            level, record.getMessage()
        )


def intercept_stdlib_logging(
    loggers: list[str] = None,
    level: int = logging.DEBUG,
) -> None:
    """
    Install InterceptHandler for all stdlib loggers.
    Optionally restrict to specific logger names.
    """
    handler = InterceptHandler()
    logging.basicConfig(handlers=[handler], level=level, force=True)

    # Also configure named loggers (uvicorn, sqlalchemy, httpx, etc.)
    for name in (loggers or []):
        logging.getLogger(name).handlers = [handler]
        logging.getLogger(name).propagate = False


# ── 2. Contextual logging ─────────────────────────────────────────────────────

def get_request_logger(
    request_id: str,
    user_id:    str | int | None = None,
    **extra,
) -> "loguru.Logger":
    """
    Return a logger pre-bound with request context.
    Every log call from this logger includes request_id and user_id fields.
    """
    return logger.bind(request_id=request_id, user_id=str(user_id or "anon"), **extra)


@contextmanager
def log_context(**fields):
    """
    Context manager that binds fields for all log calls within the block.
    Useful for wrapping a request handler or background task.
    """
    bound = logger.bind(**fields)
    token = logger.configure(patcher=lambda record: record["extra"].update(fields))
    try:
        yield bound
    finally:
        pass   # loguru's bind() scoping handles cleanup automatically


# ── 3. Decorators ─────────────────────────────────────────────────────────────

def log_calls(
    level:    str = "DEBUG",
    log_args: bool = False,
    log_time: bool = True,
) -> Callable:
    """
    Decorator: log function entry, exit, and elapsed time.
    """
    def decorator(fn: Callable) -> Callable:
        @wraps(fn)
        def wrapper(*args, **kwargs):
            name = f"{fn.__module__}.{fn.__qualname__}"
            if log_args:
                logger.log(level, "→ {} args={} kwargs={}", name, args, kwargs)
            else:
                logger.log(level, "→ {}", name)
            t0 = time.perf_counter()
            try:
                result = fn(*args, **kwargs)
                elapsed = (time.perf_counter() - t0) * 1000
                if log_time:
                    logger.log(level, "← {} ({:.1f}ms)", name, elapsed)
                return result
            except Exception:
                elapsed = (time.perf_counter() - t0) * 1000
                logger.opt(exception=True).error("✗ {} failed ({:.1f}ms)", name, elapsed)
                raise
        return wrapper
    return decorator


def retry_with_logging(
    max_attempts: int = 3,
    delay:        float = 1.0,
    exceptions:   tuple = (Exception,),
) -> Callable:
    """Decorator: retry with exponential back-off, logging each attempt."""
    def decorator(fn: Callable) -> Callable:
        @wraps(fn)
        def wrapper(*args, **kwargs):
            for attempt in range(1, max_attempts + 1):
                try:
                    return fn(*args, **kwargs)
                except exceptions as exc:
                    if attempt == max_attempts:
                        logger.error(
                            "{} failed after {} attempts: {}",
                            fn.__qualname__, max_attempts, exc
                        )
                        raise
                    wait = delay * (2 ** (attempt - 1))
                    logger.warning(
                        "{} attempt {}/{} failed, retrying in {:.1f}s: {}",
                        fn.__qualname__, attempt, max_attempts, wait, exc
                    )
                    time.sleep(wait)
        return wrapper
    return decorator


# ── 4. Performance and audit logging ─────────────────────────────────────────

@contextmanager
def timed_block(name: str, level: str = "INFO", **extra):
    """Log elapsed time for a code block."""
    t0 = time.perf_counter()
    bound = logger.bind(**extra)
    bound.log(level, "{} started", name)
    try:
        yield
        elapsed = (time.perf_counter() - t0) * 1000
        bound.log(level, "{} completed in {:.1f}ms", name, elapsed)
    except Exception:
        elapsed = (time.perf_counter() - t0) * 1000
        bound.error("{} failed after {:.1f}ms", name, elapsed)
        raise


def log_audit_event(
    action:      str,
    actor_id:    str,
    resource:    str,
    resource_id: str,
    outcome:     str = "success",
    **details,
) -> None:
    """
    Write a structured audit log entry.
    Use a dedicated JSON sink for audit events in compliance contexts.
    """
    logger.bind(
        event_type="audit",
        actor_id=actor_id,
        resource=resource,
        resource_id=resource_id,
        outcome=outcome,
        **details,
    ).info("AUDIT: {} {} {}{}", action, resource, resource_id, outcome)


# ── 5. Exception logging helpers ──────────────────────────────────────────────

def log_exception(
    exc:      Exception,
    message:  str = "Unhandled exception",
    level:    str = "ERROR",
    **context,
) -> None:
    """Log an exception with optional context fields."""
    logger.bind(**context).opt(exception=exc).log(level, message)


def safe_call(fn: Callable, *args, default=None, **kwargs) -> Any:
    """
    Call fn(*args, **kwargs), returning default and logging on exception.
    Useful for non-critical operations (metrics, caching) that must not crash.
    """
    try:
        return fn(*args, **kwargs)
    except Exception as exc:
        logger.opt(exception=True).warning(
            "safe_call: {} raised {}: {}", fn.__qualname__, type(exc).__name__, exc
        )
        return default


# ── Demo ──────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    # Configure logging
    setup_logging(log_dir="/tmp/demo_logs", app_name="demo", level="DEBUG",
                  json_enabled=True, stderr_enabled=True, enqueue=False)

    # Basic levels
    logger.debug("Config loaded: db_host={host}", host="localhost")
    logger.info("Application started, version=1.0.0")
    logger.warning("Cache miss rate high: {rate:.1%}", rate=0.42)

    # Bound context
    req_log = get_request_logger("req-abc123", user_id=99, endpoint="/api/orders")
    req_log.info("Request received")
    req_log.info("Query returned {n} rows", n=42)

    # Timed block
    with timed_block("database_query", table="users"):
        time.sleep(0.01)   # simulate work

    # Exception capture
    @logger.catch
    def risky():
        raise ValueError("something went wrong")

    risky()   # exception logged, execution continues

    # Audit event
    log_audit_event(
        action="DELETE", actor_id="user_99", resource="post",
        resource_id="post_17", reason="user_request"
    )

    # Decorated function
    @log_calls(level="DEBUG", log_time=True)
    def process_batch(n: int) -> int:
        time.sleep(0.005)
        return n * 2

    result = process_batch(50)
    logger.info("Result: {}", result)
    logger.success("Demo complete.")

For the stdlib logging module alternative — Python’s logging requires basicConfig, getLogger, Formatter, FileHandler, and RotatingFileHandler setup across 20+ lines while Loguru’s logger.add("app.log", rotation="50 MB", retention="14 days", serialize=True, enqueue=True) configures a thread-safe compressed rotating structured JSON sink in a single call, logger.bind(request_id=id) injects fields into every subsequent call without a LoggerAdapter, and @logger.catch wraps any function with full stack capture including local variable values when diagnose=True. For the structlog alternative for structured logging — structlog adds a processor pipeline but requires configuring bound loggers and renderer chains while Loguru’s serialize=True emits newline-delimited JSON with timestamp, level, module, function, line, and message as native fields that Datadog, Loki, and ELK parse without custom parsers, and InterceptHandler redirects uvicorn/SQLAlchemy/httpx stdlib loggers into Loguru in five lines. The Claude Skills 360 bundle includes Loguru skill sets covering logger.add with rotation and retention, serialize JSON output, bind contextual fields, opt lazy evaluation, catch decorator and context manager, InterceptHandler for stdlib redirect, log_calls timing decorator, retry with logging, timed_block context manager, and audit event logging. Start with the free tier to try structured logging code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free