python-log-expert
Python Log Expert
Expert guidance for Python logging libraries including structlog, standard logging, and log analysis.
Quick Start
Basic structlog setup
import structlog
# Configure structlog
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.processors.JSONRenderer()
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
# Get a logger
log = structlog.get_logger()
log.info("Hello, world!", user="alice")
Standard logging integration
import logging
import structlog
# Standard logging configuration
logging.basicConfig(
format="%(message)s",
stream=sys.stdout,
level=logging.INFO,
)
structlog.configure(
processors=[
structlog.stdlib.filter_by_level,
structlog.stdlib.add_logger_name,
structlog.stdlib.add_log_level,
structlog.stdlib.PositionalArgumentsFormatter(),
structlog.processors.TimeStamper(fmt="iso"),
structlog.processors.StackInfoRenderer(),
structlog.processors.format_exc_info,
structlog.processors.UnicodeDecoder(),
structlog.stdlib.ProcessorFormatter.wrap_for_formatter,
],
context_class=dict,
logger_factory=structlog.stdlib.LoggerFactory(),
wrapper_class=structlog.stdlib.BoundLogger,
cache_logger_on_first_use=True,
)
Instructions
- Analyze logging requirements: Determine if you need structured logging, standard logging, or both
- Configure logging: Set up appropriate processors and formatters
- Implement context: Add relevant context to your log messages
- Handle exceptions: Use proper exception logging techniques
- Performance considerations: Optimize logging for production use
Deep Implementation Analysis
When you need to understand specific implementation details or troubleshoot complex issues, examine the actual source code:
- How processors work: Look at
source/structlog/src/structlog/processors.pyto understand the JSONRenderer, TimeStamper, and other processors - Context binding mechanism: Examine
source/structlog/src/structlog/contextvars.pyfor the actual contextvar implementation - Standard library bridge: Study
source/structlog/src/structlog/stdlib.pyto understand how structlog integrates with Python's logging module - Logger factory patterns: Review
source/structlog/src/structlog/_base.pyfor the core BoundLogger implementation - Configuration defaults: Check
source/structlog/src/structlog/_config.pyfor default processor chains and settings
For detailed configuration options, see REFERENCE.md. For practical examples, see EXAMPLES.md. For log analysis scripts, see scripts/ directory.
For actual implementation details and source code analysis, refer to the structlog source code in source/structlog/src/structlog/ - this contains the complete library source including processors, formatters, and core implementations that you can examine for deeper understanding.
Requirements
The structlog package must be installed in your environment:
pip install structlog
Best Practices
- Use structured logging with meaningful context
- Configure appropriate log levels for different environments
- Include correlation IDs for request tracking
- Use JSON formatting for machine-readable logs
- Implement proper error handling and exception logging
- Consider performance impact in high-throughput applications
Source Code Analysis
For deep understanding of structlog internals, examine the source code:
- Processors:
source/structlog/src/structlog/processors.py- Core data transformation logic including JSONRenderer, TimeStamper, etc. - Standard Library Integration:
source/structlog/src/structlog/stdlib.py- Logger factory and standard library bridging - Context Management:
source/structlog/src/structlog/contextvars.py- Context binding implementation - Core Configuration:
source/structlog/src/structlog/_config.py- Configuration management and defaults - Base Logger:
source/structlog/src/structlog/_base.py- Core logger implementation - Output Formatting:
source/structlog/src/structlog/_output.py- Output formatting and rendering logic - Development Tools:
source/structlog/src/structlog/dev.py- Console rendering for development - Testing:
source/structlog/src/structlog/testing.py- Testing utilities and helpers
Troubleshooting
Common issues and solutions:
- Logs not appearing: Check log level configuration and examine
source/structlog/src/structlog/stdlib.pyfor logger factory setup and BoundLogger implementation - Missing context: Ensure proper context binding - see
source/structlog/src/structlog/contextvars.pyfor implementation details - Performance issues: Review processor chain in
source/structlog/src/structlog/processors.pyand output formatting - check TimeStamper and JSONRenderer optimizations - JSON formatting problems: Verify context values are JSON-serializable - check
source/structlog/src/structlog/processors.pyfor JSONRenderer implementation - Configuration issues: Examine
source/structlog/src/structlog/_config.pyfor default values and configuration logic
More from straydragon/my-claude-skills
rust-cli-tui-developer
Expert guidance for Rust CLI and TUI development with official examples from clap, inquire, and ratatui libraries. Use when building command-line interfaces, terminal user interfaces, or console applications in Rust. Provides structured patterns, best practices, and real code implementations from official sources.
11feishu-openapi-dev
Expert guidance for Feishu (飞书) / Lark OpenAPI Python development. Build Feishu applications, robots, handle event subscriptions, card callbacks, and API integrations. Use when working with Feishu SDK, lark-oapi, building Feishu bots, or mentioning 飞书 development.
10lib-slint-expert
Comprehensive Slint GUI development expert based on official source code. Covers Rust integration, component design, layouts, styling, animations, cross-platform deployment, and performance optimization. Use when working with Slint UI toolkit, building native GUI applications, or when mentioning Slint, GUI development, or Rust user interfaces. Built with official documentation and examples.
7mermaid-expert
Expert guidance for Mermaid.js diagramming library. Create flowcharts, sequence diagrams, class diagrams, state diagrams, Gantt charts, git graphs, and block diagrams. Use when working with Mermaid syntax, creating diagrams, or visualizing complex concepts and workflows.
4agent-skills-expert
Expert for creating and managing Claude Code Agent Skills. Create skills with git submodule + sparse-checkout for source references, write SKILL.md with proper frontmatter, and follow best practices. Use when creating new skills, adding source references to skills, or managing skill configurations.
4ui-ux-pro-max
UI/UX design intelligence. 50 styles, 21 palettes, 50 font pairings, 20 charts, 8 stacks (React, Next.js, Vue, Svelte, SwiftUI, React Native, Flutter, Tailwind). Actions: plan, build, create, design, implement, review, fix, improve, optimize, enhance, refactor, check UI/UX code. Projects: website, landing page, dashboard, admin panel, e-commerce, SaaS, portfolio, blog, mobile app, .html, .tsx, .vue, .svelte. Elements: button, modal, navbar, sidebar, card, table, form, chart. Styles: glassmorphism, claymorphism, minimalism, brutalism, neumorphism, bento grid, dark mode, responsive, skeuomorphism, flat design. Topics: color palette, accessibility, animation, layout, typography, font pairing, spacing, hover, shadow, gradient.
3