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

JSON formatter for structured logs. More...

#include <structured_logger.h>

Collaboration diagram for kcenon::logger::structured::json_formatter:
Collaboration graph

Static Public Member Functions

static std::string format (const structured_log_entry &entry)
 

Static Private Member Functions

static std::string level_to_string (log_level level)
 
static std::string format_timestamp_iso8601 (std::chrono::system_clock::time_point tp)
 
static std::string escape_json_string (const std::string &s)
 
static std::string escape_json_key (const std::string &key)
 

Detailed Description

JSON formatter for structured logs.

Produces valid JSON output with proper escaping and ISO 8601 timestamps.

See also
basic_structured_logger
structured_log_entry

Definition at line 310 of file structured_logger.h.

Member Function Documentation

◆ escape_json_key()

static std::string kcenon::logger::structured::json_formatter::escape_json_key ( const std::string & key)
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 392 of file structured_logger.h.

392 {
393 // Keys should be simple identifiers, but escape just in case
394 std::string result;
395 for (char c : key) {
396 if (c == '"' || c == '\\') {
397 result += '\\';
398 }
399 result += c;
400 }
401 return result;
402 }

Referenced by format().

Here is the caller graph for this function:

◆ escape_json_string()

static std::string kcenon::logger::structured::json_formatter::escape_json_string ( const std::string & s)
inlinestaticprivate
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 368 of file structured_logger.h.

368 {
369 std::ostringstream oss;
370 oss << '"';
371 for (char c : s) {
372 switch (c) {
373 case '"': oss << "\\\""; break;
374 case '\\': oss << "\\\\"; break;
375 case '\b': oss << "\\b"; break;
376 case '\f': oss << "\\f"; break;
377 case '\n': oss << "\\n"; break;
378 case '\r': oss << "\\r"; break;
379 case '\t': oss << "\\t"; break;
380 default:
381 if (static_cast<unsigned char>(c) < 0x20) {
382 oss << "\\u" << std::hex << std::setfill('0') << std::setw(4) << static_cast<int>(c);
383 } else {
384 oss << c;
385 }
386 }
387 }
388 oss << '"';
389 return oss.str();
390 }

Referenced by format().

Here is the caller graph for this function:

◆ format()

static std::string kcenon::logger::structured::json_formatter::format ( const structured_log_entry & entry)
inlinestatic
Examples
/home/runner/work/logger_system/logger_system/include/kcenon/logger/structured/structured_logger.h.

Definition at line 312 of file structured_logger.h.

312 {
313 std::ostringstream json;
314 json << "{";
315 json << "\"timestamp\":\"" << format_timestamp_iso8601(entry.timestamp) << "\",";
316 json << "\"level\":\"" << level_to_string(entry.level) << "\",";
317 json << "\"message\":" << escape_json_string(entry.message);
318
319 // Add custom fields
320 for (const auto& [key, value] : entry.fields) {
321 json << ",\"" << escape_json_key(key) << "\":";
322
323 std::visit([&json](const auto& v) {
324 using T = std::decay_t<decltype(v)>;
325 if constexpr (std::is_same_v<T, std::string>) {
327 } else if constexpr (std::is_same_v<T, bool>) {
328 json << (v ? "true" : "false");
329 } else if constexpr (std::is_same_v<T, int>) {
330 json << v;
331 } else if constexpr (std::is_same_v<T, double>) {
332 json << std::fixed << std::setprecision(6) << v;
333 } else {
334 json << v;
335 }
336 }, value);
337 }
338
339 json << "}";
340 return json.str();
341 }
static std::string escape_json_key(const std::string &key)
static std::string escape_json_string(const std::string &s)
static std::string level_to_string(log_level level)
static std::string format_timestamp_iso8601(std::chrono::system_clock::time_point tp)

References escape_json_key(), escape_json_string(), kcenon::logger::structured::structured_log_entry::fields, format_timestamp_iso8601(), kcenon::logger::structured::json, 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 kcenon::logger::structured::basic_structured_logger::log_structured().

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

◆ format_timestamp_iso8601()

static std::string kcenon::logger::structured::json_formatter::format_timestamp_iso8601 ( 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 357 of file structured_logger.h.

357 {
358 auto time_t_val = std::chrono::system_clock::to_time_t(tp);
359 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
360 tp.time_since_epoch()) % 1000;
361
362 std::ostringstream oss;
363 oss << std::put_time(std::gmtime(&time_t_val), "%Y-%m-%dT%H:%M:%S");
364 oss << "." << std::setfill('0') << std::setw(3) << ms.count() << "Z";
365 return oss.str();
366 }

Referenced by format().

Here is the caller graph for this function:

◆ level_to_string()

static std::string kcenon::logger::structured::json_formatter::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 344 of file structured_logger.h.

344 {
345 switch (level) {
346 case log_level::trace: return "TRACE";
347 case log_level::debug: return "DEBUG";
348 case log_level::info: return "INFO";
349 case log_level::warning: return "WARN";
350 case log_level::error: return "ERROR";
351 case log_level::critical: return "FATAL";
352 case log_level::off: return "OFF";
353 default: return "UNKNOWN";
354 }
355 }

Referenced by format().

Here is the caller graph for this function:

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