forked from godror/godror
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
41 lines (34 loc) · 928 Bytes
/
log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2017, 2021 The Godror Authors
//
//
// SPDX-License-Identifier: UPL-1.0 OR Apache-2.0
package godror
import (
"context"
"sync/atomic"
"github.com/godror/godror/slog"
)
var globalLogger atomic.Value
// SetLogger sets the global logger.
func SetLogger(logger *slog.Logger) { globalLogger.Store(logger) }
type logCtxKey struct{}
// ContextWithLogger returns a context with the given logger.
func ContextWithLogger(ctx context.Context, logger *slog.Logger) context.Context {
return context.WithValue(ctx, logCtxKey{}, logger)
}
func getLogger(ctx context.Context) *slog.Logger {
if ctx != nil {
if lgr, ok := ctx.Value(logCtxKey{}).(*slog.Logger); ok {
return lgr
}
if ctxValue := ctx.Value(paramsCtxKey{}); ctxValue != nil {
if cc, ok := ctxValue.(commonAndConnParams); ok {
return cc.Logger
}
}
}
if lgr, ok := globalLogger.Load().(*slog.Logger); ok {
return lgr
}
return nil
}