Skip to main content
skill CX Codex

logging-best-practices · secondsky — Claude Code skill: structured logs, no PII

A skill that tells your agent how to log: levels, request context, PII masking. Useful when logs bloat or passwords end up in them.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add secondsky/claude-skills --skill logging-best-practices -a claude-code
Open on GitHub

What logging-best-practices does

The skill spells out how application logging should look: a level table from DEBUG to FATAL with a note on what belongs in production, Winston examples with JSON output and a separate error file, request context via AsyncLocalStorage carrying requestId and userId, and a sanitize helper that redacts password, ssn, creditCard, token and masks email addresses.

There is a section on what never to do: logging passwords or tokens, leaving console.log in production, logging inside tight loops, shipping stack traces for client errors. A separate references/advanced-logging.md file covers Python structlog, Go zap, ELK Stack, AWS CloudWatch and OpenTelemetry tracing.

The code samples are JavaScript, so a Python or Go project means reading the reference and porting the ideas yourself. The default level comes from LOG_LEVEL and falls back to info — worth checking before deploy, because a missing variable silently keeps you at the wrong verbosity.

What it helps with

  • split messages across levels and keep DEBUG out of production
  • attach requestId and userId to every log line
  • strip passwords, tokens and emails before they hit disk
  • set up rotation and a dedicated error log file
  • correlate logs across services with a correlation ID

Example prompts for the agent

  • Set up structured logging in this service following the logging-best-practices skill
  • Add request context to the logs with AsyncLocalStorage, like the skill shows
  • Audit the logs for PII and mask the sensitive fields

How to install logging-best-practices

1Skills CLI (Vercel)

npx skills add secondsky/claude-skills --skill logging-best-practices -a claude-code

2Manually: put the files in place

~/.claude/skills/logging-best-practices/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/logging-best-practices/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about logging-best-practices

How do I install logging-best-practices in Claude?

Run npx skills add secondsky/claude-skills --skill logging-best-practices -a claude-code. The skill file then lands at ~/.claude/skills/logging-best-practices/SKILL.md.

What does logging-best-practices do?

It gives the agent rules for logging: which levels to use, how to carry request context, how to sanitize personal data, and what should never be written to a log.

Does it only cover JavaScript?

The main examples use Winston and AsyncLocalStorage, so JavaScript. For Python structlog, Go zap, ELK, CloudWatch and OpenTelemetry there is a separate reference file inside the skill.

Source text

SKILL.md · MIT
---
name: logging-best-practices
description: Structured logging with proper levels, context, PII handling, centralized aggregation. Use for application logging, log management integration, distributed tracing, or encountering log bloat, PII exposure, missing context errors.
license: MIT
---

# Logging Best Practices

Implement secure, structured logging with proper levels and context.

## Log Levels

| Level | Use For | Production |
|-------|---------|------------|
| DEBUG | Detailed debugging | Off |
| INFO | Normal operations | On |
| WARN | Potential issues | On |
| ERROR | Errors with recovery | On |
| FATAL | Critical failures | On |

## Structured Logging (Winston)

```javascript
const winston = require('winston');

const logger = winston.createLogger({
  level: process.env.LOG_LEVEL || 'info',
  format: winston.format.combine(
    winston.format.timestamp(),
    winston.format.json()
  ),
  defaultMeta: { service: 'api-service' },
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'error.log', level: 'error' })
  ]
});

// Usage
logger.info('User logged in', { userId: '123', ip: '192.168.1.1' });
logger.error('Payment failed', { error: err.message, orderId: '456' });
```

## Request Context

```javascript
const { AsyncLocalStorage } = require('async_hooks');
const storage = new AsyncLocalStorage();

app.use((req, res, next) => {
  const context = {
    requestId: req.headers['x-request-id'] || uuid(),
    userId: req.user?.id
  };
  storage.run(context, next);
});

function log(level, message, meta = {}) {
  const context = storage.getStore() || {};
  logger.log(level, message, { ...context, ...meta });
}
```

## PII Sanitization

```javascript
const sensitiveFields = ['password', 'ssn', 'creditCard', 'token'];

function sanitize(obj) {
  const sanitized = { ...obj };
  for (const field of sensitiveFields) {
    if (sanitized[field]) sanitized[field] = '[REDACTED]';
  }
  if (sanitized.email) {
    sanitized.email = sanitized.email.replace(/(.{2}).*@/, '$1***@');
  }
  return sanitized;
}
```

## Best Practices

- Use structured JSON format
- Include correlation IDs across services
- Sanitize all PII before logging
- Use async logging for performance
- Implement log rotation
- Never log at DEBUG in production

## Additional Implementations

See [references/advanced-logging.md](references/advanced-logging.md) for:
- Python structlog setup
- Go zap high-performance logging
- ELK Stack integration
- AWS CloudWatch configuration
- OpenTelemetry tracing

## Never Do

- Log passwords or tokens
- Use console.log in production
- Log inside tight loops
- Include stack traces for client errors

© secondsky · MIT · secondsky/claude-skills

Similar skills

All skills for Backend and APIs →

logging-best-practices by other authors comes first.

logging-best-practices boristane This skill guides your agent to implement wide events (canonical log lines) instead of scattered logs. It consolidates request metrics, business context, and errors into one JSON event. CX Codex +1 4,800installs logging-best-practices aj-geddes A skill for setting up logging: levels, JSON output, context fields, PII handling and centralized collection. CX Codex +1 730installs logging-best-practices Mindrally A skill that spells out logging rules: structured JSON, log levels, shared fields and request context. Useful when every service logs its own way. CX Codex +1 791installs application-logging aj-geddes A skill that walks your agent through structured logging: levels, context, request tracing and centralized aggregation. CX Codex +1 496installs logging codewithmukesh A .NET 10 observability skill: splits liveness from readiness, wires correlation IDs, and sets log levels. CX Codex +1 2,800installs review-logging-patterns HugoRCD The skill audits logging in a TypeScript codebase, flags console.log spam and context-free errors, and suggests moving to evlog. CX Codex +1 750installs observability-and-instrumentation addyosmani Instruments code with structured logs, RED and USE metrics, and distributed tracing. Use it when shipping features to production so issues are diagnosable. CX Codex +1 54,800installs otel-instrumentation dash0 Gives your agent a rulebook for OpenTelemetry spans, metrics and logs, so telemetry answers questions instead of burning budget. CX Codex +1 972installs security-audit-logging aj-geddes A skill that walks your agent through building security audit logging: events, user actions, system changes. Useful when you need logs for compliance or incident review. CX Codex +1 561installs

From the same set94

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor