diff options
| author | Stefan Boberg <[email protected]> | 2026-03-23 12:53:58 +0100 |
|---|---|---|
| committer | GitHub Enterprise <[email protected]> | 2026-03-23 12:53:58 +0100 |
| commit | e12feda1272922f6ad3567dd27509f0ec53d3a7b (patch) | |
| tree | 8d238687c643a6790f9f307db3008a0e400caf42 /src/zenserver | |
| parent | Documentation updates (#882) (diff) | |
| download | zen-e12feda1272922f6ad3567dd27509f0ec53d3a7b.tar.xz zen-e12feda1272922f6ad3567dd27509f0ec53d3a7b.zip | |
Logger simplification (#883)
- **`Logger` now holds a single `SinkPtr`** instead of a `std::vector<SinkPtr>`. The `SetSinks`/`AddSink` API is replaced with a single `SetSink`. This removes complexity from `Logger` itself and makes `Clone()` cheaper (no vector copy).
- **New `BroadcastSink`** (`zencore/logging/broadcastsink.h`) acts as a thread-safe, shared indirection point that fans out to a dynamic list of child sinks. Adding or removing a child sink via `AddSink`/`RemoveSink` is immediately visible to every `Logger` that holds a reference to it — including cloned loggers — without requiring each logger to be updated individually.
- **`GetDefaultBroadcastSink()`** (exposed from `zenutil/logging.h`) gives server-layer code access to the shared broadcast sink so it can register optional sinks (OTel, TCP log stream) after logging is initialized, without going through `Default()->AddSink()`.
### Motivation
Previously, dynamically adding sinks post-initialization mutated the default logger's internal sink vector directly. This was fragile: cloned loggers (created before `AddSink` was called) would not pick up the new sinks. `BroadcastSink` fixes this by making the sink list a shared, mutable object that all loggers sharing the same broadcast instance observe uniformly.
Diffstat (limited to 'src/zenserver')
| -rw-r--r-- | src/zenserver/diag/logging.cpp | 19 | ||||
| -rw-r--r-- | src/zenserver/storage/zenstorageserver.cpp | 3 |
2 files changed, 14 insertions, 8 deletions
diff --git a/src/zenserver/diag/logging.cpp b/src/zenserver/diag/logging.cpp index 38b15480a..7513e56f7 100644 --- a/src/zenserver/diag/logging.cpp +++ b/src/zenserver/diag/logging.cpp @@ -6,6 +6,7 @@ #include <zencore/filesystem.h> #include <zencore/fmtutils.h> +#include <zencore/logging/broadcastsink.h> #include <zencore/logging/logger.h> #include <zencore/logging/registry.h> #include <zencore/memory/llm.h> @@ -47,7 +48,7 @@ InitializeServerLogging(const ZenServerConfig& InOptions, bool WithCacheService) /* max size */ 128 * 1024 * 1024, /* max files */ 16, /* rotate on open */ true)); - Ref<logging::Logger> HttpLogger(new logging::Logger("http_requests", std::vector<logging::SinkPtr>{HttpSink})); + Ref<logging::Logger> HttpLogger(new logging::Logger("http_requests", HttpSink)); logging::Registry::Instance().Register(HttpLogger); if (WithCacheService) @@ -60,30 +61,32 @@ InitializeServerLogging(const ZenServerConfig& InOptions, bool WithCacheService) /* max size */ 128 * 1024 * 1024, /* max files */ 16, /* rotate on open */ false)); - Ref<logging::Logger> CacheLogger(new logging::Logger("z$", std::vector<logging::SinkPtr>{CacheSink})); + Ref<logging::Logger> CacheLogger(new logging::Logger("z$", CacheSink)); logging::Registry::Instance().Register(CacheLogger); // Jupiter - only log upstream HTTP traffic to file - Ref<logging::Logger> JupiterLogger(new logging::Logger("jupiter", std::vector<logging::SinkPtr>{FileSink})); + Ref<logging::Logger> JupiterLogger(new logging::Logger("jupiter", FileSink)); logging::Registry::Instance().Register(JupiterLogger); // Zen - only log upstream HTTP traffic to file - Ref<logging::Logger> ZenClientLogger(new logging::Logger("zenclient", std::vector<logging::SinkPtr>{FileSink})); + Ref<logging::Logger> ZenClientLogger(new logging::Logger("zenclient", FileSink)); logging::Registry::Instance().Register(ZenClientLogger); } + Ref<logging::BroadcastSink> BroadcastSink = GetDefaultBroadcastSink(); + #if ZEN_WITH_OTEL - if (!InOptions.LoggingConfig.OtelEndpointUri.empty()) + if (BroadcastSink && !InOptions.LoggingConfig.OtelEndpointUri.empty()) { // TODO: Should sanity check that endpoint is reachable? Also, a valid URI? logging::SinkPtr OtelSink(new zen::logging::OtelHttpProtobufSink(InOptions.LoggingConfig.OtelEndpointUri)); - zen::logging::Default()->AddSink(std::move(OtelSink)); + BroadcastSink->AddSink(std::move(OtelSink)); } #endif - if (!InOptions.LoggingConfig.LogStreamEndpoint.empty()) + if (BroadcastSink && !InOptions.LoggingConfig.LogStreamEndpoint.empty()) { std::string Endpoint = InOptions.LoggingConfig.LogStreamEndpoint; std::string Host = "localhost"; @@ -100,7 +103,7 @@ InitializeServerLogging(const ZenServerConfig& InOptions, bool WithCacheService) if (Port > 0) { logging::SinkPtr StreamSink(new TcpLogStreamSink(Host, Port, "zenserver")); - zen::logging::Default()->AddSink(std::move(StreamSink)); + BroadcastSink->AddSink(std::move(StreamSink)); } } diff --git a/src/zenserver/storage/zenstorageserver.cpp b/src/zenserver/storage/zenstorageserver.cpp index 531853f5d..68d722f60 100644 --- a/src/zenserver/storage/zenstorageserver.cpp +++ b/src/zenserver/storage/zenstorageserver.cpp @@ -13,6 +13,8 @@ #include <zencore/iobuffer.h> #include <zencore/jobqueue.h> #include <zencore/logging.h> +#include <zencore/logging/broadcastsink.h> +#include <zencore/logging/registry.h> #include <zencore/scopeguard.h> #include <zencore/sentryintegration.h> #include <zencore/session.h> @@ -30,6 +32,7 @@ #include <zenstore/vfsimpl.h> #include <zenstore/workspaces.h> #include <zentelemetry/otlptrace.h> +#include <zenutil/logging.h> #include <zenutil/service.h> #include <zenutil/workerpools.h> #include <zenutil/zenserverprocess.h> |