Logging: Introduce API for contextual logging (#55198)

Introduces a FromContext method on the log.Logger interface that 
allows contextual key/value pairs to be attached, e.g. per request, 
so that any logger using this API will automatically get the per request 
context attached. The proposal makes the traceID available for 
contextual logger , if available, and would allow logs originating from 
a certain HTTP request to be correlated with traceID.
In addition, when tracing not enabled, skip adding
traceID=00000000000000000000000000000000
to logs.
This commit is contained in:
Marcus Efraimsson
2022-09-20 18:32:06 +02:00
committed by GitHub
parent c6ed7d6741
commit 862a6a2fa6
17 changed files with 202 additions and 55 deletions
+12 -1
View File
@@ -1,5 +1,7 @@
package log
import "context"
type Lvl int
const (
@@ -16,9 +18,18 @@ type Logger interface {
Log(keyvals ...interface{}) error
// Log a message at the given level with context key/value pairs
// Debug logs a message with debug level and key/value pairs, if any.
Debug(msg string, ctx ...interface{})
// Info logs a message with info level and key/value pairs, if any.
Info(msg string, ctx ...interface{})
// Warn logs a message with warning level and key/value pairs, if any.
Warn(msg string, ctx ...interface{})
// Error logs a message with error level and key/value pairs, if any.
Error(msg string, ctx ...interface{})
// FromContext returns a new contextual Logger that has this logger's context plus the given context.
FromContext(ctx context.Context) Logger
}