AgentSkillsCN

logging

结构化日志记录模式、日志级别、追踪ID,以及哪些内容应被记录、哪些内容绝不应被记录。在设置日志记录、选择日志级别、实施请求追踪,或审查日志输出质量时使用此功能。

SKILL.md
--- frontmatter
name: logging
description: Structured logging patterns, log levels, trace IDs, and what to log vs what never to log. Use when setting up logging, choosing log levels, implementing request tracing, or reviewing log output quality.
metadata:
  version: "1.0"

Logging

Decision Tree

code
What to log? → What level?
    ├─ Application started, config loaded → INFO
    ├─ Request received/completed → INFO (or DEBUG in high-traffic)
    ├─ User action (login, purchase, permission change) → INFO
    ├─ Expected failure (validation, 404) → WARN
    ├─ Unexpected failure (unhandled error, 500) → ERROR
    ├─ Variable values during debugging → DEBUG
    └─ Granular tracing (entering/exiting functions) → TRACE (off in production)

Log Levels

LevelWhenProduction?
ERRORUnexpected failures, needs attentionAlways on
WARNExpected failures, degraded stateAlways on
INFOBusiness events, lifecycle eventsUsually on
DEBUGDiagnostic detail, variable valuesOff (enable per-request)
TRACEFunction-level tracingOff

Structured Logging Format

Always log as structured JSON, not free-text strings.

code
BAD:  logger.info(`User ${userId} logged in from ${ip}`)
GOOD: logger.info('User logged in', { userId, ip, method: 'password' })
json
{
  "level": "info",
  "message": "User logged in",
  "timestamp": "2026-02-05T12:00:00Z",
  "userId": "abc-123",
  "ip": "192.168.1.1",
  "method": "password",
  "requestId": "req-456",
  "service": "auth-api"
}

What to Log

Always LogNEVER Log
Request ID / trace IDPasswords or password hashes
User ID (not PII)API keys, tokens, secrets
Action performedCredit card numbers
HTTP method, path, status, durationSession tokens
Error messages and codesFull request bodies with PII
Timestamp (ISO 8601, UTC)Social security / national IDs

Request Tracing

Assign a unique requestId at the edge (API gateway or first middleware). Pass it through every log entry and downstream call.

typescript
// Middleware
app.use((req, res, next) => {
  req.id = req.headers['x-request-id'] || crypto.randomUUID();
  res.setHeader('x-request-id', req.id);
  next();
});

// Then in every log call
logger.info('Processing payment', { requestId: req.id, orderId });

Return it in error responses so users can reference it in support requests.

Tools by Stack

StackLibraryKey Feature
Node.jspinoFast structured JSON, low overhead
Node.jswinstonFlexible transports, widely used
PythonstructlogStructured logging, processors
Pythonlogging (stdlib)Built-in, use with JSON formatter
Goslog (stdlib 1.21+)Structured, built-in

Anti-Patterns

Anti-PatternFix
console.log / print in productionUse structured logger
Logging sensitive dataScrub PII, mask secrets
No request IDAdd trace ID middleware
String concatenation in log messagesUse structured fields
Logging in hot loopsGuard with level check or move outside loop
No log rotation / retention policyConfigure max size, retention days