-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.go
64 lines (51 loc) · 1.27 KB
/
init.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
package log
import (
"io"
"os"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)
const (
TimestampFieldNameDefault = "t"
MessageFieldNameDefault = "msg"
)
// InitLog initializes zerolog for this service
// you need provide stdout for your log
// this function will init default logger with default formatter
// and default hook
func InitLog(out io.Writer, isProd bool) {
if isProd {
SetDefaultProductionLogger(out)
log.Info().Msg("Inited prod version logger")
return
}
SetDefaultDevLogger(out)
log.Info().Msg("Inited dev version logger")
return
}
func SetDefaultProductionLogger(w io.Writer) {
setDefaultLogger(w)
}
func SetDefaultDevLogger(w io.Writer) {
output := zerolog.ConsoleWriter{
Out: w,
TimeFormat: "2006|01|02 15:04:05.000",
NoColor: false,
}
setDefaultLogger(output)
}
func setDefaultLogger(out io.Writer) {
zerolog.TimestampFieldName = TimestampFieldNameDefault
zerolog.MessageFieldName = MessageFieldNameDefault
logger := zerolog.New(out).With().Timestamp().Logger()
log.Logger = logger
lvl, err := zerolog.ParseLevel(os.Getenv("LOGLVL"))
if err != nil || lvl == zerolog.NoLevel {
lvl = zerolog.DebugLevel
}
log.Logger = log.Level(lvl)
//hooks
for i := range defaultHooks {
log.Logger = log.Hook(defaultHooks[i])
}
}