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