-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
69 lines (56 loc) · 1.34 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
package dyndns
import (
"fmt"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func newLogEncoderConfig() zapcore.EncoderConfig {
cfg := zap.NewProductionEncoderConfig()
cfg.EncodeTime = zapcore.ISO8601TimeEncoder
cfg.TimeKey = "time"
return cfg
}
func newLogger(logAtom zap.AtomicLevel) *zap.Logger {
logEncoderConfig := newLogEncoderConfig()
logger := zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(logEncoderConfig),
zapcore.Lock(os.Stdout),
logAtom,
))
return logger
}
func (s *Server) initLogger() {
if s.log != nil {
return
}
logAtom := zap.NewAtomicLevel()
s.log = newLogger(logAtom)
// TODO: what to do with the below?
// defer s.log.Sync()
}
// GetLogger returns the instance of logger for a server.
func (s *Server) GetLogger() *zap.Logger {
return s.log
}
// SetLogLevel sets the server logging level.
func (s *Server) SetLogLevel(logLevel string) error {
logAtom := zap.NewAtomicLevel()
switch logLevel {
case "info":
logAtom.SetLevel(zapcore.InfoLevel)
case "warn":
logAtom.SetLevel(zapcore.WarnLevel)
case "debug":
logAtom.SetLevel(zapcore.DebugLevel)
case "error":
logAtom.SetLevel(zapcore.ErrorLevel)
case "fatal":
logAtom.SetLevel(zapcore.FatalLevel)
default:
return fmt.Errorf("unsupported log level %s", logLevel)
}
s.log = newLogger(logAtom)
s.cfg.LogLevel = logLevel
return nil
}