-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
96 lines (76 loc) · 2.56 KB
/
logger.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
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package logger
import (
"fmt"
"net/url"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
const (
// newline is used to split logs by line in our custom log sink and route them to the storage
newline = zapcore.DefaultLineEnding
schemeLoggo = "loggo"
outputLoggo = "loggo://loggo"
)
// LogConsumer is supposed to consume log messages. The default usage routes logs to the storage.
type LogConsumer func(t time.Time, message []byte) error
var (
globalSink consumerSink
globalLogger *zap.SugaredLogger
)
// InitGlobal initializes the global logger using the specified consumer. Note that no synchronization
// is done here, which is why this must be done during the "init" step of the program.
func InitGlobal(consumer LogConsumer, outputs ...string) error {
globalSink.consumer = consumer
// rebuild logger with proper output
logger, err := newLogger(append(outputs, outputLoggo)...)
if err != nil {
return fmt.Errorf("creating global logger: %w", err)
}
globalLogger = logger
return nil
}
// Infow logs an informational message to the global logger.
func Infow(msg string, kvs ...any) {
globalLogger.Infow(msg, appendGlobalKVs(kvs)...)
}
// Errorw logs an error message to the global logger.
func Errorw(msg string, kvs ...any) {
globalLogger.Errorw(msg, appendGlobalKVs(kvs)...)
}
// Sync runs the global logger Sync routine. Meant to be run at the end of the app's execution.
func Sync() error {
return globalLogger.Sync()
}
func newLogger(outputs ...string) (*zap.SugaredLogger, error) {
cfg := zap.NewProductionConfig()
cfg.DisableCaller = true
cfg.DisableStacktrace = true
cfg.EncoderConfig.EncodeDuration = zapcore.StringDurationEncoder
cfg.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.EncoderConfig.LineEnding = newline
cfg.OutputPaths = outputs
logger, err := cfg.Build()
if err != nil {
return nil, fmt.Errorf("building global logger: %w", err)
}
return logger.Sugar(), nil
}
func init() {
globalSink = consumerSink{lineEnding: []byte(newline)}
if err := zap.RegisterSink(schemeLoggo, func(u *url.URL) (zap.Sink, error) {
return &globalSink, nil
}); err != nil {
panic(fmt.Sprintf("registering custom log sink: %s", err))
}
// Since the consumer won't be set at this point, this is basically a noop logger.
logger, err := newLogger(outputLoggo)
if err != nil {
panic(fmt.Sprintf("initializing default global logger: %s", err))
}
globalLogger = logger
}
// :TODO: redo using global logger package functions WithKVs, WithComponent, etc...
func appendGlobalKVs(kvs []any) []any {
return append([]any{"service", "loggo"}, kvs...)
}