Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
Loading...
Searching...
No Matches
kcenon::logger::analysis::realtime_log_analyzer Class Reference

Real-time log analyzer with anomaly detection. More...

#include <realtime_log_analyzer.h>

Collaboration diagram for kcenon::logger::analysis::realtime_log_analyzer:
Collaboration graph

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_configget_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_entrylog_window_
 
std::deque< timestamped_entryerror_window_
 
std::deque< double > baseline_rates_
 
std::shared_mutex window_mutex_
 
std::vector< pattern_alertpatterns_
 
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_
 

Detailed Description

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:

  • Sliding window for rate calculation
  • Error spike detection
  • Pattern-based alerting with regex support
  • Rate anomaly detection (high/low rate alerts)
  • New error type tracking
  • Thread-safe for concurrent logging
Note
This class is designed to have minimal performance impact (< 5% overhead) on the logging pipeline.
Since
3.2.0

Definition at line 138 of file realtime_log_analyzer.h.

Member Typedef Documentation

◆ anomaly_callback

Constructor & Destructor Documentation

◆ realtime_log_analyzer() [1/2]

kcenon::logger::analysis::realtime_log_analyzer::realtime_log_analyzer ( )
default

Default constructor.

◆ realtime_log_analyzer() [2/2]

kcenon::logger::analysis::realtime_log_analyzer::realtime_log_analyzer ( const realtime_analysis_config & config)
inlineexplicit

Constructor with configuration.

Parameters
configAnalysis configuration

Definition at line 154 of file realtime_log_analyzer.h.

155 : config_(config) {}

Member Function Documentation

◆ add_pattern_alert()

void kcenon::logger::analysis::realtime_log_analyzer::add_pattern_alert ( const std::string & pattern,
log_level min_level )
inline

Add a pattern-based alert.

Parameters
patternRegex pattern to match against log messages
min_levelMinimum log level for this pattern to trigger

Definition at line 219 of file realtime_log_analyzer.h.

219 {
220 std::unique_lock lock(patterns_mutex_);
221 patterns_.emplace_back(pattern, min_level);
222 }

References patterns_, and patterns_mutex_.

◆ add_to_window()

void kcenon::logger::analysis::realtime_log_analyzer::add_to_window ( const analyzed_log_entry & entry,
std::chrono::system_clock::time_point now )
inlineprivate

Definition at line 367 of file realtime_log_analyzer.h.

368 {
369 std::unique_lock lock(window_mutex_);
370
371 // Add to log window
372 log_window_.push_back({now, entry});
373
374 // Add to error window if error/fatal
375 if (entry.level == log_level::error ||
376 entry.level == log_level::fatal) {
377 error_window_.push_back({now, entry});
378 }
379
380 // Clean up old entries
381 auto cutoff = now - config_.window_duration;
384
385 // Update statistics
386 total_analyzed_.fetch_add(1, std::memory_order_relaxed);
387 if (entry.level == log_level::error ||
388 entry.level == log_level::fatal) {
389 total_errors_.fetch_add(1, std::memory_order_relaxed);
390 }
391 }
static void cleanup_window(std::deque< timestamped_entry > &window, std::chrono::system_clock::time_point cutoff)
std::chrono::seconds window_duration
Sliding window duration for rate calculation.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ analyze()

void kcenon::logger::analysis::realtime_log_analyzer::analyze ( const analyzed_log_entry & entry)
inline

Analyze a log entry in real-time.

Parameters
entryThe 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.

178 {
179 auto now = std::chrono::system_clock::now();
180
181 // Add to sliding window
182 add_to_window(entry, now);
183
184 // Check for error spike
185 if (entry.level == log_level::error ||
186 entry.level == log_level::fatal) {
187 check_error_spike(entry, now);
188 }
189
190 // Check pattern alerts
191 check_pattern_alerts(entry, now);
192
193 // Check rate anomaly
196 }
197
198 // Track new error types
200 (entry.level == log_level::error ||
201 entry.level == log_level::fatal)) {
202 check_new_error_type(entry, now);
203 }
204 }
void check_rate_anomaly(std::chrono::system_clock::time_point now)
void add_to_window(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_new_error_type(const analyzed_log_entry &entry, std::chrono::system_clock::time_point now)
void check_error_spike(const analyzed_log_entry &entry, std::chrono::system_clock::time_point now)
bool enable_rate_anomaly_detection
Enable rate anomaly detection.
bool track_new_errors
Enable new error type detection.

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.

Here is the call graph for this function:

◆ calculate_rate()

double kcenon::logger::analysis::realtime_log_analyzer::calculate_rate ( const std::deque< timestamped_entry > & window) const
inlineprivate

Definition at line 400 of file realtime_log_analyzer.h.

400 {
401 if (window.empty()) return 0.0;
402
403 auto duration = std::chrono::duration_cast<std::chrono::seconds>(
404 config_.window_duration).count();
405 if (duration == 0) duration = 60;
406
407 return static_cast<double>(window.size()) * 60.0 / duration;
408 }

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().

Here is the caller graph for this function:

◆ check_error_spike()

void kcenon::logger::analysis::realtime_log_analyzer::check_error_spike ( const analyzed_log_entry & entry,
std::chrono::system_clock::time_point now )
inlineprivate

Definition at line 410 of file realtime_log_analyzer.h.

411 {
412 std::shared_lock lock(window_mutex_);
413
414 double current_rate = calculate_rate(error_window_);
415
416 if (current_rate >= static_cast<double>(config_.error_spike_threshold)) {
417 // Rate limit: don't alert more than once per minute
418 {
419 std::shared_lock rate_lock(rate_limit_mutex_);
420 if (now - last_spike_alert_ < std::chrono::minutes(1)) {
421 return;
422 }
423 }
424
425 lock.unlock(); // Release before callback
426
427 // Update last alert time
428 {
429 std::unique_lock rate_lock(rate_limit_mutex_);
430 last_spike_alert_ = now;
431 }
432
433 anomaly_event event;
434 event.anomaly_type = anomaly_event::type::error_spike;
435 event.detected_at = now;
436 event.description = "Error spike detected: " +
437 std::to_string(static_cast<size_t>(current_rate)) +
438 " errors/minute (threshold: " +
439 std::to_string(config_.error_spike_threshold) + ")";
440 event.current_count = static_cast<size_t>(current_rate);
441 event.threshold = config_.error_spike_threshold;
442
443 // Collect related entries
444 {
445 std::shared_lock entries_lock(window_mutex_);
447 }
448
449 notify_anomaly(event);
450 error_spikes_.fetch_add(1, std::memory_order_relaxed);
451 }
452 }
void collect_related_entries(anomaly_event &event, const std::deque< timestamped_entry > &window) const
double calculate_rate(const std::deque< timestamped_entry > &window) const
std::chrono::system_clock::time_point last_spike_alert_
@ error_spike
Sudden increase in errors.
size_t error_spike_threshold
Errors per minute to trigger spike alert.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_new_error_type()

void kcenon::logger::analysis::realtime_log_analyzer::check_new_error_type ( const analyzed_log_entry & entry,
std::chrono::system_clock::time_point now )
inlineprivate

Definition at line 534 of file realtime_log_analyzer.h.

535 {
536 // Normalize error message (remove numbers, timestamps, etc.)
537 std::string normalized = normalize_error_message(entry.message);
538
539 {
540 std::shared_lock lock(errors_mutex_);
541 if (known_errors_.contains(normalized)) {
542 return; // Already seen this error type
543 }
544 }
545
546 // New error type detected
547 {
548 std::unique_lock lock(errors_mutex_);
549 known_errors_.insert(normalized);
550 }
551
552 anomaly_event event;
553 event.anomaly_type = anomaly_event::type::new_error_type;
554 event.detected_at = now;
555 event.description = "New error type detected: " + entry.message;
556 event.related_entries.push_back(entry);
557
558 notify_anomaly(event);
559 new_error_types_.fetch_add(1, std::memory_order_relaxed);
560 }
static std::string normalize_error_message(const std::string &message)
@ new_error_type
Previously unseen error message.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_pattern_alerts()

void kcenon::logger::analysis::realtime_log_analyzer::check_pattern_alerts ( const analyzed_log_entry & entry,
std::chrono::system_clock::time_point now )
inlineprivate

Definition at line 454 of file realtime_log_analyzer.h.

455 {
456 std::shared_lock lock(patterns_mutex_);
457
458 for (const auto& alert : patterns_) {
459 // Check level threshold
460 if (static_cast<int>(entry.level) < static_cast<int>(alert.min_level)) {
461 continue;
462 }
463
464 // Check pattern match
465 if (std::regex_search(entry.message, alert.compiled_pattern)) {
466 lock.unlock(); // Release before callback
467
468 anomaly_event event;
469 event.anomaly_type = anomaly_event::type::pattern_match;
470 event.detected_at = now;
471 event.pattern = alert.pattern;
472 event.description = "Pattern '" + alert.pattern +
473 "' matched in log message: " + entry.message;
474 event.related_entries.push_back(entry);
475
476 notify_anomaly(event);
477 pattern_matches_.fetch_add(1, std::memory_order_relaxed);
478 return; // Only report first match per entry
479 }
480 }
481 }
@ pattern_match
Configured pattern detected.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_rate_anomaly()

void kcenon::logger::analysis::realtime_log_analyzer::check_rate_anomaly ( std::chrono::system_clock::time_point now)
inlineprivate

Definition at line 483 of file realtime_log_analyzer.h.

483 {
484 // Rate limit rate anomaly checks to once per 10 seconds
485 {
486 std::shared_lock rate_lock(rate_limit_mutex_);
487 if (now - last_rate_check_ < std::chrono::seconds(10)) {
488 return;
489 }
490 }
491 {
492 std::unique_lock rate_lock(rate_limit_mutex_);
493 last_rate_check_ = now;
494 }
495
496 std::shared_lock lock(window_mutex_);
497 double current_rate = calculate_rate(log_window_);
498 lock.unlock();
499
500 // Check high rate
501 if (current_rate >= static_cast<double>(config_.rate_anomaly_high_threshold)) {
502 anomaly_event event;
503 event.anomaly_type = anomaly_event::type::rate_anomaly;
504 event.detected_at = now;
505 event.description = "High log rate detected: " +
506 std::to_string(static_cast<size_t>(current_rate)) +
507 " logs/minute (threshold: " +
508 std::to_string(config_.rate_anomaly_high_threshold) + ")";
509 event.current_count = static_cast<size_t>(current_rate);
510 event.threshold = config_.rate_anomaly_high_threshold;
511
512 notify_anomaly(event);
513 rate_anomalies_.fetch_add(1, std::memory_order_relaxed);
514 }
515 // Check low rate (if enabled)
516 else if (config_.rate_anomaly_low_threshold > 0 &&
517 current_rate < static_cast<double>(config_.rate_anomaly_low_threshold) &&
518 total_analyzed_.load() > 100) { // Only after enough data
519 anomaly_event event;
520 event.anomaly_type = anomaly_event::type::rate_anomaly;
521 event.detected_at = now;
522 event.description = "Low log rate detected: " +
523 std::to_string(static_cast<size_t>(current_rate)) +
524 " logs/minute (threshold: " +
525 std::to_string(config_.rate_anomaly_low_threshold) + ")";
526 event.current_count = static_cast<size_t>(current_rate);
527 event.threshold = config_.rate_anomaly_low_threshold;
528
529 notify_anomaly(event);
530 rate_anomalies_.fetch_add(1, std::memory_order_relaxed);
531 }
532 }
std::chrono::system_clock::time_point last_rate_check_
@ rate_anomaly
Unusual log rate (too high or too low)
size_t rate_anomaly_low_threshold
Logs per minute considered low (0 = disabled)
size_t rate_anomaly_high_threshold
Logs per minute considered high.

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().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ cleanup_window()

static void kcenon::logger::analysis::realtime_log_analyzer::cleanup_window ( std::deque< timestamped_entry > & window,
std::chrono::system_clock::time_point cutoff )
inlinestaticprivate

Definition at line 393 of file realtime_log_analyzer.h.

394 {
395 while (!window.empty() && window.front().timestamp < cutoff) {
396 window.pop_front();
397 }
398 }

Referenced by add_to_window().

Here is the caller graph for this function:

◆ clear_pattern_alerts()

void kcenon::logger::analysis::realtime_log_analyzer::clear_pattern_alerts ( )
inline

Clear all pattern alerts.

Definition at line 245 of file realtime_log_analyzer.h.

245 {
246 std::unique_lock lock(patterns_mutex_);
247 patterns_.clear();
248 }

References patterns_, and patterns_mutex_.

◆ collect_related_entries()

void kcenon::logger::analysis::realtime_log_analyzer::collect_related_entries ( anomaly_event & event,
const std::deque< timestamped_entry > & window ) const
inlineprivate

Definition at line 579 of file realtime_log_analyzer.h.

580 {
581 size_t count = 0;
582 for (auto it = window.rbegin();
583 it != window.rend() && count < config_.max_related_entries;
584 ++it, ++count) {
585 event.related_entries.push_back(it->entry);
586 }
587 }
size_t max_related_entries
Max entries to store per anomaly.

References config_, and kcenon::logger::analysis::realtime_analysis_config::max_related_entries.

Referenced by check_error_spike().

Here is the caller graph for this function:

◆ get_config()

const realtime_analysis_config & kcenon::logger::analysis::realtime_log_analyzer::get_config ( ) const
inline

Get the configuration.

Returns
Current configuration

Definition at line 349 of file realtime_log_analyzer.h.

349 {
350 return config_;
351 }

References config_.

◆ get_error_rate()

double kcenon::logger::analysis::realtime_log_analyzer::get_error_rate ( ) const
inline

Get current error rate (errors per minute)

Returns
Current error rate

Definition at line 272 of file realtime_log_analyzer.h.

272 {
273 std::shared_lock lock(window_mutex_);
275 }

References calculate_rate(), error_window_, and window_mutex_.

Referenced by get_statistics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_log_rate()

double kcenon::logger::analysis::realtime_log_analyzer::get_log_rate ( ) const
inline

Get current log rate (logs per minute)

Returns
Current log rate

Definition at line 281 of file realtime_log_analyzer.h.

281 {
282 std::shared_lock lock(window_mutex_);
284 }

References calculate_rate(), log_window_, and window_mutex_.

Referenced by get_statistics().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_statistics()

statistics kcenon::logger::analysis::realtime_log_analyzer::get_statistics ( ) const
inline

Definition at line 302 of file realtime_log_analyzer.h.

302 {
303 std::shared_lock lock(stats_mutex_);
304 statistics stats;
305 stats.total_analyzed = total_analyzed_.load();
306 stats.total_errors = total_errors_.load();
307 stats.anomalies_detected = anomalies_detected_.load();
308 stats.error_spikes = error_spikes_.load();
309 stats.pattern_matches = pattern_matches_.load();
310 stats.rate_anomalies = rate_anomalies_.load();
311 stats.new_error_types = new_error_types_.load();
312 stats.current_log_rate = get_log_rate();
313 stats.current_error_rate = get_error_rate();
314 return stats;
315 }
double get_log_rate() const
Get current log rate (logs per minute)
double get_error_rate() const
Get current error rate (errors per minute)

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_.

Here is the call graph for this function:

◆ normalize_error_message()

static std::string kcenon::logger::analysis::realtime_log_analyzer::normalize_error_message ( const std::string & message)
inlinestaticprivate

Definition at line 562 of file realtime_log_analyzer.h.

562 {
563 // Remove numbers (IDs, timestamps, etc.)
564 std::string normalized = std::regex_replace(message,
565 std::regex(R"(\d+)"), "N");
566
567 // Remove hex values
568 normalized = std::regex_replace(normalized,
569 std::regex(R"(0x[0-9a-fA-F]+)"), "HEX");
570
571 // Remove UUIDs
572 normalized = std::regex_replace(normalized,
573 std::regex(R"([0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})"),
574 "UUID");
575
576 return normalized;
577 }

Referenced by check_new_error_type().

Here is the caller graph for this function:

◆ notify_anomaly()

void kcenon::logger::analysis::realtime_log_analyzer::notify_anomaly ( const anomaly_event & event)
inlineprivate

Definition at line 589 of file realtime_log_analyzer.h.

589 {
590 anomalies_detected_.fetch_add(1, std::memory_order_relaxed);
591
592 std::shared_lock lock(callback_mutex_);
593 if (callback_) {
594 callback_(event);
595 }
596 }

References anomalies_detected_, callback_, and callback_mutex_.

Referenced by check_error_spike(), check_new_error_type(), check_pattern_alerts(), and check_rate_anomaly().

Here is the caller graph for this function:

◆ remove_pattern_alert()

bool kcenon::logger::analysis::realtime_log_analyzer::remove_pattern_alert ( const std::string & pattern)
inline

Remove a pattern alert.

Parameters
patternThe pattern to remove
Returns
true if pattern was found and removed

Definition at line 229 of file realtime_log_analyzer.h.

229 {
230 std::unique_lock lock(patterns_mutex_);
231 auto it = std::remove_if(patterns_.begin(), patterns_.end(),
232 [&pattern](const pattern_alert& alert) {
233 return alert.pattern == pattern;
234 });
235 if (it != patterns_.end()) {
236 patterns_.erase(it, patterns_.end());
237 return true;
238 }
239 return false;
240 }

References patterns_, and patterns_mutex_.

◆ reset()

void kcenon::logger::analysis::realtime_log_analyzer::reset ( )
inline

Reset all statistics and tracked state.

Definition at line 320 of file realtime_log_analyzer.h.

320 {
321 {
322 std::unique_lock lock(window_mutex_);
323 log_window_.clear();
324 error_window_.clear();
325 baseline_rates_.clear();
326 }
327 {
328 std::unique_lock lock(errors_mutex_);
329 known_errors_.clear();
330 }
331 {
332 std::unique_lock lock(stats_mutex_);
333 total_analyzed_ = 0;
334 total_errors_ = 0;
336 error_spikes_ = 0;
338 rate_anomalies_ = 0;
340 }
341 last_rate_check_ = std::chrono::system_clock::time_point{};
342 last_spike_alert_ = std::chrono::system_clock::time_point{};
343 }

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_.

◆ set_anomaly_callback()

void kcenon::logger::analysis::realtime_log_analyzer::set_anomaly_callback ( anomaly_callback cb)
inline

Set the anomaly callback.

Parameters
cbCallback 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.

164 {
165 std::unique_lock lock(callback_mutex_);
166 callback_ = std::move(cb);
167 }

References callback_, and callback_mutex_.

◆ set_config()

void kcenon::logger::analysis::realtime_log_analyzer::set_config ( const realtime_analysis_config & config)
inline

Set the configuration.

Parameters
configNew configuration

Definition at line 357 of file realtime_log_analyzer.h.

357 {
358 config_ = config;
359 }

References config_.

◆ set_error_spike_threshold()

void kcenon::logger::analysis::realtime_log_analyzer::set_error_spike_threshold ( size_t errors_per_minute)
inline

Set error spike threshold.

Parameters
errors_per_minuteNumber of errors per minute to trigger alert

Definition at line 210 of file realtime_log_analyzer.h.

210 {
211 config_.error_spike_threshold = errors_per_minute;
212 }

References config_, and kcenon::logger::analysis::realtime_analysis_config::error_spike_threshold.

◆ set_rate_thresholds()

void kcenon::logger::analysis::realtime_log_analyzer::set_rate_thresholds ( size_t high_threshold,
size_t low_threshold = 0 )
inline

Set rate anomaly thresholds.

Parameters
high_thresholdHigh rate threshold (logs per minute)
low_thresholdLow rate threshold (logs per minute), 0 to disable

Definition at line 255 of file realtime_log_analyzer.h.

255 {
256 config_.rate_anomaly_high_threshold = high_threshold;
257 config_.rate_anomaly_low_threshold = low_threshold;
258 }

References config_, kcenon::logger::analysis::realtime_analysis_config::rate_anomaly_high_threshold, and kcenon::logger::analysis::realtime_analysis_config::rate_anomaly_low_threshold.

◆ set_track_new_errors()

void kcenon::logger::analysis::realtime_log_analyzer::set_track_new_errors ( bool enable)
inline

Enable or disable new error tracking.

Parameters
enabletrue to enable, false to disable

Definition at line 264 of file realtime_log_analyzer.h.

264 {
265 config_.track_new_errors = enable;
266 }

References config_, and kcenon::logger::analysis::realtime_analysis_config::track_new_errors.

Member Data Documentation

◆ anomalies_detected_

std::atomic<size_t> kcenon::logger::analysis::realtime_log_analyzer::anomalies_detected_ {0}
private

Definition at line 627 of file realtime_log_analyzer.h.

627{0};

Referenced by get_statistics(), notify_anomaly(), and reset().

◆ baseline_rates_

std::deque<double> kcenon::logger::analysis::realtime_log_analyzer::baseline_rates_
private

Definition at line 608 of file realtime_log_analyzer.h.

Referenced by reset().

◆ callback_

anomaly_callback kcenon::logger::analysis::realtime_log_analyzer::callback_
private

Definition at line 602 of file realtime_log_analyzer.h.

Referenced by notify_anomaly(), and set_anomaly_callback().

◆ callback_mutex_

std::shared_mutex kcenon::logger::analysis::realtime_log_analyzer::callback_mutex_
mutableprivate

Definition at line 603 of file realtime_log_analyzer.h.

Referenced by notify_anomaly(), and set_anomaly_callback().

◆ config_

◆ error_spikes_

std::atomic<size_t> kcenon::logger::analysis::realtime_log_analyzer::error_spikes_ {0}
private

Definition at line 628 of file realtime_log_analyzer.h.

628{0};

Referenced by check_error_spike(), get_statistics(), and reset().

◆ error_window_

std::deque<timestamped_entry> kcenon::logger::analysis::realtime_log_analyzer::error_window_
private

Definition at line 607 of file realtime_log_analyzer.h.

Referenced by add_to_window(), check_error_spike(), get_error_rate(), and reset().

◆ errors_mutex_

std::shared_mutex kcenon::logger::analysis::realtime_log_analyzer::errors_mutex_
mutableprivate

Definition at line 617 of file realtime_log_analyzer.h.

Referenced by check_new_error_type(), and reset().

◆ known_errors_

std::unordered_set<std::string> kcenon::logger::analysis::realtime_log_analyzer::known_errors_
private

Definition at line 616 of file realtime_log_analyzer.h.

Referenced by check_new_error_type(), and reset().

◆ last_rate_check_

std::chrono::system_clock::time_point kcenon::logger::analysis::realtime_log_analyzer::last_rate_check_
private

Definition at line 620 of file realtime_log_analyzer.h.

Referenced by check_rate_anomaly(), and reset().

◆ last_spike_alert_

std::chrono::system_clock::time_point kcenon::logger::analysis::realtime_log_analyzer::last_spike_alert_
private

Definition at line 621 of file realtime_log_analyzer.h.

Referenced by check_error_spike(), and reset().

◆ log_window_

std::deque<timestamped_entry> kcenon::logger::analysis::realtime_log_analyzer::log_window_
private

Definition at line 606 of file realtime_log_analyzer.h.

Referenced by add_to_window(), check_rate_anomaly(), get_log_rate(), and reset().

◆ new_error_types_

std::atomic<size_t> kcenon::logger::analysis::realtime_log_analyzer::new_error_types_ {0}
private

Definition at line 631 of file realtime_log_analyzer.h.

631{0};

Referenced by check_new_error_type(), get_statistics(), and reset().

◆ pattern_matches_

std::atomic<size_t> kcenon::logger::analysis::realtime_log_analyzer::pattern_matches_ {0}
private

Definition at line 629 of file realtime_log_analyzer.h.

629{0};

Referenced by check_pattern_alerts(), get_statistics(), and reset().

◆ patterns_

std::vector<pattern_alert> kcenon::logger::analysis::realtime_log_analyzer::patterns_
private

◆ patterns_mutex_

std::shared_mutex kcenon::logger::analysis::realtime_log_analyzer::patterns_mutex_
mutableprivate

◆ rate_anomalies_

std::atomic<size_t> kcenon::logger::analysis::realtime_log_analyzer::rate_anomalies_ {0}
private

Definition at line 630 of file realtime_log_analyzer.h.

630{0};

Referenced by check_rate_anomaly(), get_statistics(), and reset().

◆ rate_limit_mutex_

std::shared_mutex kcenon::logger::analysis::realtime_log_analyzer::rate_limit_mutex_
mutableprivate

Definition at line 622 of file realtime_log_analyzer.h.

Referenced by check_error_spike(), and check_rate_anomaly().

◆ stats_mutex_

std::shared_mutex kcenon::logger::analysis::realtime_log_analyzer::stats_mutex_
mutableprivate

Definition at line 632 of file realtime_log_analyzer.h.

Referenced by get_statistics(), and reset().

◆ total_analyzed_

std::atomic<size_t> kcenon::logger::analysis::realtime_log_analyzer::total_analyzed_ {0}
private

Definition at line 625 of file realtime_log_analyzer.h.

625{0};

Referenced by add_to_window(), check_rate_anomaly(), get_statistics(), and reset().

◆ total_errors_

std::atomic<size_t> kcenon::logger::analysis::realtime_log_analyzer::total_errors_ {0}
private

Definition at line 626 of file realtime_log_analyzer.h.

626{0};

Referenced by add_to_window(), get_statistics(), and reset().

◆ window_mutex_

std::shared_mutex kcenon::logger::analysis::realtime_log_analyzer::window_mutex_
mutableprivate

The documentation for this class was generated from the following file: