Structured JSON Logging — Implementing Structured Logging in Backend Apps
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you'll learn about Structured Json Logging. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Structured logging outputs machine-parseable JSON log entries with consistent fields for effective querying in log aggregation systems.
// Structured logger implementation
const logSchema = {
timestamp: new Date().toISOString(),
level: 'info',
logger: 'scan-service',
correlationId: req.correlationId,
userId: req.user?.id,
message: 'Scan completed successfully',
context: {
scanId: scan.id,
fileSize: scan.fileSize,
duration: scan.durationMs,
threatsFound: scan.threats.length
},
service: {
name: config.serviceName,
version: config.version,
environment: config.environment,
host: os.hostname()
},
trace: {
traceId: req.traceId,
spanId: req.spanId
}
};
// Production logger
const logger = pino({
level: config.logLevel,
redact: ['req.headers.authorization', 'req.body.password', 'user.email'],
serializers: {
req: (req) => ({
method: req.method,
url: req.url,
correlationId: req.correlationId
}),
err: pino.stdSerializers.err
},
formatters: {
level: (label) => ({ level: label.toUpperCase() }),
bindings: () => ({ pid: process.pid, host: os.hostname(), service: config.serviceName })
}
});
Structured logging transforms logs from text files into queryable data for debugging and analysis.
← Previous
Observability Project: Building a Complete Logging and Monitoring System
Next →
Log Aggregation — Centralizing Logs with Aggregation Systems
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro