Logging Testing — Testing Log Output and Logging Behavior
DodaTech
Updated 2026-06-28
1 min read
In this tutorial, you'll learn about Logging Testing. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
Testing logging behavior ensures that applications emit the correct log entries with proper context and formatting.
// Test log capture utility
class TestLogCapture {
constructor() {
this.entries = [];
this.originalTransport = null;
}
attach(logger) {
const transport = new winston.transports.Stream({
stream: new require('stream').Writable({
write: (chunk, encoding, callback) => {
this.entries.push(JSON.parse(chunk.toString()));
callback();
}
})
});
logger.add(transport);
this.originalTransport = transport;
}
detach(logger) {
if (this.originalTransport) {
logger.remove(this.originalTransport);
}
}
getEntries() { return this.entries; }
getErrors() { return this.entries.filter(e => e.level === 'ERROR'); }
contains(message) { return this.entries.some(e => e.message.includes(message)); }
findByCorrelationId(id) { return this.entries.filter(e => e.correlationId === id); }
findByType(type) { return this.entries.filter(e => e.type === type); }
clear() { this.entries = []; }
}
// Test example
describe('ScanController logging', () => {
let capture;
let logger;
beforeEach(() => {
capture = new TestLogCapture();
capture.attach(logger);
});
afterEach(() => {
capture.detach(logger);
});
it('should log scan initiation', async () => {
await request(app)
.post('/api/scans')
.send({ fileId: 'test-123' })
.expect(201);
const logEntry = capture.findByType('scan.initiated');
expect(logEntry).toHaveLength(1);
expect(logEntry[0].message).toContain('test-123');
expect(logEntry[0].userId).toBeDefined();
expect(logEntry[0].correlationId).toBeDefined();
});
it('should log errors with stack traces', async () => {
await request(app)
.post('/api/scans')
.send({})
.expect(400);
const errors = capture.getErrors();
expect(errors.length).toBeGreaterThan(0);
expect(errors[0].stack).toBeDefined();
});
it('should not log sensitive data', async () => {
await request(app).post('/auth/login')
.send({ email: 'test@example.com', password: 'supersecret' });
const logs = capture.getEntries();
const logString = JSON.stringify(logs);
expect(logString).not.toContain('supersecret');
});
});
Testing log output ensures logging reliability and prevents accidental sensitive data exposure in production logs.
← Previous
Log Retention Policies — Defining and Implementing Log Retention
Next →
Logging in Containers — Best Practices for Container Logging
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro