AgentSkillsCN

logging

实施结构化、安全的日志记录,合理设置日志级别、关联 ID 并进行敏感信息脱敏处理。

SKILL.md
--- frontmatter
name: logging
description: Implement structured, secure logging with proper levels, correlation IDs, and redaction

Logging

Implement effective logging — structured format, appropriate levels, correlation IDs, and sensitive data protection.

When to Use

  • Adding logging to new services or features
  • Reviewing existing logging for security and quality
  • Setting up observability infrastructure
  • Debugging production issues through log analysis

Workflow

1. Choose a Structured Logger

Use a logging library that supports structured (JSON) output — not console.log.

LanguageLibrary
JavaScript/TSpino, winston
Pythonstructlog, python-json-logger
Gozerolog, zap
JavaSLF4J + Logback

2. Set Log Levels

LevelWhenExample
errorUnexpected failureDatabase connection lost
warnConcerning but handledRetry succeeded
infoBusiness eventsUser registered
debugDev details (off in prod)Function parameters

3. Include Correlation IDs

Every request should carry a unique ID through all log entries:

javascript
// Generate at entry point
const requestId = req.headers['x-request-id'] || uuid()

// Include in all logs
logger.info('Order processed', { requestId, orderId, userId })

4. Redact Sensitive Data

Never log: passwords, tokens, API keys, PII, credit card numbers

javascript
// BAD
logger.info('Login', { email, password })

// GOOD
logger.info('Login', { email, passwordProvided: !!password })

5. Configure Per-Environment

SettingDevelopmentProduction
Leveldebuginfo
FormatPretty printedJSON
OutputConsoleLog aggregator

6. Verify

  • No sensitive data in log output
  • Structured format (key-value pairs)
  • Correlation IDs present
  • Appropriate log levels used
  • Error logs include stack traces

Red Flags

SignalAction
Secrets appearing in logsAdd redaction immediately
console.log in production codeReplace with structured logger
Debug logging enabled in productionSet level to info or higher
Error logged AND re-thrownChoose one — log or throw

Related Skills

SkillWhen
debuggingUsing logs to debug issues
secure-codingEnsuring logs don't leak data

Backing Guide