12#include <kcenon/common/interfaces/logger_interface.h>
15#include <unordered_map>
22using log_level = common::interfaces::log_level;
69 void add_entries(
const std::vector<analyzed_log_entry>& entries) {
98 std::vector<analyzed_log_entry> filtered;
100 if (entry.level == level) {
101 filtered.push_back(entry);
111 const std::chrono::system_clock::time_point& start,
112 const std::chrono::system_clock::time_point& end)
const {
114 std::vector<analyzed_log_entry> filtered;
115 for (
const auto& entry :
entries_) {
116 if (entry.timestamp >= start && entry.timestamp <= end) {
117 filtered.push_back(entry);
126 std::vector<analyzed_log_entry>
search_messages(
const std::string& search_text)
const {
127 std::vector<analyzed_log_entry> results;
128 for (
const auto& entry :
entries_) {
129 if (entry.message.find(search_text) != std::string::npos) {
130 results.push_back(entry);
139 double get_error_rate(
const std::chrono::minutes& window = std::chrono::minutes(60))
const {
140 auto now = std::chrono::system_clock::now();
141 auto start_time = now - window;
143 size_t total_in_window = 0;
144 size_t errors_in_window = 0;
146 for (
const auto& entry :
entries_) {
147 if (entry.timestamp >= start_time) {
149 if (entry.level == log_level::error ||
150 entry.level == log_level::fatal) {
156 return total_in_window > 0 ?
157 static_cast<double>(errors_in_window) / total_in_window : 0.0;
166 std::string report =
"=== Log Analysis Summary ===\n";
167 report +=
"Total Entries: " + std::to_string(stats.total_entries) +
"\n";
168 report +=
"Level Distribution:\n";
170 for (
const auto& [level, count] : stats.level_counts) {
171 report +=
" " +
level_to_string(level) +
": " + std::to_string(count) +
"\n";
174 if (stats.total_entries > 0) {
175 auto duration = std::chrono::duration_cast<std::chrono::minutes>(
176 stats.latest_timestamp - stats.earliest_timestamp);
177 report +=
"Time Range: " + std::to_string(duration.count()) +
" minutes\n";
197 for (
const auto& entry :
entries_) {
211 case log_level::trace:
return "TRACE";
212 case log_level::debug:
return "DEBUG";
213 case log_level::info:
return "INFO";
214 case log_level::warn:
return "WARN";
215 case log_level::error:
return "ERROR";
216 case log_level::fatal:
return "FATAL";
217 case log_level::off:
return "OFF";
218 default:
return "UNKNOWN";
226class analyzer_factory {
232 return std::make_unique<log_analyzer>();
static std::unique_ptr< log_analyzer > create_basic()
Create a basic log analyzer.
Log analyzer for processing and analyzing log data.
analysis_stats cached_stats_
const analysis_stats & get_stats()
Get analysis statistics.
void add_entries(const std::vector< analyzed_log_entry > &entries)
Add multiple log entries.
std::string generate_summary_report()
Generate summary report.
std::vector< analyzed_log_entry > filter_by_level(log_level level) const
Filter entries by log level.
void add_entry(const analyzed_log_entry &entry)
Add a log entry for analysis.
std::vector< analyzed_log_entry > search_messages(const std::string &search_text) const
Find entries containing specific text.
std::string level_to_string(log_level level) const
double get_error_rate(const std::chrono::minutes &window=std::chrono::minutes(60)) const
Get error rate for a time window.
std::vector< analyzed_log_entry > entries_
void clear()
Clear all entries.
std::vector< analyzed_log_entry > filter_by_time_range(const std::chrono::system_clock::time_point &start, const std::chrono::system_clock::time_point &end) const
Filter entries by time range.
Analysis result statistics.
std::unordered_map< log_level, size_t > level_counts
std::chrono::system_clock::time_point latest_timestamp
std::unordered_map< std::string, size_t > error_patterns
std::vector< std::string > most_frequent_messages
std::chrono::system_clock::time_point earliest_timestamp
std::string function_name
std::chrono::system_clock::time_point timestamp