|
Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
|
Real-time log analyzer with anomaly detection. More...
#include <realtime_log_analyzer.h>

Classes | |
| struct | statistics |
| Get current statistics. More... | |
| struct | timestamped_entry |
Public Types | |
| using | anomaly_callback = std::function<void(const anomaly_event&)> |
| Callback type for anomaly notifications. | |
Public Member Functions | |
| realtime_log_analyzer ()=default | |
| Default constructor. | |
| realtime_log_analyzer (const realtime_analysis_config &config) | |
| Constructor with configuration. | |
| void | set_anomaly_callback (anomaly_callback cb) |
| Set the anomaly callback. | |
| void | analyze (const analyzed_log_entry &entry) |
| Analyze a log entry in real-time. | |
| void | set_error_spike_threshold (size_t errors_per_minute) |
| Set error spike threshold. | |
| void | add_pattern_alert (const std::string &pattern, log_level min_level) |
| Add a pattern-based alert. | |
| bool | remove_pattern_alert (const std::string &pattern) |
| Remove a pattern alert. | |
| void | clear_pattern_alerts () |
| Clear all pattern alerts. | |
| void | set_rate_thresholds (size_t high_threshold, size_t low_threshold=0) |
| Set rate anomaly thresholds. | |
| void | set_track_new_errors (bool enable) |
| Enable or disable new error tracking. | |
| double | get_error_rate () const |
| Get current error rate (errors per minute) | |
| double | get_log_rate () const |
| Get current log rate (logs per minute) | |
| statistics | get_statistics () const |
| void | reset () |
| Reset all statistics and tracked state. | |
| const realtime_analysis_config & | get_config () const |
| Get the configuration. | |
| void | set_config (const realtime_analysis_config &config) |
| Set the configuration. | |
Private Member Functions | |
| void | add_to_window (const analyzed_log_entry &entry, std::chrono::system_clock::time_point now) |
| double | calculate_rate (const std::deque< timestamped_entry > &window) const |
| void | check_error_spike (const analyzed_log_entry &entry, std::chrono::system_clock::time_point now) |
| void | check_pattern_alerts (const analyzed_log_entry &entry, std::chrono::system_clock::time_point now) |
| void | check_rate_anomaly (std::chrono::system_clock::time_point now) |
| void | check_new_error_type (const analyzed_log_entry &entry, std::chrono::system_clock::time_point now) |
| void | collect_related_entries (anomaly_event &event, const std::deque< timestamped_entry > &window) const |
| void | notify_anomaly (const anomaly_event &event) |
Static Private Member Functions | |
| static void | cleanup_window (std::deque< timestamped_entry > &window, std::chrono::system_clock::time_point cutoff) |
| static std::string | normalize_error_message (const std::string &message) |
Private Attributes | |
| realtime_analysis_config | config_ |
| anomaly_callback | callback_ |
| std::shared_mutex | callback_mutex_ |
| std::deque< timestamped_entry > | log_window_ |
| std::deque< timestamped_entry > | error_window_ |
| std::deque< double > | baseline_rates_ |
| std::shared_mutex | window_mutex_ |
| std::vector< pattern_alert > | patterns_ |
| std::shared_mutex | patterns_mutex_ |
| std::unordered_set< std::string > | known_errors_ |
| std::shared_mutex | errors_mutex_ |
| std::chrono::system_clock::time_point | last_rate_check_ |
| std::chrono::system_clock::time_point | last_spike_alert_ |
| std::shared_mutex | rate_limit_mutex_ |
| std::atomic< size_t > | total_analyzed_ {0} |
| std::atomic< size_t > | total_errors_ {0} |
| std::atomic< size_t > | anomalies_detected_ {0} |
| std::atomic< size_t > | error_spikes_ {0} |
| std::atomic< size_t > | pattern_matches_ {0} |
| std::atomic< size_t > | rate_anomalies_ {0} |
| std::atomic< size_t > | new_error_types_ {0} |
| std::shared_mutex | stats_mutex_ |
Real-time log analyzer with anomaly detection.
The realtime_log_analyzer class provides real-time analysis of log entries during the logging process. Unlike the post-hoc log_analyzer, this class is designed to detect anomalies as they occur and trigger callbacks for immediate alerting.
Key features:
Definition at line 138 of file realtime_log_analyzer.h.
| using kcenon::logger::analysis::realtime_log_analyzer::anomaly_callback = std::function<void(const anomaly_event&)> |
Callback type for anomaly notifications.
Definition at line 143 of file realtime_log_analyzer.h.
|
default |
Default constructor.
|
inlineexplicit |
Constructor with configuration.
| config | Analysis configuration |
Definition at line 154 of file realtime_log_analyzer.h.
|
inline |
Add a pattern-based alert.
| pattern | Regex pattern to match against log messages |
| min_level | Minimum log level for this pattern to trigger |
Definition at line 219 of file realtime_log_analyzer.h.
References patterns_, and patterns_mutex_.
|
inlineprivate |
Definition at line 367 of file realtime_log_analyzer.h.
References cleanup_window(), config_, error_window_, kcenon::logger::analysis::analyzed_log_entry::level, log_window_, total_analyzed_, total_errors_, kcenon::logger::analysis::realtime_analysis_config::window_duration, and window_mutex_.
Referenced by analyze().


|
inline |
Analyze a log entry in real-time.
| entry | The log entry to analyze |
This method should be called for each log entry during logging. It performs all configured detection checks and may trigger the anomaly callback.
Thread-safe: Multiple threads can call this method concurrently.
Definition at line 178 of file realtime_log_analyzer.h.
References add_to_window(), check_error_spike(), check_new_error_type(), check_pattern_alerts(), check_rate_anomaly(), config_, kcenon::logger::analysis::realtime_analysis_config::enable_rate_anomaly_detection, kcenon::logger::analysis::analyzed_log_entry::level, and kcenon::logger::analysis::realtime_analysis_config::track_new_errors.

|
inlineprivate |
Definition at line 400 of file realtime_log_analyzer.h.
References config_, and kcenon::logger::analysis::realtime_analysis_config::window_duration.
Referenced by check_error_spike(), check_rate_anomaly(), get_error_rate(), and get_log_rate().

|
inlineprivate |
Definition at line 410 of file realtime_log_analyzer.h.
References kcenon::logger::analysis::anomaly_event::anomaly_type, calculate_rate(), collect_related_entries(), config_, kcenon::logger::analysis::anomaly_event::error_spike, kcenon::logger::analysis::realtime_analysis_config::error_spike_threshold, error_spikes_, error_window_, last_spike_alert_, notify_anomaly(), rate_limit_mutex_, and window_mutex_.
Referenced by analyze().


|
inlineprivate |
Definition at line 534 of file realtime_log_analyzer.h.
References kcenon::logger::analysis::anomaly_event::anomaly_type, errors_mutex_, known_errors_, kcenon::logger::analysis::analyzed_log_entry::message, kcenon::logger::analysis::anomaly_event::new_error_type, new_error_types_, normalize_error_message(), and notify_anomaly().
Referenced by analyze().


|
inlineprivate |
Definition at line 454 of file realtime_log_analyzer.h.
References kcenon::logger::analysis::anomaly_event::anomaly_type, kcenon::logger::analysis::analyzed_log_entry::level, kcenon::logger::analysis::analyzed_log_entry::message, notify_anomaly(), kcenon::logger::analysis::anomaly_event::pattern_match, pattern_matches_, patterns_, and patterns_mutex_.
Referenced by analyze().


|
inlineprivate |
Definition at line 483 of file realtime_log_analyzer.h.
References kcenon::logger::analysis::anomaly_event::anomaly_type, calculate_rate(), config_, last_rate_check_, log_window_, notify_anomaly(), rate_anomalies_, kcenon::logger::analysis::anomaly_event::rate_anomaly, kcenon::logger::analysis::realtime_analysis_config::rate_anomaly_high_threshold, kcenon::logger::analysis::realtime_analysis_config::rate_anomaly_low_threshold, rate_limit_mutex_, total_analyzed_, and window_mutex_.
Referenced by analyze().


|
inlinestaticprivate |
Definition at line 393 of file realtime_log_analyzer.h.
Referenced by add_to_window().

|
inline |
Clear all pattern alerts.
Definition at line 245 of file realtime_log_analyzer.h.
References patterns_, and patterns_mutex_.
|
inlineprivate |
Definition at line 579 of file realtime_log_analyzer.h.
References config_, and kcenon::logger::analysis::realtime_analysis_config::max_related_entries.
Referenced by check_error_spike().

|
inline |
Get the configuration.
Definition at line 349 of file realtime_log_analyzer.h.
References config_.
|
inline |
Get current error rate (errors per minute)
Definition at line 272 of file realtime_log_analyzer.h.
References calculate_rate(), error_window_, and window_mutex_.
Referenced by get_statistics().


|
inline |
Get current log rate (logs per minute)
Definition at line 281 of file realtime_log_analyzer.h.
References calculate_rate(), log_window_, and window_mutex_.
Referenced by get_statistics().


|
inline |
Definition at line 302 of file realtime_log_analyzer.h.
References kcenon::logger::analysis::realtime_log_analyzer::statistics::anomalies_detected, anomalies_detected_, kcenon::logger::analysis::realtime_log_analyzer::statistics::current_error_rate, kcenon::logger::analysis::realtime_log_analyzer::statistics::current_log_rate, kcenon::logger::analysis::realtime_log_analyzer::statistics::error_spikes, error_spikes_, get_error_rate(), get_log_rate(), kcenon::logger::analysis::realtime_log_analyzer::statistics::new_error_types, new_error_types_, kcenon::logger::analysis::realtime_log_analyzer::statistics::pattern_matches, pattern_matches_, kcenon::logger::analysis::realtime_log_analyzer::statistics::rate_anomalies, rate_anomalies_, stats_mutex_, kcenon::logger::analysis::realtime_log_analyzer::statistics::total_analyzed, total_analyzed_, kcenon::logger::analysis::realtime_log_analyzer::statistics::total_errors, and total_errors_.

|
inlinestaticprivate |
Definition at line 562 of file realtime_log_analyzer.h.
Referenced by check_new_error_type().

|
inlineprivate |
Definition at line 589 of file realtime_log_analyzer.h.
References anomalies_detected_, callback_, and callback_mutex_.
Referenced by check_error_spike(), check_new_error_type(), check_pattern_alerts(), and check_rate_anomaly().

|
inline |
Remove a pattern alert.
| pattern | The pattern to remove |
Definition at line 229 of file realtime_log_analyzer.h.
References patterns_, and patterns_mutex_.
|
inline |
Reset all statistics and tracked state.
Definition at line 320 of file realtime_log_analyzer.h.
References anomalies_detected_, baseline_rates_, error_spikes_, error_window_, errors_mutex_, known_errors_, last_rate_check_, last_spike_alert_, log_window_, new_error_types_, pattern_matches_, rate_anomalies_, stats_mutex_, total_analyzed_, total_errors_, and window_mutex_.
|
inline |
Set the anomaly callback.
| cb | Callback function to invoke when anomaly is detected |
The callback is invoked synchronously when an anomaly is detected. For non-blocking operation, the callback should dispatch to a separate thread.
Definition at line 164 of file realtime_log_analyzer.h.
References callback_, and callback_mutex_.
|
inline |
Set the configuration.
| config | New configuration |
Definition at line 357 of file realtime_log_analyzer.h.
References config_.
|
inline |
Set error spike threshold.
| errors_per_minute | Number of errors per minute to trigger alert |
Definition at line 210 of file realtime_log_analyzer.h.
References config_, and kcenon::logger::analysis::realtime_analysis_config::error_spike_threshold.
|
inline |
Set rate anomaly thresholds.
| high_threshold | High rate threshold (logs per minute) |
| low_threshold | Low rate threshold (logs per minute), 0 to disable |
Definition at line 255 of file realtime_log_analyzer.h.
References config_, kcenon::logger::analysis::realtime_analysis_config::rate_anomaly_high_threshold, and kcenon::logger::analysis::realtime_analysis_config::rate_anomaly_low_threshold.
|
inline |
Enable or disable new error tracking.
| enable | true to enable, false to disable |
Definition at line 264 of file realtime_log_analyzer.h.
References config_, and kcenon::logger::analysis::realtime_analysis_config::track_new_errors.
|
private |
Definition at line 627 of file realtime_log_analyzer.h.
Referenced by get_statistics(), notify_anomaly(), and reset().
|
private |
Definition at line 608 of file realtime_log_analyzer.h.
Referenced by reset().
|
private |
Definition at line 602 of file realtime_log_analyzer.h.
Referenced by notify_anomaly(), and set_anomaly_callback().
|
mutableprivate |
Definition at line 603 of file realtime_log_analyzer.h.
Referenced by notify_anomaly(), and set_anomaly_callback().
|
private |
Definition at line 599 of file realtime_log_analyzer.h.
Referenced by add_to_window(), analyze(), calculate_rate(), check_error_spike(), check_rate_anomaly(), collect_related_entries(), get_config(), set_config(), set_error_spike_threshold(), set_rate_thresholds(), and set_track_new_errors().
|
private |
Definition at line 628 of file realtime_log_analyzer.h.
Referenced by check_error_spike(), get_statistics(), and reset().
|
private |
Definition at line 607 of file realtime_log_analyzer.h.
Referenced by add_to_window(), check_error_spike(), get_error_rate(), and reset().
|
mutableprivate |
Definition at line 617 of file realtime_log_analyzer.h.
Referenced by check_new_error_type(), and reset().
|
private |
Definition at line 616 of file realtime_log_analyzer.h.
Referenced by check_new_error_type(), and reset().
|
private |
Definition at line 620 of file realtime_log_analyzer.h.
Referenced by check_rate_anomaly(), and reset().
|
private |
Definition at line 621 of file realtime_log_analyzer.h.
Referenced by check_error_spike(), and reset().
|
private |
Definition at line 606 of file realtime_log_analyzer.h.
Referenced by add_to_window(), check_rate_anomaly(), get_log_rate(), and reset().
|
private |
Definition at line 631 of file realtime_log_analyzer.h.
Referenced by check_new_error_type(), get_statistics(), and reset().
|
private |
Definition at line 629 of file realtime_log_analyzer.h.
Referenced by check_pattern_alerts(), get_statistics(), and reset().
|
private |
Definition at line 612 of file realtime_log_analyzer.h.
Referenced by add_pattern_alert(), check_pattern_alerts(), clear_pattern_alerts(), and remove_pattern_alert().
|
mutableprivate |
Definition at line 613 of file realtime_log_analyzer.h.
Referenced by add_pattern_alert(), check_pattern_alerts(), clear_pattern_alerts(), and remove_pattern_alert().
|
private |
Definition at line 630 of file realtime_log_analyzer.h.
Referenced by check_rate_anomaly(), get_statistics(), and reset().
|
mutableprivate |
Definition at line 622 of file realtime_log_analyzer.h.
Referenced by check_error_spike(), and check_rate_anomaly().
|
mutableprivate |
Definition at line 632 of file realtime_log_analyzer.h.
Referenced by get_statistics(), and reset().
|
private |
Definition at line 625 of file realtime_log_analyzer.h.
Referenced by add_to_window(), check_rate_anomaly(), get_statistics(), and reset().
|
private |
Definition at line 626 of file realtime_log_analyzer.h.
Referenced by add_to_window(), get_statistics(), and reset().
|
mutableprivate |
Definition at line 609 of file realtime_log_analyzer.h.
Referenced by add_to_window(), check_error_spike(), check_rate_anomaly(), get_error_rate(), get_log_rate(), and reset().