How LogSentinelAI Handles Large Log Files with Chunked Processing
LogSentinelAI processes large log files by breaking them into fixed-size line chunks, ensuring constant memory usage regardless of file size through both realtime and batch streaming modes.
The call518/logsentinelai repository implements a memory-efficient architecture for log analysis that never loads entire files into RAM. By leveraging configurable chunked processing strategies, the library can analyze multi-gigabyte log files on resource-constrained systems while maintaining real-time monitoring capabilities.
Configuration Options for Chunked Processing
Chunk dimensions and behavior are centralized in src/logsentinelai/core/config.py, where values load from environment variables with sensible defaults.
# src/logsentinelai/core/config.py
REALTIME_CONFIG = {
"polling_interval": int(os.getenv("REALTIME_POLLING_INTERVAL", "5")),
"max_lines_per_batch": int(os.getenv("REALTIME_MAX_LINES_PER_BATCH", "50")),
"only_sampling_mode": os.getenv("REALTIME_ONLY_SAMPLING_MODE", "false").lower() == "true",
"sampling_threshold": int(os.getenv("REALTIME_SAMPLING_THRESHOLD", "100")),
"chunk_pending_timeout": int(os.getenv("REALTIME_CHUNK_PENDING_TIMEOUT", "1800"))
}
LOG_CHUNK_SIZES = {
"httpd_access": int(os.getenv("CHUNK_SIZE_HTTPD_ACCESS", "10")),
# ... additional log types
}
Key parameters include:
chunk_size— Controls the number of log lines per LLM request (e.g., 10 lines for Apache access logs).max_lines_per_batch— Caps the number of new lines read in a single poll to prevent memory spikes.chunk_pending_timeout— Forces emission of a partial chunk if the buffer sits idle for 1800 seconds (default).
Realtime Chunking with RealtimeLogMonitor
The RealtimeLogMonitor class in src/logsentinelai/core/monitoring.py implements tail-like monitoring that accumulates new lines in a pending buffer rather than loading the entire file.
Buffer Management and Sampling Logic
When new lines arrive via self._read_new_lines(), they append to self.pending_lines. The monitor supports two buffering strategies controlled by sampling_threshold and only_sampling_mode:
# src/logsentinelai/core/monitoring.py
if self.only_sampling_mode:
should_sample = True # always keep only the latest chunk
elif len(self.pending_lines) > self.sampling_threshold:
should_sample = True # auto-switch to sampling when buffer grows large
In sampling mode, older lines are discarded to retain only the newest chunk_size lines, preventing unbounded growth during high-volume bursts.
Timeout Handling and Chunk Emission
The monitor tracks self.pending_start_time when the first line enters the buffer. If the elapsed time exceeds chunk_pending_timeout, the current buffer flushes as a timeout chunk regardless of size.
Once the buffer contains at least chunk_size lines, the generator yields slices and resets the timer:
# part of get_new_log_chunks()
while len(self.pending_lines) >= self.chunk_size:
chunk = self.pending_lines[:self.chunk_size]
self.pending_lines = self.pending_lines[self.chunk_size:]
yield chunk
This guarantees that even a multi-gigabyte active log never exceeds the configured memory footprint defined by chunk_size.
Batch Streaming for Static Files
For historical analysis, src/logsentinelai/core/commons.py provides _process_file_streaming_batch, which performs a single-pass read with constant memory bounds.
Rotation Awareness and File Handling
The routine records initial inode and file size before streaming. Every 1,000 lines, it checks for inode changes via _check_file_rotation() and stops if the file grows beyond initial_size, ensuring integrity during log rotation events.
Memory-Bound Processing Loop
The function maintains a local chunk_buffer list that accumulates lines until reaching chunk_size:
# src/logsentinelai/core/commons.py
if len(chunk_buffer) >= chunk_size:
chunk_count += 1
_process_single_chunk_streaming(
chunk_buffer=chunk_buffer,
chunk_number=chunk_count,
# ...
)
total_lines_processed += len(chunk_buffer)
chunk_buffer = [] # free memory
After each chunk processes, the buffer clears immediately. A final emission handles any remaining lines at EOF. This design allows analysis of 10-GB files with memory usage capped at approximately chunk_size * average_line_length.
Reusable Chunking Utilities
The library includes a generic generator in src/logsentinelai/utils/general.py for ad-hoc chunking of any iterable:
# src/logsentinelai/utils/general.py
def chunked_iterable(iterable, size, debug=False):
"""Yield successive chunks (lists) from *iterable*."""
chunk = []
for item in iterable:
chunk.append(f"{item.rstrip()}\n")
if len(chunk) == size:
if debug:
pass # optional debug prints
yield chunk
chunk = []
if chunk:
if debug:
pass # optional debug prints
yield chunk
This utility underpins both realtime and batch pipelines whenever safe slicing is required.
Implementation Examples
Realtime Monitoring Example
Monitor a live Apache access log with automatic chunking:
from logsentinelai.core.monitoring import create_realtime_monitor
# Create a realtime monitor for the Apache access log
monitor = create_realtime_monitor(
log_type="httpd_access",
remote_mode="local", # or "ssh"
ssh_config=None,
remote_log_path=None
)
# Consume chunks indefinitely (Ctrl-C to stop)
for chunk in monitor.get_new_log_chunks():
# `chunk` is a list of up to `monitor.chunk_size` log lines
print(f"🔹 Received chunk of {len(chunk)} lines")
# ... send to LLM, store, alert, etc.
Source references:
- Factory function:
monitoring.pylines 58-74 - Chunk generation logic:
monitoring.pylines 300-430
Batch Processing Example
Stream a 10-million-line file without loading it entirely:
from logsentinelai.core import run_generic_batch_analysis
from logsentinelai.analyzers.httpd_access import HttpdAccessSchema, httpd_access_prompt
# Run a streaming batch analysis on a 10-million-line file
run_generic_batch_analysis(
log_type="httpd_access",
analysis_schema_class=HttpdAccessSchema,
prompt_template=httpd_access_prompt,
analysis_title="Apache Access Log Overview",
chunk_size=5000, # 5k lines per request
model=my_llm, # any LLM wrapper that follows the API
response_language="english"
)
Source references:
- Batch driver:
core/__init__.pyline 18 - Streaming implementation:
commons.pylines 966-1010
Generic Chunking Helper
Process any file-like object with the low-level utility:
from logsentinelai.utils import chunked_iterable
lines = open("huge.log", "r", encoding="utf-8")
for block in chunked_iterable(lines, size=2000, debug=True):
# `block` is a list of 2000 log lines (each ending with "\n")
process(block) # send to LLM, write to DB, etc.
Source reference: general.py lines 12-42
Summary
- LogSentinelAI avoids loading entire log files into memory by using fixed-size line chunks.
- Realtime monitoring employs a pending buffer with configurable timeouts and sampling modes to handle high-volume streams.
- Batch processing reads files line-by-line in
src/logsentinelai/core/commons.py, clearing buffers after each chunk to maintain constant memory usage. - Rotation detection safeguards batch analysis by monitoring inode changes and file size during streaming.
- The
chunked_iterableutility insrc/logsentinelai/utils/general.pyprovides a reusable generator for any iterable chunking needs.
Frequently Asked Questions
What is the default chunk size in LogSentinelAI?
Default chunk sizes vary by log type and are defined in src/logsentinelai/core/config.py. For example, Apache access logs default to 10 lines per chunk via the CHUNK_SIZE_HTTPD_ACCESS environment variable (default value "10"). You can override these defaults by setting the corresponding environment variables before initializing the monitor or batch processor.
How does LogSentinelAI prevent memory exhaustion during realtime monitoring?
The RealtimeLogMonitor class prevents memory exhaustion through sampling mode and buffer limits. When only_sampling_mode is enabled or the sampling_threshold (default 100 lines) is exceeded, the system discards older lines and retains only the newest chunk_size lines. Additionally, max_lines_per_batch caps the number of lines read per poll cycle, ensuring the pending buffer never grows unbounded.
Can LogSentinelAI detect log rotation during batch processing?
Yes. The _process_file_streaming_batch function in src/logsentinelai/core/commons.py detects log rotation by comparing the current file inode and size against initial values every 1,000 lines. If the inode changes or the file size decreases below the recorded initial_size, the processor recognizes the rotation and stops reading to avoid analyzing stale or incorrect data.
What is the difference between sampling mode and standard buffering in realtime processing?
Standard buffering accumulates all new lines in pending_lines until chunk_size is reached or a timeout occurs, preserving every log entry. Sampling mode (triggered by only_sampling_mode=True or exceeding sampling_threshold) discards older lines to keep only the most recent chunk_size lines. This trade-off prioritizes recency over completeness during extreme log volume spikes.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →