Why Application Logs Are Missing or Delayed and How to Fix It
In this article
Application logs can be missing or delayed due to buffering, asynchronous shipping, timezone mismatches, log rotation races, or misconfigured agents—and the fix usually involves adjusting flush intervals, checking clocks, and validating the shipping pipeline. Below, we break down the common causes, how logging actually works, and step-by-step solutions you can apply today.
How Logging Pipelines Work
Most modern applications don't write logs directly to a file or console; they send them through a pipeline: the app emits a log record → the logging library formats it → a buffer holds it → an agent or shipper forwards it → the central collector stores it. Each stage can introduce delay or loss. For example, a common setup is: Python's logging module → FileHandler → Fluentd → Elasticsearch. If any component buffers aggressively, logs appear late. Similarly, containerized apps often write to stdout, which the runtime captures and forwards—if the container's logging driver is set to json-file with a large max-size, logs stay in the file until rotation.
Common Causes of Missing Logs
- Log rotation races: The application writes to a file while the rotation script renames it. The app keeps writing to the old inode, so new logs go to a file that's no longer being read.
- Timezone mismatches: If the app logs in UTC but the collector expects local time, logs may appear to be delayed or sorted incorrectly.
- Buffering in the library: Many logging frameworks (e.g., Python's
logging.handlers.BufferingHandler) hold records in memory until a threshold is reached. If the app crashes before flush, logs are lost. - Agent misconfiguration: A shipping agent like Filebeat or Fluent Bit may have a
close_inactivesetting that stops reading a file after a period of inactivity, missing late writes. - Network issues: TCP backpressure or retries can delay delivery. If the collector is down, logs queue locally—but only up to a limit.
- Clock skew: If the app server and the collector have different system times, a log timestamped in the future may be rejected or sorted oddly, appearing missing.
How to Diagnose Delayed Logs
- Check the application's logging configuration: look for
flush,buffer,async, orbatchsettings. - Inspect the shipping agent's status: most agents have a
healthorstatuscommand to show how many events are pending. - Compare timestamps: run
dateon the app server and the collector. If they differ by more than a minute, fix with NTP. - Look at the raw log file: if the file has entries but the collector doesn't, the issue is in shipping. If the file is empty, the issue is in the app.
- Test the network path: use
nc -vz collector 514or similar to check connectivity.
Fixing Buffering and Flush Delays
For Python, use logging.handlers.MemoryHandler with a small capacity or set flushLevel to logging.ERROR so critical logs are flushed immediately. In Java, Logback's AsyncAppender has queueSize and discardingThreshold—set discardingThreshold to 0 to avoid dropping logs when the queue is full. For Node.js, the pino library has a sync option; set it to true for critical logs. In general, reduce batch sizes and flush intervals: in Filebeat, set flush_bytes to 1024 and flush_interval to 1s. In Fluent Bit, lower Buffer_Chunk_Size and Buffer_Max_Size.
Handling Log Rotation Correctly
Use a copytruncate strategy: copy the log file, then truncate the original, so the app keeps writing to the same inode. In logrotate, add copytruncate to the config. Alternatively, use a logging library that supports file reopening, like Python's TimedRotatingFileHandler with delay=True. Ensure the shipping agent is configured to follow the file by name, not inode—Filebeat's ignore_older should be set to a value that doesn't skip the rotated file. Test rotation with a script that writes logs continuously and verify no gaps.
When Logs Are Lost During Shutdown or Crash
If the app crashes, buffered logs are lost. To minimize this, use synchronous logging for critical events or implement a atexit hook that flushes buffers. In Python, logging.shutdown() is called automatically, but if the process is killed with SIGKILL, it won't run. Consider using a dedicated log shipper that reads from the file system rather than relying on in-memory buffers. For example, write logs to a file with flush=True on every write for critical paths, or use a systemd service with StandardOutput=append:/var/log/app.log to capture stdout/stderr directly.
Comparing Local vs. Remote Logging
Local logging (writing to a file on the same machine) is simple and fast, but if the disk fills or the app crashes, logs can be lost. Remote logging (sending to a central server) adds resilience but introduces network latency and potential for loss. A hybrid approach: write to a local file with synchronous flush, and have an agent ship it asynchronously. This gives you both durability and centralization. For high-throughput systems, consider using a message queue like Kafka as a buffer between the app and the collector—this decouples the producer from the consumer and reduces the chance of loss due to backpressure.
Frequently Asked Questions
Why are my logs delayed by exactly 5 minutes?
That sounds like a batch flush interval. Many logging agents (e.g., Filebeat) have a default flush_interval of 5s, but if you've set it higher, logs will be held that long. Check the agent's config for flush_interval or batch_timeout.
Can timezone differences cause logs to appear missing?
Yes. If your app logs in UTC but your search UI displays local time, logs from late evening may appear as the next day. This isn't actual loss, but it looks like a gap. Always store logs in UTC and convert at display time.
What is the best way to ensure no logs are lost?
There's no absolute guarantee, but you can minimize loss by using synchronous writes for critical logs, a reliable shipping agent with persistent queues (like Filebeat's queue.mem or queue.spool), and a disk buffer. Also, monitor the agent's dropped events counter.
Why do logs stop after a log rotation?
This is usually because the app continues writing to the old inode after rotation. Use copytruncate or configure the app to reopen its log file on a signal (e.g., SIGUSR1 for many daemons).
How do I fix delayed logs in Docker?
Docker's default json-file logging driver buffers logs in a file. Set max-size to a small value (e.g., 10m) and max-file to 3 to force rotation. Alternatively, use the local driver or a logging driver that sends directly to a collector.
Is it safe to set flush interval to 0?
Setting flush interval to 0 means flush on every write, which can severely impact performance for high-throughput apps. It's better to flush on a threshold or on error level. For critical logs, use a separate logger with synchronous flush.