-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogs.go
75 lines (65 loc) · 1.72 KB
/
logs.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
package pandati
import (
"fmt"
stdlog "log"
"os"
"time"
"github.com/rs/zerolog"
)
type LogConfig struct {
DefaultOutput *os.File
Logger zerolog.Logger
}
func NewLogger() *LogConfig {
log := new(LogConfig)
switch dll := os.Getenv("LOG_LEVEL"); dll {
case "debug":
zerolog.SetGlobalLevel(zerolog.DebugLevel)
case "warn":
zerolog.SetGlobalLevel(zerolog.WarnLevel)
case "error":
zerolog.SetGlobalLevel(zerolog.ErrorLevel)
default:
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
zerolog.TimeFieldFormat = time.RFC3339
zerolog.MessageFieldName = "short_message"
zerolog.TimestampFieldName = "timestamp"
zerolog.LevelFatalValue = "critical"
log.Logger = log.Logger.With().Timestamp().Logger()
stdlog.SetFlags(0)
stdlog.SetOutput(log.Logger)
return log
}
func (l *LogConfig) Critical(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stderr)
log(w.Fatal(), message, v...)
}
func (l *LogConfig) Error(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stderr)
log(w.Error(), message, v...)
}
func (l *LogConfig) Warning(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stdout)
log(w.Warn(), message, v...)
}
func (l *LogConfig) Info(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stdout)
log(w.Info(), message, v...)
}
func (l *LogConfig) Debug(message string, v ...map[string]interface{}) {
w := l.Logger.Output(os.Stdout)
log(w.Debug(), message, v...)
}
func log(contextLog *zerolog.Event, message string, v ...map[string]interface{}) {
if len(v) > 0 {
for _, m := range v {
for k, v := range m {
if !IsZero(v) && !IsZero(k) {
contextLog.Str(k, fmt.Sprintf("%v", v))
}
}
}
}
contextLog.Msg(message)
}