forked from temporalio/omes
/
logging.go
58 lines (50 loc) · 1.61 KB
/
logging.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
package cmdoptions
import (
"log"
"os"
"github.com/spf13/pflag"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// LoggingOptions for setting up the logger component
type LoggingOptions struct {
// Log level
LogLevel string
// Log encoding (console json)
LogEncoding string
}
// BackupLogger is used in case we can't instantiate zap (it's nicer DX than panicking or using built-in `log`).
var BackupLogger = log.New(os.Stderr, "", 0)
// MustCreateLogger sets up a zap logger or panics on error.
func (l *LoggingOptions) MustCreateLogger() *zap.SugaredLogger {
level, err := zap.ParseAtomicLevel(l.LogLevel)
if err != nil {
BackupLogger.Fatalf("Invalid log level: %v", err)
}
logger, err := zap.Config{
Level: level,
Encoding: l.LogEncoding,
EncoderConfig: zap.NewDevelopmentEncoderConfig(),
OutputPaths: []string{"stderr"},
ErrorOutputPaths: []string{"stderr"},
}.Build(zap.AddStacktrace(zapcore.FatalLevel))
if err != nil {
BackupLogger.Fatalf("Failed to initialize logger: %v", err)
}
return logger.Sugar()
}
// AddCLIFlags adds the relevant flags to populate the options struct.
func (l *LoggingOptions) AddCLIFlags(fs *pflag.FlagSet) {
fs.StringVar(&l.LogLevel, "log-level", "info", "(debug info warn error panic fatal)")
fs.StringVar(&l.LogEncoding, "log-encoding", "console", "(console json)")
}
// ToFlags converts these options to string flags.
func (l *LoggingOptions) ToFlags() (flags []string) {
if l.LogLevel != "" {
flags = append(flags, "--log-level", l.LogLevel)
}
if l.LogEncoding != "" {
flags = append(flags, "--log-encoding", l.LogEncoding)
}
return
}