|
1 | | -// Package gologshim provides slog-based logging that integrates with go-log |
2 | | -// when available, without requiring go-log as a dependency. |
| 1 | +// Package gologshim provides slog-based logging for go-libp2p that works |
| 2 | +// standalone or integrates with go-log for unified log management across |
| 3 | +// IPFS/libp2p applications, without adding go-log as a dependency. |
3 | 4 | // |
4 | | -// Usage: |
| 5 | +// # Usage |
5 | 6 | // |
6 | | -// var log = logging.Logger("subsystem") |
| 7 | +// Create loggers using the Logger function: |
| 8 | +// |
| 9 | +// var log = gologshim.Logger("subsystem") |
7 | 10 | // log.Debug("message", "key", "value") |
8 | 11 | // |
9 | | -// When go-log's slog bridge is detected (via duck typing), gologshim |
10 | | -// automatically integrates for unified formatting and dynamic level control. |
11 | | -// Otherwise, it creates standalone slog handlers writing to stderr. |
| 12 | +// # Integration with go-log |
| 13 | +// |
| 14 | +// Applications can optionally connect go-libp2p to go-log by calling SetDefaultHandler: |
| 15 | +// |
| 16 | +// func init() { |
| 17 | +// gologshim.SetDefaultHandler(slog.Default().Handler()) |
| 18 | +// } |
| 19 | +// |
| 20 | +// When integrated, go-libp2p logs use go-log's formatting and can be controlled |
| 21 | +// programmatically via go-log's SetLogLevel("subsystem", "level") API to adjust |
| 22 | +// log verbosity per subsystem at runtime without restarting. |
| 23 | +// |
| 24 | +// # Verification (Optional) |
| 25 | +// |
| 26 | +// To ensure correct integration, verify the handler via interface check: |
| 27 | +// |
| 28 | +// func init() { |
| 29 | +// handler := slog.Default().Handler() |
| 30 | +// |
| 31 | +// // Verify it's go-log's bridge |
| 32 | +// type goLogBridge interface { |
| 33 | +// GoLogBridge() |
| 34 | +// } |
| 35 | +// if _, ok := handler.(goLogBridge); !ok { |
| 36 | +// panic("aborting startup: slog.Default() is not go-log's bridge, logs would be missing due to incorrect wiring") |
| 37 | +// } |
| 38 | +// |
| 39 | +// gologshim.SetDefaultHandler(handler) |
| 40 | +// } |
| 41 | +// |
| 42 | +// # Standalone Usage |
| 43 | +// |
| 44 | +// Without calling SetDefaultHandler, gologshim creates standalone slog handlers |
| 45 | +// writing to stderr. This mode is useful when go-log is not present or when you |
| 46 | +// want independent log configuration via backward-compatible (go-log) environment variables: |
12 | 47 | // |
13 | | -// Environment variables (see go-log README for full details): |
14 | 48 | // - GOLOG_LOG_LEVEL: Set log levels per subsystem (e.g., "error,ping=debug") |
15 | 49 | // - GOLOG_LOG_FORMAT/GOLOG_LOG_FMT: Output format ("json" or text) |
16 | 50 | // - GOLOG_LOG_ADD_SOURCE: Include source location (default: true) |
@@ -64,6 +98,8 @@ func (h *dynamicHandler) ensureHandler() slog.Handler { |
64 | 98 | h.handler = h.createFallbackHandler() |
65 | 99 | } |
66 | 100 | attrs := make([]slog.Attr, 0, 1+len(h.config.labels)) |
| 101 | + // Use "logger" attribute for compatibility with go-log's Zap-based format |
| 102 | + // and existing IPFS/libp2p tooling and dashboards. |
67 | 103 | attrs = append(attrs, slog.String("logger", h.system)) |
68 | 104 | attrs = append(attrs, h.config.labels...) |
69 | 105 | h.handler = h.handler.WithAttrs(attrs) |
|
0 commit comments