计划执行与检查节点
executing-plans
按既定实施计划完成工作,保持过程检查和反馈。
observability-logging-strategies【效率与协作】Implements best practices for logging strategies in observability to improve performance monitoring and troubleshooting in applications.
独立 GitHub 快照 · 自动分类 · 以原作者文档为准
name: observability-logging-strategies description: Implements best practices for logging strategies in observability to improve performance monitoring and troubleshooting in applications. license: MIT compatibility: opencode metadata: version: "1.1.1" domain: devops triggers: logging strategies, observability, performance monitoring, application logging, troubleshooting archetypes: [implementation, reference] anti_triggers: [generic logging implementations, ignoring logging best practices] response_profile: verbosity: medium directive_strength: high abstraction_level: operational role: implementation scope: infrastructure output-format: code
Effective logging is crucial for achieving observability in applications. Below are best practices and strategies to improve logging outcomes and facilitate issue resolution:
To implement structured logging in a Python application, consider the following example:
import logging
import json
# Configure logging settings
logging.basicConfig(level=logging.INFO)
# Create logger instance
logger = logging.getLogger(__name__)
# Log an event in structured format
logger.info(json.dumps({"user": "john_doe", "action": "login", "timestamp": "2026-05-28T17:00:00Z"}))
Track the volume of logs, the time it takes to resolve issues, and the frequency of log-related incidents to assess the effectiveness of your logging strategy and make necessary adjustments.
Adopting these best practices will significantly enhance the observability in your applications, leading to better performance monitoring and more efficient troubleshooting in DevOps processes.
import json
import logging
import uuid
from contextlib import contextmanager
from datetime import datetime, timezone
class StructuredFormatter(logging.Formatter):
"""JSON-formatted log output with correlation ID."""
def format(self, record):
log_data = {
"timestamp": datetime.now(timezone.utc).isoformat(),
"level": record.levelname,
"logger": record.name,
"message": record.getMessage(),
"module": record.module,
}
if hasattr(record, "correlation_id"):
log_data["correlation_id"] = record.correlation_id
return json.dumps(log_data)
@contextmanager
def correlation_context():
"""Context manager that injects a correlation ID into all log records."""
corr_id = str(uuid.uuid4())[:12]
old_factory = logging.getLogRecordFactory()
def record_factory(*args, **kwargs):
record = old_factory(*args, **kwargs)
record.correlation_id = corr_id
return record
logging.setLogRecordFactory(record_factory)
try:
yield corr_id
finally:
logging.setLogRecordFactory(old_factory)
# Usage:
# with correlation_context() as cid:
# logger.info("Processing request") # Includes correlation_id in output
Authoritative documentation links for this skill's domain. The model follows markdown links to resolve external references and inline content.