Skip to content

Agent Mesh

cmd: share the go-log setup in internal/logging

Tomas Tormo3671adab6aca
diff --git a/cmd/agentmesh-control-plane/main.go b/cmd/agentmesh-control-plane/main.go
index dd3731ab..7d0893a8 100644
--- a/cmd/agentmesh-control-plane/main.go
+++ b/cmd/agentmesh-control-plane/main.go
@@ -25,6 +25,7 @@ import (
"github.com/google/agentmesh/api"
"github.com/google/agentmesh/internal/controlplane"
+ "github.com/google/agentmesh/internal/logging"
"github.com/google/agentmesh/internal/pprofserver"
"github.com/google/agentmesh/internal/secrets"
"github.com/google/agentmesh/internal/storage"
@@ -78,20 +79,7 @@ func main() {
return nil
},
Run: func(cmd *cobra.Command, args []string) {
- // Initialize logging
- // go-log read GOLOG_LOG_FMT at package init, before a Setenv here could land.
- if os.Getenv("LOG_FORMAT") == "json" {
- cfg := golog.GetConfig()
- cfg.Format = golog.JSONOutput
- golog.SetupLogging(cfg)
- }
- golog.SetAllLoggers(golog.LevelInfo)
- if logLevel != "" {
- lvl, err := golog.LevelFromString(logLevel)
- if err == nil {
- golog.SetAllLoggers(lvl)
- }
- }
+ logging.Setup(logLevel, "")
if oidcIssuer == "" && workloadIssuer == "" {
logger.Fatalf("OIDC issuer is required (use --issuer or --workload-issuer flag)")
diff --git a/cmd/agentmesh-node/main.go b/cmd/agentmesh-node/main.go
index f6c396da..2569dd43 100644
--- a/cmd/agentmesh-node/main.go
+++ b/cmd/agentmesh-node/main.go
@@ -29,6 +29,7 @@ import (
"time"
"github.com/google/agentmesh/api"
+ "github.com/google/agentmesh/internal/logging"
"github.com/google/agentmesh/internal/node"
"github.com/google/agentmesh/internal/pprofserver"
"github.com/google/agentmesh/internal/secrets"
@@ -313,23 +314,8 @@ func main() {
}
return
}
- // Configure logging to duplicate to ring buffer
- cfg := golog.GetConfig()
- cfg.URL = "ringbuffer://"
- golog.SetupLogging(cfg)
-
- // Initialize logging
- golog.SetAllLoggers(golog.LevelInfo)
- if logLevelFlag != "" {
- lvl, err := golog.LevelFromString(logLevelFlag)
- if err == nil {
- golog.SetAllLoggers(lvl)
- }
- }
-
- // Suppress noisy DHT logs
- _ = golog.SetLogLevel("dht", "fatal")
- _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")
+ // Duplicate logs to the ring buffer served by /debug/logs.
+ logging.Setup(logLevelFlag, "ringbuffer://")
apiTokenFlag = resolveDaemonSecret("api-token", apiTokenPathFlag, "AGENTMESH_API_TOKEN")
bootstrapTokenFlag = resolveSecretFlag("bootstrap-token", bootstrapTokenFlag, bootstrapTokenPathFlag)
diff --git a/cmd/agentmesh-one/main.go b/cmd/agentmesh-one/main.go
index 09f8e18e..73d5f81e 100644
--- a/cmd/agentmesh-one/main.go
+++ b/cmd/agentmesh-one/main.go
@@ -29,6 +29,7 @@ import (
"syscall"
"github.com/google/agentmesh/api"
+ "github.com/google/agentmesh/internal/logging"
"github.com/google/agentmesh/internal/router"
"github.com/google/agentmesh/internal/standalone"
"github.com/google/agentmesh/internal/tunnel"
@@ -74,27 +75,7 @@ func main() {
Short: "Agent Mesh - all-in-one standalone server",
Version: version.String(),
Run: func(cmd *cobra.Command, args []string) {
- // go-log read GOLOG_LOG_FMT at package init, before a Setenv here could land.
- if os.Getenv("LOG_FORMAT") == "json" {
- cfg := golog.GetConfig()
- cfg.Format = golog.JSONOutput
- golog.SetupLogging(cfg)
- }
- lvl := golog.LevelInfo
- if logLevel != "" {
- if parsed, err := golog.LevelFromString(logLevel); err == nil {
- lvl = parsed
- }
- }
- golog.SetAllLoggers(lvl)
- // Same as agentmesh-node: go-libp2p-kad-dht logs routine small-mesh
- // conditions (empty routing table, no closer peers) at INFO/ERROR,
- // the latter through a malformed zap call inside the library.
- // Only an explicit debug level keeps them.
- if lvl > golog.LevelDebug {
- _ = golog.SetLogLevel("dht", "fatal")
- _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")
- }
+ logging.Setup(logLevel, "")
// Secrets arrive through a file or the environment, never as a
// flag value that would sit in `ps` and shell history. Env keeps
diff --git a/cmd/agentmesh-router/main.go b/cmd/agentmesh-router/main.go
index aa597af3..a7da7fcd 100644
--- a/cmd/agentmesh-router/main.go
+++ b/cmd/agentmesh-router/main.go
@@ -23,6 +23,7 @@ import (
"time"
"github.com/google/agentmesh/api"
+ "github.com/google/agentmesh/internal/logging"
"github.com/google/agentmesh/internal/pprofserver"
"github.com/google/agentmesh/internal/router"
"github.com/google/agentmesh/internal/version"
@@ -68,20 +69,7 @@ func main() {
Short: "Agent Mesh - libp2p Router Node",
Version: version.String(),
Run: func(cmd *cobra.Command, args []string) {
- // Initialize logging
- // go-log read GOLOG_LOG_FMT at package init, before a Setenv here could land.
- if os.Getenv("LOG_FORMAT") == "json" {
- cfg := golog.GetConfig()
- cfg.Format = golog.JSONOutput
- golog.SetupLogging(cfg)
- }
- golog.SetAllLoggers(golog.LevelInfo)
- if logLevel != "" {
- lvl, err := golog.LevelFromString(logLevel)
- if err == nil {
- golog.SetAllLoggers(lvl)
- }
- }
+ logging.Setup(logLevel, "")
opts := router.Options{
ControlPlaneURL: controlPlaneURL,
diff --git a/internal/logging/logging.go b/internal/logging/logging.go
new file mode 100644
index 00000000..1108ee80
--- /dev/null
+++ b/internal/logging/logging.go
@@ -0,0 +1,61 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// Package logging configures go-log the same way in every binary.
+package logging
+
+import (
+ "os"
+
+ golog "github.com/ipfs/go-log/v2"
+)
+
+var logger = golog.Logger("logging")
+
+// Setup applies the sink URL, the LOG_FORMAT=json bridge and the log level.
+// go-log reads GOLOG_LOG_FMT at package init, before main runs, so the
+// config has to be re-applied here through SetupLogging.
+func Setup(level, sinkURL string) {
+ cfg := golog.GetConfig()
+ if sinkURL != "" {
+ cfg.URL = sinkURL
+ }
+ if os.Getenv("LOG_FORMAT") == "json" {
+ cfg.Format = golog.JSONOutput
+ }
+ golog.SetupLogging(cfg)
+
+ lvl := golog.LevelInfo
+ var levelErr error
+ if level != "" {
+ if parsed, err := golog.LevelFromString(level); err == nil {
+ lvl = parsed
+ } else {
+ levelErr = err
+ }
+ }
+ golog.SetAllLoggers(lvl)
+ // Warn after SetAllLoggers, which sets this logger to a level that lets
+ // the warning through.
+ if levelErr != nil {
+ logger.Warnf("invalid log level %q, falling back to info", level)
+ }
+ // go-libp2p-kad-dht logs routine small-mesh conditions (empty routing
+ // table, no closer peers) at INFO/ERROR, the latter through a malformed
+ // zap call inside the library. Only an explicit debug level keeps them.
+ if lvl > golog.LevelDebug {
+ _ = golog.SetLogLevel("dht", "fatal")
+ _ = golog.SetLogLevel("dht/RtRefreshManager", "fatal")
+ }
+}
diff --git a/internal/logging/logging_test.go b/internal/logging/logging_test.go
new file mode 100644
index 00000000..1f62c6ac
--- /dev/null
+++ b/internal/logging/logging_test.go
@@ -0,0 +1,59 @@
+// Copyright 2026 Google LLC
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package logging
+
+import (
+ "testing"
+
+ golog "github.com/ipfs/go-log/v2"
+ "go.uber.org/zap/zapcore"
+)
+
+func TestSetupFormatBridge(t *testing.T) {
+ t.Setenv("LOG_FORMAT", "json")
+ Setup("info", "")
+ if got := golog.GetConfig().Format; got != golog.JSONOutput {
+ t.Fatalf("format = %v, want %v (JSONOutput)", got, golog.JSONOutput)
+ }
+}
+
+func TestSetupLevels(t *testing.T) {
+ dht := golog.Logger("dht")
+ app := golog.Logger("logging-test-app")
+
+ Setup("info", "")
+ if !app.Desugar().Core().Enabled(zapcore.InfoLevel) {
+ t.Error("info level not applied to ordinary loggers")
+ }
+ if dht.Desugar().Core().Enabled(zapcore.ErrorLevel) {
+ t.Error("dht logger not suppressed at info level")
+ }
+
+ Setup("debug", "")
+ if !dht.Desugar().Core().Enabled(zapcore.ErrorLevel) {
+ t.Error("explicit debug level must keep dht logs")
+ }
+}
+
+func TestSetupInvalidLevelFallsBackToInfo(t *testing.T) {
+ app := golog.Logger("logging-test-fallback")
+ Setup("degub", "")
+ if !app.Desugar().Core().Enabled(zapcore.InfoLevel) {
+ t.Error("invalid level must fall back to info, got stricter")
+ }
+ if app.Desugar().Core().Enabled(zapcore.DebugLevel) {
+ t.Error("invalid level must fall back to info, got debug")
+ }
+}
diff --git a/site/content/docs/reference/agentmesh-node.md b/site/content/docs/reference/agentmesh-node.md
index 9609146d..8f4dfc85 100644
--- a/site/content/docs/reference/agentmesh-node.md
+++ b/site/content/docs/reference/agentmesh-node.md
@@ -127,7 +127,7 @@ explains how to enroll.
| `--monitor-bootstrap` | `2m` | Delay before the router-connection monitor starts. |
| `--monitor-interval` | `1m` | How often the monitor checks that a router is connected. |
| `--autorelay-min-interval`, `--autorelay-boot-delay`, `--autorelay-backoff` | `30s`, `0s`, `3s` | libp2p AutoRelay tuning. |
-| `--log-level` | `info` | `debug`, `info`, `warn` or `error`. |
+| `--log-level` | `info` | `debug`, `info`, `warn` or `error`. `LOG_FORMAT=json` selects JSON output. |
## `reset`