Skip to content

Commit ca8d7d2

Browse files
authored
Merge pull request #6 from smallstep/context-name
Allow to pass the logger name through the context
2 parents d5eb379 + 05fd709 commit ca8d7d2

File tree

4 files changed

+41
-10
lines changed

4 files changed

+41
-10
lines changed

context.go

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@ import (
99

1010
type key int
1111

12-
// traceheaderKey is the context key that should store the request identifier.
13-
const traceheaderKey key = iota
12+
const (
13+
// traceheaderKey is the context key that should store the request identifier.
14+
traceheaderKey key = iota
15+
// nameKey is the context key used to stored the log name.
16+
nameKey
17+
)
1418

1519
// Tracing returns a new middleware that gets the given header and sets it in
1620
// the context so it can be written in the logger. If the header does not exists
@@ -51,8 +55,20 @@ func WithTraceparent(ctx context.Context, tp *tracing.Traceparent) context.Conte
5155
return context.WithValue(ctx, traceheaderKey, tp)
5256
}
5357

54-
// GetTracing returns the tracing id from the context if it exists.
58+
// GetTraceparent returns the tracing id from the context if it exists.
5559
func GetTraceparent(ctx context.Context) (*tracing.Traceparent, bool) {
5660
v, ok := ctx.Value(traceheaderKey).(*tracing.Traceparent)
5761
return v, ok
5862
}
63+
64+
// WithName returns a new context with the given name in the context. This name
65+
// will appear in the log entries that include the returning context.
66+
func WithName(ctx context.Context, name string) context.Context {
67+
return context.WithValue(ctx, nameKey, name)
68+
}
69+
70+
// GetName returns the log name from the context if it exists.
71+
func GetName(ctx context.Context) (string, bool) {
72+
v, ok := ctx.Value(nameKey).(string)
73+
return v, ok
74+
}

examples/httplog.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
)
1111

1212
func main() {
13-
logger, err := logging.New("scim",
13+
logger, err := logging.New("saluter",
1414
logging.WithLogResponses(),
1515
logging.WithLogRequests(),
1616
)

grpclog/server_logger.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,19 @@ func (l *serverLogger) Log(ctx context.Context, fullMethod string, t time.Time,
4949
service = parts[l-1]
5050
}
5151

52-
var requestID, tracingID string
52+
var name, requestID, tracingID string
53+
if s, ok := logging.GetName(ctx); ok {
54+
name = s
55+
} else {
56+
name = l.name
57+
}
5358
if tp, ok := logging.GetTraceparent(ctx); ok {
5459
requestID = tp.TraceID()
5560
tracingID = tp.String()
5661
}
5762

5863
fields := []zap.Field{
59-
zap.String("name", l.name),
64+
zap.String("name", name),
6065
zap.String("system", "grpc"),
6166
zap.String("span.kind", "server"),
6267
zap.String("grpc.package", pkg),
@@ -113,14 +118,19 @@ func (l *serverLogger) LogStream(ctx context.Context, fullMethod string, msg str
113118
service = parts[l-1]
114119
}
115120

116-
var requestID, tracingID string
121+
var name, requestID, tracingID string
122+
if s, ok := logging.GetName(ctx); ok {
123+
name = s
124+
} else {
125+
name = l.name
126+
}
117127
if tp, ok := logging.GetTraceparent(ctx); ok {
118128
requestID = tp.TraceID()
119129
tracingID = tp.String()
120130
}
121131

122132
fields := []zap.Field{
123-
zap.String("name", l.name),
133+
zap.String("name", name),
124134
zap.String("system", "grpc"),
125135
zap.String("span.kind", "server"),
126136
zap.String("grpc.package", pkg),

httplog/handler.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,12 @@ func (l *LoggerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
9191
// writeEntry writes to the Logger writer the request information in the logger.
9292
func (l *LoggerHandler) writeEntry(w ResponseLogger, r *http.Request, t time.Time, d time.Duration) {
9393
ctx := r.Context()
94-
var requestID, tracingID string
94+
var name, requestID, tracingID string
95+
if s, ok := logging.GetName(ctx); ok {
96+
name = s
97+
} else {
98+
name = l.name
99+
}
95100
if tp, ok := logging.GetTraceparent(ctx); ok {
96101
requestID = tp.TraceID()
97102
tracingID = tp.String()
@@ -118,7 +123,7 @@ func (l *LoggerHandler) writeEntry(w ResponseLogger, r *http.Request, t time.Tim
118123
status := w.StatusCode()
119124

120125
fields := []zap.Field{
121-
zap.String("name", l.name),
126+
zap.String("name", name),
122127
zap.String("system", "http"),
123128
zap.String("request-id", requestID),
124129
zap.String("tracing-id", tracingID),

0 commit comments

Comments
 (0)