Logging Best Practices
Table of Contents
Overview
Comprehensive guide to implementing structured, secure, and performant logging across applications. Covers log levels, structured logging formats, contextual information, PII protection, and centralized logging systems.
When to Use
- Setting up application logging infrastructure
- Implementing structured logging
- Configuring log levels for different environments
- Managing sensitive data in logs
- Setting up centralized logging
- Implementing distributed tracing
- Debugging production issues
- Compliance with logging regulations
Quick Start
Minimal working example:
// logger.ts
enum LogLevel {
DEBUG = 0, // Detailed information for debugging
INFO = 1, // General informational messages
WARN = 2, // Warning messages, potentially harmful
ERROR = 3, // Error messages, application can continue
FATAL = 4, // Critical errors, application must stop
}
class Logger {
constructor(private minLevel: LogLevel = LogLevel.INFO) {}
debug(message: string, context?: object) {
if (this.minLevel <= LogLevel.DEBUG) {
this.log(LogLevel.DEBUG, message, context);
}
}
info(message: string, context?: object) {
if (this.minLevel <= LogLevel.INFO) {
this.log(LogLevel.INFO, message, context);
}
}
warn(message: string, context?: object) {
// ... (see reference guides for full implementation)
Reference Guides
Detailed implementations in the references/ directory:
| Guide |
Contents |
| Log Levels |
Log Levels |
| Structured Logging (JSON) |
Structured Logging (JSON) |
| Contextual Logging |
Contextual Logging |
| PII and Sensitive Data Handling |
PII and Sensitive Data Handling |
| Performance Logging |
Performance Logging |
| Centralized Logging |
Centralized Logging |
| Distributed Tracing |
Distributed Tracing |
| Log Sampling (High-Volume Services) |
Log Sampling (High-Volume Services) |
Best Practices
✅ DO
- Use structured logging (JSON) in production
- Include correlation/request IDs in all logs
- Log at appropriate levels (don't overuse DEBUG)
- Redact sensitive data (PII, passwords, tokens)
- Include context (userId, requestId, etc.)
- Log errors with full stack traces
- Use centralized logging in distributed systems
- Set up log rotation to manage disk space
- Monitor log volume and costs
- Use async logging for performance
- Include timestamps in ISO 8601 format
- Log business events (user actions, transactions)
- Set up alerts for error patterns
❌ DON'T
- Log passwords, tokens, or sensitive data
- Use console.log in production
- Log at DEBUG level in production by default
- Log inside tight loops (use sampling)
- Include PII without anonymization
- Ignore log rotation (disk will fill up)
- Use synchronous logging in hot paths
- Log to multiple transports without need
- Forget to include error stack traces
- Log binary data or large objects
- Use string concatenation (use structured fields)
- Log every single request in high-volume APIs
1---
2name: logging-best-practices
3description: >
4 Implement structured logging with JSON formats, log levels (DEBUG, INFO, WARN,
5 ERROR), contextual logging, PII handling, and centralized logging. Use for
6 logging, observability, log levels, structured logs, or debugging.
7---
8
9# Logging Best Practices
10
11## Table of Contents
12
13- [Overview](#overview)
14- [When to Use](#when-to-use)
15- [Quick Start](#quick-start)
16- [Reference Guides](#reference-guides)
17- [Best Practices](#best-practices)
18
19## Overview
20
21Comprehensive guide to implementing structured, secure, and performant logging across applications. Covers log levels, structured logging formats, contextual information, PII protection, and centralized logging systems.
22
23## When to Use
24
25- Setting up application logging infrastructure
26- Implementing structured logging
27- Configuring log levels for different environments
28- Managing sensitive data in logs
29- Setting up centralized logging
30- Implementing distributed tracing
31- Debugging production issues
32- Compliance with logging regulations
33
34## Quick Start
35
36Minimal working example:
37
38```typescript
39// logger.ts
40enum LogLevel {
41 DEBUG = 0, // Detailed information for debugging
42 INFO = 1, // General informational messages
43 WARN = 2, // Warning messages, potentially harmful
44 ERROR = 3, // Error messages, application can continue
45 FATAL = 4, // Critical errors, application must stop
46}
47
48class Logger {
49 constructor(private minLevel: LogLevel = LogLevel.INFO) {}
50
51 debug(message: string, context?: object) {
52 if (this.minLevel <= LogLevel.DEBUG) {
53 this.log(LogLevel.DEBUG, message, context);
54 }
55 }
56
57 info(message: string, context?: object) {
58 if (this.minLevel <= LogLevel.INFO) {
59 this.log(LogLevel.INFO, message, context);
60 }
61 }
62
63 warn(message: string, context?: object) {
64// ... (see reference guides for full implementation)
65```
66
67## Reference Guides
68
69Detailed implementations in the `references/` directory:
70
71| Guide | Contents |
72|---|---|
73| [Log Levels](references/log-levels.md) | Log Levels |
74| [Structured Logging (JSON)](references/structured-logging-json.md) | Structured Logging (JSON) |
75| [Contextual Logging](references/contextual-logging.md) | Contextual Logging |
76| [PII and Sensitive Data Handling](references/pii-and-sensitive-data-handling.md) | PII and Sensitive Data Handling |
77| [Performance Logging](references/performance-logging.md) | Performance Logging |
78| [Centralized Logging](references/centralized-logging.md) | Centralized Logging |
79| [Distributed Tracing](references/distributed-tracing.md) | Distributed Tracing |
80| [Log Sampling (High-Volume Services)](references/log-sampling-high-volume-services.md) | Log Sampling (High-Volume Services) |
81
82## Best Practices
83
84### ✅ DO
85
86- Use structured logging (JSON) in production
87- Include correlation/request IDs in all logs
88- Log at appropriate levels (don't overuse DEBUG)
89- Redact sensitive data (PII, passwords, tokens)
90- Include context (userId, requestId, etc.)
91- Log errors with full stack traces
92- Use centralized logging in distributed systems
93- Set up log rotation to manage disk space
94- Monitor log volume and costs
95- Use async logging for performance
96- Include timestamps in ISO 8601 format
97- Log business events (user actions, transactions)
98- Set up alerts for error patterns
99
100### ❌ DON'T
101
102- Log passwords, tokens, or sensitive data
103- Use console.log in production
104- Log at DEBUG level in production by default
105- Log inside tight loops (use sampling)
106- Include PII without anonymization
107- Ignore log rotation (disk will fill up)
108- Use synchronous logging in hot paths
109- Log to multiple transports without need
110- Forget to include error stack traces
111- Log binary data or large objects
112- Use string concatenation (use structured fields)
113- Log every single request in high-volume APIs