|
Logger System 0.1.3
High-performance C++20 thread-safe logging system with asynchronous capabilities
|
Unified interface for managing all types of logging context. More...
#include <unified_log_context.h>

Classes | |
| struct | entry |
| Internal entry structure. More... | |
Public Member Functions | |
| unified_log_context ()=default | |
| Default constructor. | |
| unified_log_context (const unified_log_context &other) | |
| Copy constructor. | |
| unified_log_context (unified_log_context &&other) noexcept | |
| Move constructor. | |
| unified_log_context & | operator= (const unified_log_context &other) |
| Copy assignment operator. | |
| unified_log_context & | operator= (unified_log_context &&other) noexcept |
| Move assignment operator. | |
| ~unified_log_context ()=default | |
| Destructor. | |
| unified_log_context & | set (std::string_view key, context_value value, context_category category=context_category::custom) |
| Set a context value. | |
| unified_log_context & | set_trace (std::string_view trace_id, std::string_view span_id, std::optional< std::string_view > parent_span_id=std::nullopt) |
| Set trace context. | |
| unified_log_context & | set_request (std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt) |
| Set request context. | |
| unified_log_context & | set_otel (const otlp::otel_context &ctx) |
| Set OpenTelemetry context. | |
| std::optional< context_value > | get (std::string_view key) const |
| Get a context value by key. | |
| template<typename T > | |
| std::optional< T > | get_as (std::string_view key) const |
| std::string | get_string (std::string_view key, std::string_view default_value="") const |
| Get a context value as string. | |
| std::optional< context_category > | get_category (std::string_view key) const |
| Get the category of a context entry. | |
| bool | has (std::string_view key) const |
| Check if a key exists in the context. | |
| bool | empty () const |
| Check if the context is empty. | |
| size_t | size () const |
| Get the number of entries. | |
| std::vector< std::string > | keys () const |
| Get all keys in the context. | |
| std::vector< std::string > | keys (context_category category) const |
| Get keys for a specific category. | |
| void | remove (std::string_view key) |
| Remove a specific key from the context. | |
| void | clear () |
| Clear all entries from the context. | |
| void | clear (context_category category) |
| Clear entries of a specific category. | |
| log_fields | to_fields () const |
| Export context to log_fields format. | |
| unified_log_context & | merge (const unified_log_context &other, bool overwrite=true) |
| Merge another context into this one. | |
Private Attributes | |
| std::shared_mutex | mutex_ |
| std::unordered_map< std::string, entry > | data_ |
Unified interface for managing all types of logging context.
This class provides a single point of access for all logging context operations. It consolidates what was previously spread across multiple storage mechanisms into one coherent interface.
Thread Safety: All methods are thread-safe. Read operations use shared locks, write operations use exclusive locks.
Definition at line 117 of file unified_log_context.h.
|
default |
Default constructor.
| kcenon::logger::unified_log_context::unified_log_context | ( | const unified_log_context & | other | ) |
Copy constructor.
| other | Context to copy from |
Definition at line 12 of file unified_log_context.cpp.
|
noexcept |
Move constructor.
| other | Context to move from |
Definition at line 17 of file unified_log_context.cpp.
|
default |
Destructor.
| void kcenon::logger::unified_log_context::clear | ( | ) |
Clear all entries from the context.
Definition at line 174 of file unified_log_context.cpp.
| void kcenon::logger::unified_log_context::clear | ( | context_category | category | ) |
Clear entries of a specific category.
| category | Category to clear |
Definition at line 179 of file unified_log_context.cpp.
|
nodiscard |
|
nodiscard |
Get a context value by key.
| key | Field name |
Definition at line 95 of file unified_log_context.cpp.
Referenced by get_string(), and kcenon::logger::scoped_context_guard::~scoped_context_guard().

|
inlinenodiscard |
Definition at line 235 of file unified_log_context.h.
|
nodiscard |
Get the category of a context entry.
| key | Field name |
Definition at line 116 of file unified_log_context.cpp.
Referenced by kcenon::logger::scoped_context_guard::~scoped_context_guard().

|
nodiscard |
Get a context value as string.
| key | Field name |
| default_value | Value to return if not found |
Returns the value if it's a string, or the default value if not found or if the value is not a string type.
Definition at line 104 of file unified_log_context.cpp.
References get().

|
nodiscard |
|
nodiscard |
Get all keys in the context.
Definition at line 144 of file unified_log_context.cpp.
Referenced by kcenon::logger::scoped_context_guard::~scoped_context_guard().

|
nodiscard |
Get keys for a specific category.
| category | Category to filter by |
Definition at line 154 of file unified_log_context.cpp.
References kcenon::logger::unified_log_context::entry::category, data_, and mutex_.
| unified_log_context & kcenon::logger::unified_log_context::merge | ( | const unified_log_context & | other, |
| bool | overwrite = true ) |
Merge another context into this one.
| other | Context to merge from |
| overwrite | If true, other's values overwrite existing keys |
Copies all entries from other into this context. If overwrite is true (default), existing keys are replaced. If overwrite is false, existing keys are preserved.
Definition at line 218 of file unified_log_context.cpp.
| unified_log_context & kcenon::logger::unified_log_context::operator= | ( | const unified_log_context & | other | ) |
Copy assignment operator.
| other | Context to copy from |
Definition at line 22 of file unified_log_context.cpp.
|
noexcept |
Move assignment operator.
| other | Context to move from |
Definition at line 30 of file unified_log_context.cpp.
| void kcenon::logger::unified_log_context::remove | ( | std::string_view | key | ) |
Remove a specific key from the context.
| key | Field name to remove |
Definition at line 169 of file unified_log_context.cpp.
Referenced by kcenon::logger::log_context_scope::remove_logger_context().

| unified_log_context & kcenon::logger::unified_log_context::set | ( | std::string_view | key, |
| context_value | value, | ||
| context_category | category = context_category::custom ) |
Set a context value.
| key | Field name |
| value | Field value |
| category | Context category (default: custom) |
Sets a key-value pair in the context. If the key already exists, its value and category are updated.
Definition at line 42 of file unified_log_context.cpp.
Referenced by kcenon::logger::log_context_scope::log_context_scope(), and kcenon::logger::scoped_context_guard::set().

| unified_log_context & kcenon::logger::unified_log_context::set_otel | ( | const otlp::otel_context & | ctx | ) |
Set OpenTelemetry context.
| ctx | OpenTelemetry context structure |
Imports all fields from an otel_context structure with context_category::otel.
Definition at line 74 of file unified_log_context.cpp.
References data_, mutex_, kcenon::logger::otel, kcenon::logger::otlp::otel_context::span_id, kcenon::logger::otlp::otel_context::trace_flags, kcenon::logger::otlp::otel_context::trace_id, and kcenon::logger::otlp::otel_context::trace_state.
Referenced by kcenon::logger::scoped_context_guard::set_otel().

| unified_log_context & kcenon::logger::unified_log_context::set_request | ( | std::string_view | request_id, |
| std::optional< std::string_view > | correlation_id = std::nullopt ) |
Set request context.
| request_id | Request identifier |
| correlation_id | Optional correlation identifier |
Sets request_id and optionally correlation_id with context_category::request. These are used for request tracking.
Definition at line 63 of file unified_log_context.cpp.
References data_, mutex_, and kcenon::logger::request.
Referenced by kcenon::logger::scoped_context_guard::set_request().

| unified_log_context & kcenon::logger::unified_log_context::set_trace | ( | std::string_view | trace_id, |
| std::string_view | span_id, | ||
| std::optional< std::string_view > | parent_span_id = std::nullopt ) |
Set trace context.
| trace_id | Trace identifier (32 hex chars) |
| span_id | Span identifier (16 hex chars) |
| parent_span_id | Optional parent span identifier |
Sets trace_id, span_id, and optionally parent_span_id with context_category::trace. These are used for distributed tracing.
Definition at line 50 of file unified_log_context.cpp.
References data_, mutex_, and kcenon::logger::trace.
Referenced by kcenon::logger::scoped_context_guard::set_trace().

|
nodiscard |
|
nodiscard |
Export context to log_fields format.
Converts all context entries to the log_fields format used by the structured logging system. Values are converted as follows:
Definition at line 194 of file unified_log_context.cpp.
References data_, mutex_, and kcenon::logger::unified_log_context::entry::value.
|
private |
Definition at line 362 of file unified_log_context.h.
Referenced by clear(), clear(), empty(), get(), get_category(), has(), keys(), keys(), merge(), operator=(), remove(), set(), set_otel(), set_request(), set_trace(), size(), to_fields(), and unified_log_context().
|
mutableprivate |
Definition at line 361 of file unified_log_context.h.
Referenced by clear(), clear(), empty(), get(), get_category(), has(), keys(), keys(), merge(), operator=(), remove(), set(), set_otel(), set_request(), set_trace(), size(), to_fields(), and unified_log_context().