Conversation
|
LGTM |
|
Sorry for reopening this but the logger introduces some latency. |
|
No worries at all, really appreciate you bringing it up! Yeah good call on the logger latency. I was thinking might be worth trying a few things: |
|
That sounds really good! |
|
Great let me work on it, I'll also dig into how much time send() is actually taking in the current setup. If it's a noticeable source of latency, I’ll explore optimizing around the standard library's channel without pulling in any external crates. This project is pretty unique and lean, so I want to keep it tight and dependency-free. I’ll get back to you with what I find! |
Logging System Integration for HTeaPot
This pull request introduces a comprehensive logging system for the HTeaPot web server. The system provides structured, component-based logging with performance metrics, millisecond-level precision, and robust concurrency all implemented without any external dependencies.
Key Features
Log Levels
Five distinct log levels:
DEBUG,INFO,WARN,ERROR, andFATAL, with built-in filtering capabilities.Component-Based Logging
Enables creation of multiple loggers for different components, sharing the same output stream but maintaining component-specific labels.
Millisecond Precision
Timestamps include millisecond resolution, providing more precise insights into system behavior.
Performance Metrics
Logging includes timing data for request handling, cache hits/misses, and proxy operations.
ISO-8601 Compatible Timestamps
All timestamps follow the format
YYYY-MM-DD HH:MM:SS.mmmfor clarity and standardization.Thread-Safe Implementation
Concurrency is handled safely using
Arcand channels, ensuring high throughput without locking the main thread.Adaptive Buffer Sizing
Logging buffers scale dynamically based on runtime activity and log volume.
Implementation Details
Logger Structure
The logger uses a two-tiered architecture:
LoggerCore
Manages the shared channel and the background worker thread, wrapped in an
Arcto enable shared ownership.Logger
A lightweight front-end with component context that delegates log writing to
LoggerCore.This modular design allows each subsystem to have a specialized logger, improving log clarity while maintaining resource efficiency and thread safety.
Log Message Format
All logs follow this format:
YYYY-MM-DD HH:MM:SS.mmm [LEVEL] [component] Message content
Example:
2025-04-10 07:50:01.135 [INFO] [main] Server started at http://localhost:8081/
2025-04-10 07:50:22.892 [WARN] [http] Path /favicon.ico does not exist
Performance Considerations
Batched Writing
Log messages are buffered and written in batches to reduce I/O overhead.
Adaptive Buffer Sizing
The system dynamically resizes internal buffers depending on log traffic.
Timeout-Based Flushing
All logs are flushed at least once per second, ensuring timely delivery.
Non-Blocking Design
Logging never blocks the main application thread. Backpressure is handled internally.
Usage in HTeaPot
The main application now uses distinct loggers for each core component:
main: General server lifecycle logshttp: HTTP server request handlingproxy: DNS and proxy communicationcache: Caching system activityThis separation enhances traceability and sets the foundation for future fine-grained log filtering.
Performance Metrics
The logger also collects and reports timing data for:
These metrics are helpful for profiling server behavior and identifying performance bottlenecks.
Migration Notes
All existing
logger.msg()calls have been migrated to structured logging methods likelogger.info(),logger.warn(), etc. The update preserves compatibility with previous log configurations.Future Improvements
Possible enhancements under consideration:
Sample Output