-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.go
More file actions
72 lines (61 loc) · 1.6 KB
/
Copy pathlogger.go
File metadata and controls
72 lines (61 loc) · 1.6 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package ctfdsetup
import (
"context"
"os"
"sync"
"go.opentelemetry.io/contrib/bridges/otelzap"
"go.opentelemetry.io/otel/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type Logger struct {
sub *zap.Logger
}
func (log *Logger) Info(_ context.Context, msg string, fields ...zap.Field) {
log.sub.Info(msg, fields...)
}
func (log *Logger) Debug(_ context.Context, msg string, fields ...zap.Field) {
log.sub.Debug(msg, fields...)
}
func (log *Logger) Error(_ context.Context, msg string, fields ...zap.Field) {
log.sub.Error(msg, fields...)
}
var (
loggerMx sync.Mutex
logger *Logger
)
// Log returns the zap logger, ready to use.
// It exports no log, and is configured at level "info".
func Log() *Logger {
loggerMx.Lock()
defer loggerMx.Unlock()
// If used as a library, defaults to the most basic logger we can get
if logger != nil {
return logger
}
sub, _ := zap.NewProduction()
logger = &Logger{sub: sub}
return logger
}
// UpsertLogger overrides the global logger used by the tool for future operations.
//
// Ideally, it should be called once at the beginning our your tool integration, but
// you can adapt to match your needs (e.g., hot level-reconfiguration).
func UpsertLogger(prov log.LoggerProvider, level string) *Logger {
loggerMx.Lock()
defer loggerMx.Unlock()
lvl, _ := zapcore.ParseLevel(level)
core := zapcore.NewTee(
zapcore.NewCore(
zapcore.NewJSONEncoder(zap.NewProductionEncoderConfig()),
zapcore.AddSync(os.Stdout),
lvl,
),
otelzap.NewCore(
serviceName,
otelzap.WithLoggerProvider(prov),
),
)
logger = &Logger{sub: zap.New(core)}
return logger
}