python-logging-best-practices

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Logging Best Practices

Python日志记录最佳实践

When to Use This Skill

何时使用该技能

Use this skill when:
  • Setting up Python logging with loguru
  • Configuring structured JSONL logging for analysis
  • Implementing log rotation
  • Using platformdirs for cross-platform log directories
在以下场景使用该技能:
  • 使用loguru搭建Python日志记录系统
  • 配置用于分析的结构化JSONL日志
  • 实现日志轮转
  • 使用platformdirs实现跨平台日志目录

Overview

概述

Unified reference for Python logging patterns optimized for machine readability (Claude Code analysis) and operational reliability.
针对Python日志记录模式的统一参考,优化了机器可读性(适配Claude Code分析)和运行可靠性。

MANDATORY Best Practices

强制遵循的最佳实践

1. Log Rotation (ALWAYS CONFIGURE)

1. 日志轮转(必须配置)

Prevent unbounded log growth - configure rotation for ALL log files:
python
undefined
防止日志无限制增长 - 为所有日志文件配置轮转:
python
undefined

Loguru pattern (recommended for modern scripts)

Loguru pattern (recommended for modern scripts)

from loguru import logger
logger.add( log_path, rotation="10 MB", # Rotate at 10MB retention="7 days", # Keep 7 days compression="gz" # Compress old logs )
from loguru import logger
logger.add( log_path, rotation="10 MB", # Rotate at 10MB retention="7 days", # Keep 7 days compression="gz" # Compress old logs )

RotatingFileHandler pattern (stdlib-only)

RotatingFileHandler pattern (stdlib-only)

from logging.handlers import RotatingFileHandler
handler = RotatingFileHandler( log_path, maxBytes=100 * 1024 * 1024, # 100MB backupCount=5 # Keep 5 backups (~500MB max) )
undefined
from logging.handlers import RotatingFileHandler
handler = RotatingFileHandler( log_path, maxBytes=100 * 1024 * 1024, # 100MB backupCount=5 # Keep 5 backups (~500MB max) )
undefined

2. JSONL Format (Machine-Readable)

2. JSONL格式(机器可读)

Use JSONL (
.jsonl
) for logs that Claude Code or other tools will analyze:
python
undefined
将Claude Code或其他工具要分析的日志设置为JSONL(
.jsonl
)格式:
python
undefined

One JSON object per line - jq-parseable

One JSON object per line - jq-parseable

{"timestamp": "2026-01-14T12:45:23.456Z", "level": "info", "message": "..."} {"timestamp": "2026-01-14T12:45:24.789Z", "level": "error", "message": "..."}

**File extension**: Always use `.jsonl` (not `.json` or `.log`)

**Validation**: `cat file.jsonl | jq -c .`

**Terminology**: JSONL is canonical. Equivalent terms: NDJSON, JSON Lines.
{"timestamp": "2026-01-14T12:45:23.456Z", "level": "info", "message": "..."} {"timestamp": "2026-01-14T12:45:24.789Z", "level": "error", "message": "..."}

**文件扩展名**:始终使用`.jsonl`(不要用`.json`或`.log`)

**验证**:`cat file.jsonl | jq -c .`

**术语说明**:JSONL是标准叫法,等效术语包括NDJSON、JSON Lines。

When to Use Which Approach

不同方案的适用场景

ApproachUse CaseProsCons
loguru
Modern scripts, CLI toolsZero-config, async-safe, built-in rotationExternal dependency
RotatingFileHandler
LaunchAgent daemons, stdlib-onlyNo dependenciesMore setup
logger_setup.py
Rich terminal appsBeautiful outputComplex setup
方案适用场景优势劣势
loguru
现代脚本、CLI工具零配置、支持异步、内置日志轮转功能依赖第三方库
RotatingFileHandler
LaunchAgent守护进程、仅用标准库无额外依赖配置步骤繁琐
logger_setup.py
功能丰富的终端应用输出美观配置复杂

Complete Loguru + platformdirs Pattern

完整的Loguru + platformdirs示例

Cross-platform log directory handling with structured JSONL output:
python
#!/usr/bin/env python3
实现跨平台日志目录管理及结构化JSONL输出:
python
#!/usr/bin/env python3

/// script

/// script

requires-python = ">=3.11"

requires-python = ">=3.11"

dependencies = ["loguru", "platformdirs"]

dependencies = ["loguru", "platformdirs"]

///

///

import json import sys from pathlib import Path from uuid import uuid4
import platformdirs from loguru import logger
def json_formatter(record) -> str: """JSONL formatter for Claude Code analysis.""" log_entry = { "timestamp": record["time"].strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z", "level": record["level"].name.lower(), "component": record["function"], "operation": record["extra"].get("operation", "unknown"), "operation_status": record["extra"].get("status", None), "trace_id": record["extra"].get("trace_id"), "message": record["message"], "context": {k: v for k, v in record["extra"].items() if k not in ("operation", "status", "trace_id", "metrics")}, "metrics": record["extra"].get("metrics", {}), "error": None }
if record["exception"]:
    exc_type, exc_value, _ = record["exception"]
    log_entry["error"] = {
        "type": exc_type.__name__ if exc_type else "Unknown",
        "message": str(exc_value) if exc_value else "Unknown error",
    }

return json.dumps(log_entry)
def setup_logger(app_name: str = "my-app"): """Configure Loguru for machine-readable JSONL output.""" logger.remove()
# Console output (JSONL to stderr)
logger.add(sys.stderr, format=json_formatter, level="INFO")

# Cross-platform log directory
# macOS: ~/Library/Logs/{app_name}/
# Linux: ~/.local/state/{app_name}/log/
log_dir = Path(platformdirs.user_log_dir(
    appname=app_name,
    ensure_exists=True
))

# File output with rotation
logger.add(
    str(log_dir / f"{app_name}.jsonl"),
    format=json_formatter,
    rotation="10 MB",
    retention="7 days",
    compression="gz",
    level="DEBUG"
)

return logger
import json import sys from pathlib import Path from uuid import uuid4
import platformdirs from loguru import logger
def json_formatter(record) -> str: """JSONL formatter for Claude Code analysis.""" log_entry = { "timestamp": record["time"].strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z", "level": record["level"].name.lower(), "component": record["function"], "operation": record["extra"].get("operation", "unknown"), "operation_status": record["extra"].get("status", None), "trace_id": record["extra"].get("trace_id"), "message": record["message"], "context": {k: v for k, v in record["extra"].items() if k not in ("operation", "status", "trace_id", "metrics")}, "metrics": record["extra"].get("metrics", {}), "error": None }
if record["exception"]:
    exc_type, exc_value, _ = record["exception"]
    log_entry["error"] = {
        "type": exc_type.__name__ if exc_type else "Unknown",
        "message": str(exc_value) if exc_value else "Unknown error",
    }

return json.dumps(log_entry)
def setup_logger(app_name: str = "my-app"): """Configure Loguru for machine-readable JSONL output.""" logger.remove()
# Console output (JSONL to stderr)
logger.add(sys.stderr, format=json_formatter, level="INFO")

# Cross-platform log directory
# macOS: ~/Library/Logs/{app_name}/
# Linux: ~/.local/state/{app_name}/log/
log_dir = Path(platformdirs.user_log_dir(
    appname=app_name,
    ensure_exists=True
))

# File output with rotation
logger.add(
    str(log_dir / f"{app_name}.jsonl"),
    format=json_formatter,
    rotation="10 MB",
    retention="7 days",
    compression="gz",
    level="DEBUG"
)

return logger

Usage

Usage

setup_logger("my-app") trace_id = str(uuid4())
logger.info( "Operation started", operation="my_operation", status="started", trace_id=trace_id )
logger.info( "Operation complete", operation="my_operation", status="success", trace_id=trace_id, metrics={"duration_ms": 150, "items_processed": 42} )
undefined
setup_logger("my-app") trace_id = str(uuid4())
logger.info( "Operation started", operation="my_operation", status="started", trace_id=trace_id )
logger.info( "Operation complete", operation="my_operation", status="success", trace_id=trace_id, metrics={"duration_ms": 150, "items_processed": 42} )
undefined

Semantic Fields Reference

语义字段参考

FieldTypePurpose
timestamp
ISO 8601 with ZEvent ordering
level
stringdebug/info/warning/error/critical
component
stringModule/function name
operation
stringWhat action is being performed
operation_status
stringstarted/success/failed/skipped
trace_id
UUID4Correlation for async operations
message
stringHuman-readable description
context
objectOperation-specific metadata
metrics
objectQuantitative data (counts, durations)
error
object/nullException details if failed
字段类型用途
timestamp
ISO 8601带Z后缀事件排序
level
字符串debug/info/warning/error/critical
component
字符串模块/函数名
operation
字符串当前执行的操作
operation_status
字符串started/success/failed/skipped
trace_id
UUID4异步操作的关联ID
message
字符串人类可读的描述信息
context
对象操作相关的元数据
metrics
对象量化数据(数量、耗时等)
error
对象/空值操作失败时的异常详情

Related Resources

相关资源

  • Python logging.handlers - RotatingFileHandler for log rotation
  • platformdirs reference - Cross-platform directories
  • loguru patterns - Advanced loguru configuration
  • migration guide - From print() to structured logging
  • Python logging.handlers - 用于日志轮转的RotatingFileHandler
  • platformdirs参考 - 跨平台目录处理
  • loguru模式 - Loguru高级配置
  • 迁移指南 - 从print()到结构化日志的迁移

Anti-Patterns to Avoid

需避免的反模式

  1. Unbounded logs - Always configure rotation
  2. print() for logging - Use structured logger
  3. Bare except - Catch specific exceptions, log them
  4. Silent failures - Log errors before suppressing
  5. Hardcoded paths - Use platformdirs for cross-platform

  1. 无限制增长的日志 - 始终配置日志轮转
  2. 用print()记录日志 - 使用结构化日志记录器
  3. 裸except语句 - 捕获特定异常并记录
  4. 静默失败 - 抑制异常前先记录错误
  5. 硬编码路径 - 使用platformdirs实现跨平台兼容

Troubleshooting

故障排查

IssueCauseSolution
loguru not foundNot installedRun
uv add loguru
Logs not appearingWrong log levelSet level to DEBUG for troubleshooting
Log rotation not workingMissing rotation configAdd rotation param to logger.add()
platformdirs import errorNot installedRun
uv add platformdirs
JSONL parse errorsMalformed log lineCheck for unescaped special characters
Logs in wrong directoryUsing hardcoded pathUse platformdirs.user_log_dir()
问题原因解决方案
loguru未找到未安装运行
uv add loguru
日志未显示日志级别设置错误设置级别为DEBUG以排查问题
日志轮转不生效缺少轮转配置为logger.add()添加rotation参数
platformdirs导入错误未安装运行
uv add platformdirs
JSONL解析错误日志行格式错误检查是否存在未转义的特殊字符
日志存储目录错误使用了硬编码路径使用platformdirs.user_log_dir()