13 : logger_(&log), saved_context_(log.context()) {
22 set(key, value, category);
42 if (value && category) {
43 ctx.set(key, *value, *category);
49 : logger_(other.logger_),
50 saved_context_(std::move(other.saved_context_)),
51 tracked_keys_(std::move(other.tracked_keys_)) {
52 other.logger_ =
nullptr;
59 auto& ctx = logger_->context();
60 for (
const auto& key : tracked_keys_) {
63 for (
const auto& key : saved_context_.keys()) {
64 auto value = saved_context_.get(key);
65 auto category = saved_context_.get_category(key);
66 if (value && category) {
67 ctx.set(key, *value, *category);
73 logger_ = other.logger_;
74 saved_context_ = std::move(other.saved_context_);
75 tracked_keys_ = std::move(other.tracked_keys_);
76 other.logger_ =
nullptr;
92 std::string_view trace_id,
93 std::string_view span_id,
94 std::optional<std::string_view> parent_span_id) {
107 std::string_view request_id,
108 std::optional<std::string_view> correlation_id) {
112 if (correlation_id) {
140 std::string key_str(key);
unified_log_context & context()
RAII guard for automatic context management.
scoped_context_guard & operator=(const scoped_context_guard &)=delete
scoped_context_guard & set(std::string_view key, context_value value, context_category category=context_category::custom)
Set a context value.
~scoped_context_guard()
Destructor - restores previous context.
scoped_context_guard & set_request(std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt)
Set request context.
scoped_context_guard & 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 saved_context_
std::vector< std::string > tracked_keys_
scoped_context_guard & set_otel(const otlp::otel_context &ctx)
Set OpenTelemetry context.
scoped_context_guard(logger &log)
Construct guard and save current context.
void track_key(std::string_view key)
Track a key as added or overridden.
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.
std::vector< std::string > keys() const
Get all keys in the context.
unified_log_context & set_otel(const otlp::otel_context &ctx)
Set OpenTelemetry context.
unified_log_context & set_request(std::string_view request_id, std::optional< std::string_view > correlation_id=std::nullopt)
Set request context.
std::optional< context_value > get(std::string_view key) const
Get a context value by key.
std::optional< context_category > get_category(std::string_view key) const
Get the category of a context entry.
unified_log_context & set(std::string_view key, context_value value, context_category category=context_category::custom)
Set a context value.
High-performance, thread-safe logging system with asynchronous capabilities.
std::variant< std::monostate, bool, int64_t, double, std::string > context_value
Value type for unified context storage.
context_category
Categories for organizing context entries.
RAII guard for automatic context restoration.
OpenTelemetry context for trace correlation.