Skip to content

Commit 7a7ab1b

Browse files
committed
docs(gologshim): improve godoc clarity
1 parent 789d14c commit 7a7ab1b

File tree

1 file changed

+44
-8
lines changed

1 file changed

+44
-8
lines changed

gologshim/gologshim.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,50 @@
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.
34
//
4-
// Usage:
5+
// # Usage
56
//
6-
// var log = logging.Logger("subsystem")
7+
// Create loggers using the Logger function:
8+
//
9+
// var log = gologshim.Logger("subsystem")
710
// log.Debug("message", "key", "value")
811
//
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:
1247
//
13-
// Environment variables (see go-log README for full details):
1448
// - GOLOG_LOG_LEVEL: Set log levels per subsystem (e.g., "error,ping=debug")
1549
// - GOLOG_LOG_FORMAT/GOLOG_LOG_FMT: Output format ("json" or text)
1650
// - GOLOG_LOG_ADD_SOURCE: Include source location (default: true)
@@ -64,6 +98,8 @@ func (h *dynamicHandler) ensureHandler() slog.Handler {
6498
h.handler = h.createFallbackHandler()
6599
}
66100
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.
67103
attrs = append(attrs, slog.String("logger", h.system))
68104
attrs = append(attrs, h.config.labels...)
69105
h.handler = h.handler.WithAttrs(attrs)

0 commit comments

Comments
 (0)