Skip to content

Commit 9eadb32

Browse files
JAORMXclaude
andcommitted
Redirect toolhive slog singleton to log file
Toolhive's pkg/logger maintains its own slog singleton (set during init()) that writes JSON to stderr. The existing initToolhiveLogger only redirected the zap global logger, so all vmcp diagnostics (aggregator, discovery, server) still polluted the terminal session. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4dde685 commit 9eadb32

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

internal/infra/mcp/provider.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ import (
1515
"go.uber.org/zap/zapcore"
1616

1717
"github.com/stacklok/toolhive-core/env"
18+
"github.com/stacklok/toolhive-core/logging"
1819
thvauth "github.com/stacklok/toolhive/pkg/auth"
1920
"github.com/stacklok/toolhive/pkg/groups"
21+
thvlogger "github.com/stacklok/toolhive/pkg/logger"
2022
"github.com/stacklok/toolhive/pkg/vmcp"
2123
"github.com/stacklok/toolhive/pkg/vmcp/aggregator"
2224
vmcpauthfactory "github.com/stacklok/toolhive/pkg/vmcp/auth/factory"
@@ -205,16 +207,23 @@ func (r *statusRecorder) WriteHeader(code int) {
205207
r.ResponseWriter.WriteHeader(code)
206208
}
207209

208-
// initToolhiveLogger configures the zap global logger used by toolhive
209-
// internals to write to the given writer (typically the log file).
210-
// If w is nil a no-op logger is installed.
210+
// initToolhiveLogger redirects both the zap global logger and the toolhive
211+
// slog singleton to the given writer (typically the apiary log file) so that
212+
// vmcp diagnostics don't pollute stdout/stderr during the terminal session.
213+
// If w is nil, no-op loggers are installed for both.
211214
func initToolhiveLogger(w io.Writer) {
212215
if w == nil {
213216
zap.ReplaceGlobals(zap.NewNop())
217+
thvlogger.Set(slog.New(slog.NewJSONHandler(io.Discard, nil)))
214218
return
215219
}
216220

221+
// Redirect zap (legacy toolhive code paths).
217222
enc := zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig())
218223
core := zapcore.NewCore(enc, zapcore.AddSync(w), zap.DebugLevel)
219224
zap.ReplaceGlobals(zap.New(core))
225+
226+
// Redirect the toolhive slog singleton — this is the primary logger used
227+
// by vmcp internals (aggregator, discovery, server, etc.).
228+
thvlogger.Set(logging.New(logging.WithOutput(w)))
220229
}

0 commit comments

Comments
 (0)