AgentSkillsCN

Logging Standards

日志记录规范

SKILL.md

Logging Standards Skill

Purpose

Standards for consistent, useful logging across all application layers.

Auto-Invoke Triggers

  • Adding log statements
  • Configuring logging infrastructure
  • Debugging issues
  • Setting up monitoring

Log Levels

LevelUse CaseExample
TRACEDetailed debuggingVariable values, loop iterations
DEBUGDiagnostic infoMethod entry/exit, state changes
INFONormal operationsRequest completed, job started
WARNPotential issuesRetry attempt, deprecated usage
ERRORFailuresException caught, operation failed
FATALSystem failureCannot start, out of memory

Level Selection Rules

  • Production default: INFO
  • Development default: DEBUG
  • Never log TRACE in production
  • ERROR should always have context

Log Message Format

Structure

code
[timestamp] [level] [correlation-id] [logger] - message {structured-data}

Example

code
2024-01-15T10:30:45.123Z INFO abc-123-def UserService - User created {"userId": 42, "email": "user@example.com"}

Message Guidelines

  • Start with action verb: "Creating user", "Processing order"
  • Be specific: "User 123 deleted" not "Deleted"
  • Include relevant IDs
  • Keep messages concise

Structured Logging

Required Fields

FieldDescriptionExample
timestampISO 8601 format2024-01-15T10:30:45.123Z
levelLog levelINFO, ERROR
messageHuman-readable"User created"
correlationIdRequest trace IDabc-123-def
loggerSource class/moduleUserService

Contextual Fields

FieldWhen to Include
userIdAuthenticated request
requestPathHTTP request
requestMethodHTTP request
durationOperation timing
errorCodeError situations
stackTraceERROR level only

What to Log

Always Log

  • Application startup/shutdown
  • Authentication events (login, logout, failures)
  • Authorization failures
  • External service calls (start, end, errors)
  • Database migrations
  • Configuration changes
  • Business-critical operations

Per Request

  • Request start (DEBUG)
  • Request completion with duration (INFO)
  • Validation errors (WARN)
  • Exceptions (ERROR)

Never Log

  • Passwords or tokens
  • Credit card numbers
  • Personal data (PII) without masking
  • Session IDs in plain text
  • API keys or secrets
  • Full request/response bodies with sensitive data

PII Masking

Fields to Mask

FieldMaskingExample
EmailPartialj***@example.com
PhonePartial+1***456
Credit CardPartial--****-1234
SSNFull***
PasswordNever log-
TokenNever log-

Masking Rules

  • Mask at log time, not display time
  • Keep enough for debugging (last 4 digits)
  • Never log masked values to DEBUG level

Error Logging

Required Information

  • Error type/code
  • Error message
  • Stack trace (first occurrence)
  • Request context (path, method, user)
  • Correlation ID
  • Timestamp

Error Logging Rules

  • Log full stack trace on first occurrence
  • Group repeated errors (rate limit logging)
  • Include recovery actions taken
  • Don't log expected errors as ERROR

Performance Logging

Metrics to Log

MetricLevelWhen
Request durationINFOEvery request
Slow queryWARN> 1 second
External callDEBUGEvery call
Cache hit/missDEBUGWhen relevant

Slow Operation Thresholds

OperationWarn Threshold
HTTP request> 1s
Database query> 500ms
External API> 2s
Cache operation> 100ms

Log Aggregation

Requirements

  • Centralized log storage
  • Full-text search
  • Correlation ID filtering
  • Time-range queries
  • Alerting on patterns

Recommended Tools

ToolUse Case
ELK StackSelf-hosted, full-featured
DatadogCloud, APM integration
SplunkEnterprise, compliance
LokiKubernetes-native

Log Retention

EnvironmentRetention
Development7 days
Staging30 days
Production90 days
Audit logs1 year+

Archival Rules

  • Compress logs older than 7 days
  • Archive to cold storage after retention
  • Delete non-essential logs aggressively
  • Keep audit logs per compliance

Best Practices

DO

  • Use structured logging (JSON)
  • Include correlation IDs
  • Log at appropriate levels
  • Mask sensitive data
  • Use async logging in production
  • Set up alerts for ERROR logs

DON'T

  • Log passwords or secrets
  • Use string concatenation for messages
  • Log inside tight loops
  • Ignore log level guidelines
  • Skip correlation IDs
  • Log full stack traces repeatedly

Logging Checklist

  • Structured logging configured
  • Log levels appropriate per environment
  • Correlation IDs propagated
  • PII masking implemented
  • Error logs include context
  • Slow operation logging enabled
  • Log aggregation configured
  • Alerts set up for errors
  • Retention policy implemented