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

Log analyzer for processing and analyzing log data. More...

#include <log_analyzer.h>

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

Public Member Functions

void add_entry (const analyzed_log_entry &entry)
 Add a log entry for analysis.
 
void add_entries (const std::vector< analyzed_log_entry > &entries)
 Add multiple log entries.
 
void clear ()
 Clear all entries.
 
const analysis_statsget_stats ()
 Get analysis statistics.
 
std::vector< analyzed_log_entryfilter_by_level (log_level level) const
 Filter entries by log level.
 
std::vector< analyzed_log_entryfilter_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.
 
std::vector< analyzed_log_entrysearch_messages (const std::string &search_text) const
 Find entries containing specific text.
 
double get_error_rate (const std::chrono::minutes &window=std::chrono::minutes(60)) const
 Get error rate for a time window.
 
std::string generate_summary_report ()
 Generate summary report.
 
void add_entry (const analyzed_log_entry &entry)
 Add a log entry for analysis.
 
void add_entries (const std::vector< analyzed_log_entry > &entries)
 Add multiple log entries.
 
void clear ()
 Clear all entries.
 
const analysis_statsget_stats ()
 Get analysis statistics.
 
std::vector< analyzed_log_entryfilter_by_level (common::interfaces::log_level level) const
 Filter entries by log level.
 
std::vector< analyzed_log_entryfilter_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.
 
std::vector< analyzed_log_entrysearch_messages (const std::string &search_text) const
 Find entries containing specific text.
 
double get_error_rate (const std::chrono::minutes &window=std::chrono::minutes(60)) const
 Get error rate for a time window.
 
std::string generate_summary_report ()
 Generate summary report.
 
size_t size () const
 Get the total number of entries.
 
bool empty () const
 Check if the analyzer has no entries.
 

Private Member Functions

void update_stats ()
 
std::string level_to_string (log_level level) const
 
void update_stats ()
 
std::string level_to_string (common::interfaces::log_level level) const
 

Private Attributes

std::vector< analyzed_log_entryentries_
 
analysis_stats cached_stats_
 
bool stats_dirty_ = true
 

Detailed Description

Log analyzer for processing and analyzing log data.

Provides functionality to:

  • Add and store log entries
  • Filter entries by various criteria
  • Compute statistics and rates
  • Generate summary reports

Definition at line 75 of file analysis.cppm.

Member Function Documentation

◆ add_entries() [1/2]

void kcenon::logger::analysis::log_analyzer::add_entries ( const std::vector< analyzed_log_entry > & entries)
inline

Add multiple log entries.

Definition at line 69 of file log_analyzer.h.

69 {
70 entries_.insert(entries_.end(), entries.begin(), entries.end());
71 stats_dirty_ = true;
72 }
std::vector< analyzed_log_entry > entries_

References entries_, and stats_dirty_.

◆ add_entries() [2/2]

void kcenon::logger::analysis::log_analyzer::add_entries ( const std::vector< analyzed_log_entry > & entries)
inlineexport

Add multiple log entries.

Parameters
entriesVector of entries to add

Definition at line 95 of file analysis.cppm.

95 {
96 entries_.insert(entries_.end(), entries.begin(), entries.end());
97 stats_dirty_ = true;
98 }

References entries_, and stats_dirty_.

◆ add_entry() [1/2]

void kcenon::logger::analysis::log_analyzer::add_entry ( const analyzed_log_entry & entry)
inline

Add a log entry for analysis.

Definition at line 61 of file log_analyzer.h.

61 {
62 entries_.push_back(entry);
63 stats_dirty_ = true;
64 }

References entries_, and stats_dirty_.

◆ add_entry() [2/2]

void kcenon::logger::analysis::log_analyzer::add_entry ( const analyzed_log_entry & entry)
inlineexport

Add a log entry for analysis.

Parameters
entryThe entry to add

Definition at line 86 of file analysis.cppm.

86 {
87 entries_.push_back(entry);
88 stats_dirty_ = true;
89 }

References entries_, and stats_dirty_.

◆ clear() [1/2]

void kcenon::logger::analysis::log_analyzer::clear ( )
inline

Clear all entries.

Definition at line 77 of file log_analyzer.h.

77 {
78 entries_.clear();
79 cached_stats_ = analysis_stats{};
80 stats_dirty_ = true;
81 }

References cached_stats_, entries_, and stats_dirty_.

◆ clear() [2/2]

void kcenon::logger::analysis::log_analyzer::clear ( )
inlineexport

Clear all entries.

Definition at line 103 of file analysis.cppm.

103 {
104 entries_.clear();
105 cached_stats_ = analysis_stats{};
106 stats_dirty_ = true;
107 }

References cached_stats_, entries_, and stats_dirty_.

◆ empty()

bool kcenon::logger::analysis::log_analyzer::empty ( ) const
inlineexport

Check if the analyzer has no entries.

Returns
true if empty

Definition at line 232 of file analysis.cppm.

232 {
233 return entries_.empty();
234 }

References entries_.

◆ filter_by_level() [1/2]

std::vector< analyzed_log_entry > kcenon::logger::analysis::log_analyzer::filter_by_level ( common::interfaces::log_level level) const
inlineexport

Filter entries by log level.

Parameters
levelThe level to filter for
Returns
Vector of matching entries

Definition at line 126 of file analysis.cppm.

126 {
127 std::vector<analyzed_log_entry> filtered;
128 for (const auto& entry : entries_) {
129 if (entry.level == level) {
130 filtered.push_back(entry);
131 }
132 }
133 return filtered;
134 }

References entries_.

◆ filter_by_level() [2/2]

std::vector< analyzed_log_entry > kcenon::logger::analysis::log_analyzer::filter_by_level ( log_level level) const
inline

Filter entries by log level.

Definition at line 97 of file log_analyzer.h.

97 {
98 std::vector<analyzed_log_entry> filtered;
99 for (const auto& entry : entries_) {
100 if (entry.level == level) {
101 filtered.push_back(entry);
102 }
103 }
104 return filtered;
105 }

References entries_.

◆ filter_by_time_range() [1/2]

std::vector< analyzed_log_entry > kcenon::logger::analysis::log_analyzer::filter_by_time_range ( const std::chrono::system_clock::time_point & start,
const std::chrono::system_clock::time_point & end ) const
inline

Filter entries by time range.

Definition at line 110 of file log_analyzer.h.

112 {
113
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);
118 }
119 }
120 return filtered;
121 }

References entries_.

◆ filter_by_time_range() [2/2]

std::vector< analyzed_log_entry > kcenon::logger::analysis::log_analyzer::filter_by_time_range ( const std::chrono::system_clock::time_point & start,
const std::chrono::system_clock::time_point & end ) const
inlineexport

Filter entries by time range.

Parameters
startStart of time range
endEnd of time range
Returns
Vector of entries within the time range

Definition at line 142 of file analysis.cppm.

144 {
145
146 std::vector<analyzed_log_entry> filtered;
147 for (const auto& entry : entries_) {
148 if (entry.timestamp >= start && entry.timestamp <= end) {
149 filtered.push_back(entry);
150 }
151 }
152 return filtered;
153 }

References entries_.

◆ generate_summary_report() [1/2]

std::string kcenon::logger::analysis::log_analyzer::generate_summary_report ( )
inline

Generate summary report.

Definition at line 163 of file log_analyzer.h.

163 {
164 const auto& stats = get_stats();
165
166 std::string report = "=== Log Analysis Summary ===\n";
167 report += "Total Entries: " + std::to_string(stats.total_entries) + "\n";
168 report += "Level Distribution:\n";
169
170 for (const auto& [level, count] : stats.level_counts) {
171 report += " " + level_to_string(level) + ": " + std::to_string(count) + "\n";
172 }
173
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";
178 }
179
180 return report;
181 }
const analysis_stats & get_stats()
Get analysis statistics.
std::string level_to_string(log_level level) const

References get_stats(), and level_to_string().

Here is the call graph for this function:

◆ generate_summary_report() [2/2]

std::string kcenon::logger::analysis::log_analyzer::generate_summary_report ( )
inlineexport

Generate summary report.

Returns
Formatted summary string

Definition at line 200 of file analysis.cppm.

200 {
201 const auto& stats = get_stats();
202
203 std::string report = "=== Log Analysis Summary ===\n";
204 report += "Total Entries: " + std::to_string(stats.total_entries) + "\n";
205 report += "Level Distribution:\n";
206
207 for (const auto& [level, count] : stats.level_counts) {
208 report += " " + level_to_string(level) + ": " + std::to_string(count) + "\n";
209 }
210
211 if (stats.total_entries > 0) {
212 auto duration = std::chrono::duration_cast<std::chrono::minutes>(
213 stats.latest_timestamp - stats.earliest_timestamp);
214 report += "Time Range: " + std::to_string(duration.count()) + " minutes\n";
215 }
216
217 return report;
218 }

References get_stats(), and level_to_string().

Here is the call graph for this function:

◆ get_error_rate() [1/2]

double kcenon::logger::analysis::log_analyzer::get_error_rate ( const std::chrono::minutes & window = std::chrono::minutes(60)) const
inline

Get error rate for a time window.

Definition at line 139 of file log_analyzer.h.

139 {
140 auto now = std::chrono::system_clock::now();
141 auto start_time = now - window;
142
143 size_t total_in_window = 0;
144 size_t errors_in_window = 0;
145
146 for (const auto& entry : entries_) {
147 if (entry.timestamp >= start_time) {
148 total_in_window++;
149 if (entry.level == log_level::error ||
150 entry.level == log_level::fatal) {
151 errors_in_window++;
152 }
153 }
154 }
155
156 return total_in_window > 0 ?
157 static_cast<double>(errors_in_window) / total_in_window : 0.0;
158 }

References entries_.

◆ get_error_rate() [2/2]

double kcenon::logger::analysis::log_analyzer::get_error_rate ( const std::chrono::minutes & window = std::chrono::minutes(60)) const
inlineexport

Get error rate for a time window.

Parameters
windowTime window to analyze (default: 60 minutes)
Returns
Error rate as a decimal (0.0 to 1.0)

Definition at line 175 of file analysis.cppm.

175 {
176 auto now = std::chrono::system_clock::now();
177 auto start_time = now - window;
178
179 size_t total_in_window = 0;
180 size_t errors_in_window = 0;
181
182 for (const auto& entry : entries_) {
183 if (entry.timestamp >= start_time) {
184 total_in_window++;
185 if (entry.level == common::interfaces::log_level::error ||
186 entry.level == common::interfaces::log_level::critical) {
187 errors_in_window++;
188 }
189 }
190 }
191
192 return total_in_window > 0 ?
193 static_cast<double>(errors_in_window) / static_cast<double>(total_in_window) : 0.0;
194 }

References entries_.

◆ get_stats() [1/2]

const analysis_stats & kcenon::logger::analysis::log_analyzer::get_stats ( )
inline

Get analysis statistics.

Definition at line 86 of file log_analyzer.h.

86 {
87 if (stats_dirty_) {
89 stats_dirty_ = false;
90 }
91 return cached_stats_;
92 }

References cached_stats_, stats_dirty_, and update_stats().

Referenced by generate_summary_report().

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

◆ get_stats() [2/2]

const analysis_stats & kcenon::logger::analysis::log_analyzer::get_stats ( )
inlineexport

Get analysis statistics.

Returns
Reference to computed statistics

Definition at line 113 of file analysis.cppm.

113 {
114 if (stats_dirty_) {
115 update_stats();
116 stats_dirty_ = false;
117 }
118 return cached_stats_;
119 }

References cached_stats_, stats_dirty_, and update_stats().

Here is the call graph for this function:

◆ level_to_string() [1/2]

std::string kcenon::logger::analysis::log_analyzer::level_to_string ( common::interfaces::log_level level) const
inlineexportprivate

Definition at line 262 of file analysis.cppm.

262 {
263 // Use underlying integer to avoid duplicate case value errors
264 // (warn and warning both = 3, fatal and critical both = 5)
265 const int level_value = static_cast<int>(level);
266 switch (level_value) {
267 case 0: return "TRACE";
268 case 1: return "DEBUG";
269 case 2: return "INFO";
270 case 3: return "WARN";
271 case 4: return "ERROR";
272 case 5: return "FATAL";
273 case 6: return "OFF";
274 default: return "UNKNOWN";
275 }
276 }

◆ level_to_string() [2/2]

std::string kcenon::logger::analysis::log_analyzer::level_to_string ( log_level level) const
inlineprivate

Definition at line 209 of file log_analyzer.h.

209 {
210 switch (level) {
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";
219 }
220 }

Referenced by generate_summary_report().

Here is the caller graph for this function:

◆ search_messages() [1/2]

std::vector< analyzed_log_entry > kcenon::logger::analysis::log_analyzer::search_messages ( const std::string & search_text) const
inline

Find entries containing specific text.

Definition at line 126 of file log_analyzer.h.

126 {
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);
131 }
132 }
133 return results;
134 }

References entries_.

◆ search_messages() [2/2]

std::vector< analyzed_log_entry > kcenon::logger::analysis::log_analyzer::search_messages ( const std::string & search_text) const
inlineexport

Find entries containing specific text.

Parameters
search_textText to search for
Returns
Vector of matching entries

Definition at line 160 of file analysis.cppm.

160 {
161 std::vector<analyzed_log_entry> results;
162 for (const auto& entry : entries_) {
163 if (entry.message.find(search_text) != std::string::npos) {
164 results.push_back(entry);
165 }
166 }
167 return results;
168 }

References entries_.

◆ size()

size_t kcenon::logger::analysis::log_analyzer::size ( ) const
inlineexport

Get the total number of entries.

Returns
Number of stored entries

Definition at line 224 of file analysis.cppm.

224 {
225 return entries_.size();
226 }

References entries_.

◆ update_stats() [1/2]

void kcenon::logger::analysis::log_analyzer::update_stats ( )
inlineprivate

Definition at line 184 of file log_analyzer.h.

184 {
185 cached_stats_ = analysis_stats{};
187
188 if (entries_.empty()) {
189 return;
190 }
191
192 // Initialize timestamps
195
196 // Count levels and update timestamps
197 for (const auto& entry : entries_) {
198 cached_stats_.level_counts[entry.level]++;
199
200 if (entry.timestamp < cached_stats_.earliest_timestamp) {
201 cached_stats_.earliest_timestamp = entry.timestamp;
202 }
203 if (entry.timestamp > cached_stats_.latest_timestamp) {
204 cached_stats_.latest_timestamp = entry.timestamp;
205 }
206 }
207 }
std::unordered_map< log_level, size_t > level_counts
std::chrono::system_clock::time_point latest_timestamp
std::chrono::system_clock::time_point earliest_timestamp

References cached_stats_, kcenon::logger::analysis::analysis_stats::earliest_timestamp, entries_, kcenon::logger::analysis::analysis_stats::latest_timestamp, kcenon::logger::analysis::analysis_stats::level_counts, and kcenon::logger::analysis::analysis_stats::total_entries.

Referenced by get_stats().

Here is the caller graph for this function:

◆ update_stats() [2/2]

void kcenon::logger::analysis::log_analyzer::update_stats ( )
inlineexportprivate

Definition at line 237 of file analysis.cppm.

237 {
238 cached_stats_ = analysis_stats{};
240
241 if (entries_.empty()) {
242 return;
243 }
244
245 // Initialize timestamps
248
249 // Count levels and update timestamps
250 for (const auto& entry : entries_) {
251 cached_stats_.level_counts[entry.level]++;
252
253 if (entry.timestamp < cached_stats_.earliest_timestamp) {
254 cached_stats_.earliest_timestamp = entry.timestamp;
255 }
256 if (entry.timestamp > cached_stats_.latest_timestamp) {
257 cached_stats_.latest_timestamp = entry.timestamp;
258 }
259 }
260 }

References cached_stats_, kcenon::logger::analysis::analysis_stats::earliest_timestamp, entries_, kcenon::logger::analysis::analysis_stats::latest_timestamp, kcenon::logger::analysis::analysis_stats::level_counts, and kcenon::logger::analysis::analysis_stats::total_entries.

Member Data Documentation

◆ cached_stats_

analysis_stats kcenon::logger::analysis::log_analyzer::cached_stats_
exportprivate

Definition at line 54 of file log_analyzer.h.

Referenced by clear(), get_stats(), and update_stats().

◆ entries_

std::vector< analyzed_log_entry > kcenon::logger::analysis::log_analyzer::entries_
exportprivate

◆ stats_dirty_

bool kcenon::logger::analysis::log_analyzer::stats_dirty_ = true
exportprivate

Definition at line 55 of file log_analyzer.h.

Referenced by add_entries(), add_entry(), clear(), and get_stats().


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