Skip to content

Commit a36c68f

Browse files
log: improve documentation (#26753)
Add usage examples
1 parent f6a7cc6 commit a36c68f

File tree

2 files changed

+95
-1
lines changed

2 files changed

+95
-1
lines changed

log/logger.go

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,58 @@ type Logger interface {
116116
// SetHandler updates the logger to write records to the specified handler.
117117
SetHandler(h Handler)
118118

119-
// Log a message at the given level with context key/value pairs
119+
// Log a message at the trace level with context key/value pairs
120+
//
121+
// # Usage
122+
//
123+
// log.Trace("msg")
124+
// log.Trace("msg", "key1", val1)
125+
// log.Trace("msg", "key1", val1, "key2", val2)
120126
Trace(msg string, ctx ...interface{})
127+
128+
// Log a message at the debug level with context key/value pairs
129+
//
130+
// # Usage Examples
131+
//
132+
// log.Debug("msg")
133+
// log.Debug("msg", "key1", val1)
134+
// log.Debug("msg", "key1", val1, "key2", val2)
121135
Debug(msg string, ctx ...interface{})
136+
137+
// Log a message at the info level with context key/value pairs
138+
//
139+
// # Usage Examples
140+
//
141+
// log.Info("msg")
142+
// log.Info("msg", "key1", val1)
143+
// log.Info("msg", "key1", val1, "key2", val2)
122144
Info(msg string, ctx ...interface{})
145+
146+
// Log a message at the warn level with context key/value pairs
147+
//
148+
// # Usage Examples
149+
//
150+
// log.Warn("msg")
151+
// log.Warn("msg", "key1", val1)
152+
// log.Warn("msg", "key1", val1, "key2", val2)
123153
Warn(msg string, ctx ...interface{})
154+
155+
// Log a message at the error level with context key/value pairs
156+
//
157+
// # Usage Examples
158+
//
159+
// log.Error("msg")
160+
// log.Error("msg", "key1", val1)
161+
// log.Error("msg", "key1", val1, "key2", val2)
124162
Error(msg string, ctx ...interface{})
163+
164+
// Log a message at the crit level with context key/value pairs, and then exit.
165+
//
166+
// # Usage Examples
167+
//
168+
// log.Crit("msg")
169+
// log.Crit("msg", "key1", val1)
170+
// log.Crit("msg", "key1", val1, "key2", val2)
125171
Crit(msg string, ctx ...interface{})
126172
}
127173

log/root.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,79 @@ func Root() Logger {
3030
// runtime.Caller(2) always refers to the call site in client code.
3131

3232
// Trace is a convenient alias for Root().Trace
33+
//
34+
// Log a message at the trace level with context key/value pairs
35+
//
36+
// # Usage
37+
//
38+
// log.Trace("msg")
39+
// log.Trace("msg", "key1", val1)
40+
// log.Trace("msg", "key1", val1, "key2", val2)
3341
func Trace(msg string, ctx ...interface{}) {
3442
root.write(msg, LvlTrace, ctx, skipLevel)
3543
}
3644

3745
// Debug is a convenient alias for Root().Debug
46+
//
47+
// Log a message at the debug level with context key/value pairs
48+
//
49+
// # Usage Examples
50+
//
51+
// log.Debug("msg")
52+
// log.Debug("msg", "key1", val1)
53+
// log.Debug("msg", "key1", val1, "key2", val2)
3854
func Debug(msg string, ctx ...interface{}) {
3955
root.write(msg, LvlDebug, ctx, skipLevel)
4056
}
4157

4258
// Info is a convenient alias for Root().Info
59+
//
60+
// Log a message at the info level with context key/value pairs
61+
//
62+
// # Usage Examples
63+
//
64+
// log.Info("msg")
65+
// log.Info("msg", "key1", val1)
66+
// log.Info("msg", "key1", val1, "key2", val2)
4367
func Info(msg string, ctx ...interface{}) {
4468
root.write(msg, LvlInfo, ctx, skipLevel)
4569
}
4670

4771
// Warn is a convenient alias for Root().Warn
72+
//
73+
// Log a message at the warn level with context key/value pairs
74+
//
75+
// # Usage Examples
76+
//
77+
// log.Warn("msg")
78+
// log.Warn("msg", "key1", val1)
79+
// log.Warn("msg", "key1", val1, "key2", val2)
4880
func Warn(msg string, ctx ...interface{}) {
4981
root.write(msg, LvlWarn, ctx, skipLevel)
5082
}
5183

5284
// Error is a convenient alias for Root().Error
85+
//
86+
// Log a message at the error level with context key/value pairs
87+
//
88+
// # Usage Examples
89+
//
90+
// log.Error("msg")
91+
// log.Error("msg", "key1", val1)
92+
// log.Error("msg", "key1", val1, "key2", val2)
5393
func Error(msg string, ctx ...interface{}) {
5494
root.write(msg, LvlError, ctx, skipLevel)
5595
}
5696

5797
// Crit is a convenient alias for Root().Crit
98+
//
99+
// Log a message at the crit level with context key/value pairs, and then exit.
100+
//
101+
// # Usage Examples
102+
//
103+
// log.Crit("msg")
104+
// log.Crit("msg", "key1", val1)
105+
// log.Crit("msg", "key1", val1, "key2", val2)
58106
func Crit(msg string, ctx ...interface{}) {
59107
root.write(msg, LvlCrit, ctx, skipLevel)
60108
os.Exit(1)

0 commit comments

Comments
 (0)