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.
npx skills add secondsky/claude-skills --skill logging-best-practices -a claude-codeWhat 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-code2Manually: put the files in place
~/.claude/skills/logging-best-practices/SKILL.mdCopy the whole skill folder; for one project use .claude/skills/logging-best-practices/
1Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill logging-best-practices -a codex2Manually: put the files in place
.agents/skills/logging-best-practices/SKILL.mdFor all projects use ~/.agents/skills/
1Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill logging-best-practices -a cursor2Manually: put the files in place
.cursor/skills/logging-best-practices/SKILL.mdCursor also reads .agents/skills/ and .claude/skills/
1GitHub CLI
gh skill install secondsky/claude-skills logging-best-practices2Manually: put the files in place
.github/skills/logging-best-practices/SKILL.md1Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill logging-best-practices -a gemini-cli2Platform CLI
gemini skills install https://github.com/secondsky/claude-skills --path plugins/logging-best-practices/skills/logging-best-practicesManually: put the files in place
.devin/skills/logging-best-practices/SKILL.mdThe legacy .windsurf/skills/ path still works
Skills CLI (Vercel)
npx skills add secondsky/claude-skills --skill logging-best-practices -a opencodeClaude.ai
Zip the folder and upload it in Settings → Capabilities → Skills
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.