How to Choose a Log Level Strategy
Assign levels by what an operator does with the line, keep structured fields, and control volume so logs stay useful and affordable.
On this page
Introduction
A log level is not a measure of how much you care about a line; it is a promise about what an operator does with it. Error means someone should look now; warn means something is off but the service continues; info means a normal event worth recording; debug means a detail used while diagnosing. Assigning levels by that operational meaning, rather than by the author’s mood, is what makes the levels filterable and the logs useful. This article covers the meaning of each level, the cost of a log line, structured fields, sampling and how to keep production logs affordable.
What Levels Are For
Levels exist so an operator can select the volume of detail they need: errors only during normal operation, more detail while investigating an incident. For that selection to work, the level must reflect the operational significance, not the author’s importance. A line logged at error must be one that warrants attention; a line logged at info must be one that can be ignored by default.
The common failure is level inflation, where routine events are logged at error or warn, so an operator filtering on error sees hundreds of lines and stops filtering. Deflation is the opposite: a genuine problem logged at info so it is invisible until someone happens to look. Both break the selection, and both come from assigning levels by feeling.
The Cost of a Log Line
Logging is not free. Each line costs CPU to format, I/O to write or network to ship, storage to retain, and index space to make searchable. In a high-throughput service, a verbose log line in a hot path can cost more than the work it describes, and the volume can overwhelm the log pipeline and push out the lines that matter.
The cost is why the level has to gate by default: production runs at info or above, debug is enabled per-service or per-request when needed. A log line that would fire a million times a minute should not be at info; it should be at debug or sampled. Deciding the level is partly deciding the volume.
What to Log at Each Level
Error: an operation failed and a human should investigate, including the error and enough context to find the request. Warn: something unexpected happened but was handled, such as a retry that succeeded or a fallback taken. Info: a significant normal event, such as a service start or a state transition. Debug: fine-grained detail for diagnosis, off by default in production.
The test is operational: if an operator would want to be notified, it is error; if they would want to see it when investigating, it is info; if they would only want it while debugging, it is debug. Apply the test consistently, and the levels become a reliable filter.
const pino = require('pino');
// Structured logger with explicit serializers and redaction.
const log = pino({
level: process.env.LOG_LEVEL || 'info',
redact: ['req.headers.authorization', 'user.password', '*.token'],
base: { service: 'orders-api', version: process.env.GIT_SHA },
timestamp: pino.stdTimeFunctions.isoTime,
});
// error: an operator should act. Include the error and correlation id.
log.error({ err, requestId, orderId }, 'failed to charge card');
// warn: handled, but noteworthy. The request continues.
log.warn({ requestId, attempt: 2 }, 'retrying upstream after timeout');
// info: a significant normal event.
log.info({ requestId, orderId }, 'order confirmed');
// debug: detail for diagnosis, excluded by default in production.
log.debug({ requestId, payloadKeys: Object.keys(body) }, 'validated request body');
Structured Logs and Fields
A log line should be structured, with fields rather than a sentence, so it can be queried and aggregated. Include the identifiers that connect it to a request and a user, the outcome, and the duration, and put values in fields rather than in the message so no one has to parse them back out. The message describes the event; the fields carry the data.
Consistency across services makes the logs searchable as one dataset. Use the same field names for the request id, the user, the error and the duration, and derive them from the tracing context so a log line and a span share the same identifiers. This is what lets an investigation move from a metric to a trace to the log lines of the failing request.
Sampling and Volume Control
When a log line would fire at high volume, sample it: log every hundredth occurrence, or all occurrences for a failing request and none for a successful one. Sampling keeps the signal without the volume, at the cost of not seeing every event. For request logs, log errors and slow requests in full and sample the rest, so the interesting lines are never lost.
Use a global log level that production can raise without a deploy, so during an incident an operator can enable debug for a specific service and then turn it off. Bounding the change in time and scope prevents the debug logs from becoming a permanent cost, and keeps them available exactly when they are needed.
// Sample high-volume lines, but always log the interesting cases in full.
const SAMPLE_EVERY = 100;
let counter = 0;
function logRequest({ requestId, status, durationMs }) {
const slow = durationMs > 500;
const failed = status >= 500;
if (failed) {
log.error({ requestId, status, durationMs }, 'request failed'); // always
return;
}
if (slow) {
log.warn({ requestId, status, durationMs }, 'slow request'); // always
return;
}
if (++counter % SAMPLE_EVERY === 0) {
log.info({ requestId, status, sampled: true }, 'request served'); // 1 in N
}
}
Production Versus Development
Production runs at info or above, writes structured lines to the transport the pipeline expects, and never logs secrets or personal data. Development runs at debug, writes human-readable lines, and prints everything, because the volume does not matter and the detail does. The configuration differs, but the code that emits the lines is the same.
The one rule that must hold in both is not to log sensitive data. Redaction at the logger boundary is safer than relying on every call site to omit a field, because a single missed field leaks a credential or a personal datum into a log aggregator that many people can read. Redact by field name and by pattern, and test that a known secret does not appear in the output.
Conclusion
Choose log levels by what an operator does with the line: error to act, warn when handled, info for significant normal events, debug for diagnosis. Log structured fields so lines are queryable, use consistent field names across services, sample high-volume lines, and let production raise the level per service during an incident. Redact sensitive data at the logger. Then the logs are affordable, filterable, and useful exactly when someone is trying to understand what happened.
Sources: pino logger, OpenTelemetry: logs data model, OWASP: Logging Cheat Sheet.
FAQ
- What belongs at warn versus error?
- error means a human should investigate because an operation failed; warn means something unexpected happened but was handled, such as a retry that succeeded. If the service continues and no one needs to act, it is warn, not error.
- How do I keep high-volume logs affordable?
- Sample them: log every nth occurrence, or log all occurrences for failures and slow requests and sample the successful ones. Gate detail by level so production runs at info, and enable debug per service only during an investigation.
Report a problem with this articleOpens a pre-filled issue on GitHub.