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

#include <structured_logger.h>

Inheritance diagram for kcenon::logger::structured::basic_structured_logger:
Inheritance graph
Collaboration diagram for kcenon::logger::structured::basic_structured_logger:
Collaboration graph

Public Member Functions

void set_format (structured_format format)
 Set the output format.
 
void set_output_callback (structured_output_callback callback)
 Set a custom output callback.
 
void set_output_to_stderr (bool use_stderr)
 Set whether to output to stderr (default: stdout)
 
void log_structured (const structured_log_entry &entry) override
 Log a structured message.
 
log_builder start_log (log_level level) override
 Start building a structured log entry.
 
- Public Member Functions inherited from kcenon::logger::structured::structured_logger_interface
virtual ~structured_logger_interface ()=default
 

Static Private Member Functions

static std::string format_logfmt (const structured_log_entry &entry)
 Format entry in logfmt style (key=value pairs)
 
static std::string level_to_string (log_level level)
 
static std::string format_timestamp_logfmt (std::chrono::system_clock::time_point tp)
 
static std::string escape_logfmt_value (const std::string &value)
 

Private Attributes

structured_format format_ = structured_format::json
 
structured_output_callback output_callback_
 
bool output_to_stderr_ = false
 

Detailed Description

Definition at line 158 of file structured_logger.h.

Member Function Documentation

◆ escape_logfmt_value()

static std::string kcenon::logger::structured::basic_structured_logger::escape_logfmt_value ( const std::string & value)
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 271 of file structured_logger.h.

271 {
272 // If value contains spaces, quotes, or special chars, quote it
273 bool needs_quoting = false;
274 for (char c : value) {
275 if (c == ' ' || c == '"' || c == '=' || c == '\n' || c == '\t') {
276 needs_quoting = true;
277 break;
278 }
279 }
280
281 if (!needs_quoting && !value.empty()) {
282 return value;
283 }
284
285 // Quote and escape
286 std::ostringstream oss;
287 oss << '"';
288 for (char c : value) {
289 switch (c) {
290 case '"': oss << "\\\""; break;
291 case '\\': oss << "\\\\"; break;
292 case '\n': oss << "\\n"; break;
293 case '\t': oss << "\\t"; break;
294 default: oss << c;
295 }
296 }
297 oss << '"';
298 return oss.str();
299 }

Referenced by format_logfmt().

Here is the caller graph for this function:

◆ format_logfmt()

static std::string kcenon::logger::structured::basic_structured_logger::format_logfmt ( const structured_log_entry & entry)
inlinestaticprivate

Format entry in logfmt style (key=value pairs)

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 218 of file structured_logger.h.

218 {
219 std::ostringstream oss;
220
221 // Standard fields first
222 oss << "level=" << level_to_string(entry.level);
223 oss << " ts=" << format_timestamp_logfmt(entry.timestamp);
224
225 if (!entry.message.empty()) {
226 oss << " msg=" << escape_logfmt_value(entry.message);
227 }
228
229 // Custom fields
230 for (const auto& [key, value] : entry.fields) {
231 oss << " " << key << "=";
232 std::visit([&oss](const auto& v) {
233 using T = std::decay_t<decltype(v)>;
234 if constexpr (std::is_same_v<T, std::string>) {
235 oss << escape_logfmt_value(v);
236 } else if constexpr (std::is_same_v<T, bool>) {
237 oss << (v ? "true" : "false");
238 } else {
239 oss << v;
240 }
241 }, value);
242 }
243
244 return oss.str();
245 }
static std::string level_to_string(log_level level)
static std::string escape_logfmt_value(const std::string &value)
static std::string format_timestamp_logfmt(std::chrono::system_clock::time_point tp)

References escape_logfmt_value(), kcenon::logger::structured::structured_log_entry::fields, format_timestamp_logfmt(), kcenon::logger::structured::structured_log_entry::level, level_to_string(), kcenon::logger::structured::structured_log_entry::message, and kcenon::logger::structured::structured_log_entry::timestamp.

Referenced by log_structured().

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

◆ format_timestamp_logfmt()

static std::string kcenon::logger::structured::basic_structured_logger::format_timestamp_logfmt ( std::chrono::system_clock::time_point tp)
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 260 of file structured_logger.h.

260 {
261 auto time_t_val = std::chrono::system_clock::to_time_t(tp);
262 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
263 tp.time_since_epoch()) % 1000;
264
265 std::ostringstream oss;
266 oss << std::put_time(std::gmtime(&time_t_val), "%Y-%m-%dT%H:%M:%S");
267 oss << "." << std::setfill('0') << std::setw(3) << ms.count() << "Z";
268 return oss.str();
269 }

Referenced by format_logfmt().

Here is the caller graph for this function:

◆ level_to_string()

static std::string kcenon::logger::structured::basic_structured_logger::level_to_string ( log_level level)
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 247 of file structured_logger.h.

247 {
248 switch (level) {
249 case log_level::trace: return "trace";
250 case log_level::debug: return "debug";
251 case log_level::info: return "info";
252 case log_level::warning: return "warn";
253 case log_level::error: return "error";
254 case log_level::critical: return "fatal";
255 case log_level::off: return "off";
256 default: return "unknown";
257 }
258 }

Referenced by format_logfmt().

Here is the caller graph for this function:

◆ log_structured()

void kcenon::logger::structured::basic_structured_logger::log_structured ( const structured_log_entry & entry)
inlineoverridevirtual

Log a structured message.

Implements kcenon::logger::structured::structured_logger_interface.

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 189 of file structured_logger.h.

189 {
190 std::string formatted;
191
192 switch (format_) {
194 formatted = json_formatter::format(entry);
195 break;
197 formatted = format_logfmt(entry);
198 break;
199 }
200
201 // Output the formatted log
202 if (output_callback_) {
203 output_callback_(entry.level, formatted);
204 } else {
205 auto& out = output_to_stderr_ ? std::cerr : std::cout;
206 out << formatted << std::endl;
207 }
208 }
static std::string format_logfmt(const structured_log_entry &entry)
Format entry in logfmt style (key=value pairs)
static std::string format(const structured_log_entry &entry)

References kcenon::logger::structured::json_formatter::format(), format_, format_logfmt(), kcenon::logger::structured::json, kcenon::logger::structured::structured_log_entry::level, kcenon::logger::structured::logfmt, output_callback_, and output_to_stderr_.

Here is the call graph for this function:

◆ set_format()

void kcenon::logger::structured::basic_structured_logger::set_format ( structured_format format)
inline

Set the output format.

Parameters
formatThe format to use (json or logfmt)
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 169 of file structured_logger.h.

169 {
170 format_ = format;
171 }

References format_.

◆ set_output_callback()

void kcenon::logger::structured::basic_structured_logger::set_output_callback ( structured_output_callback callback)
inline

Set a custom output callback.

Parameters
callbackFunction to call with formatted log output
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 177 of file structured_logger.h.

177 {
178 output_callback_ = std::move(callback);
179 }

References output_callback_.

◆ set_output_to_stderr()

void kcenon::logger::structured::basic_structured_logger::set_output_to_stderr ( bool use_stderr)
inline

Set whether to output to stderr (default: stdout)

Parameters
use_stderrIf true, output to stderr; otherwise stdout
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 185 of file structured_logger.h.

185 {
186 output_to_stderr_ = use_stderr;
187 }

References output_to_stderr_.

◆ start_log()

log_builder kcenon::logger::structured::basic_structured_logger::start_log ( log_level level)
inlineoverridevirtual

Start building a structured log entry.

Implements kcenon::logger::structured::structured_logger_interface.

Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 210 of file structured_logger.h.

210 {
211 return log_builder(level, this);
212 }

Member Data Documentation

◆ format_

structured_format kcenon::logger::structured::basic_structured_logger::format_ = structured_format::json
private

◆ output_callback_

structured_output_callback kcenon::logger::structured::basic_structured_logger::output_callback_
private

◆ output_to_stderr_

bool kcenon::logger::structured::basic_structured_logger::output_to_stderr_ = false
private

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