-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogging.go
109 lines (102 loc) · 2.79 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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"os"
"path"
"runtime"
"strings"
)
const (
FieldRoutine = "routine" // flag for routine name
FieldRequestId = "requestId" // unique request ID
FieldMetricsName = "metricsName" // metrics name
FieldIsUp = "isUp" // is server up
FieldSession = "session" // define session ID
FieldMonitorName = "monitorName" // define monitor name field
FieldSessionId = "sessionId" // define name for session ID field
LogRequestDuration = false // define if logg duration for every request
)
func validLogLevel(level string) log.Level {
switch strings.ToUpper(level) {
case "FAT", "F", "FATAL":
return log.FatalLevel
case "ERR", "E", "ERROR":
return log.ErrorLevel
case "WAR", "W", "WARNING":
return log.WarnLevel
case "INF", "I", "INFO":
return log.InfoLevel
case "TRC", "T", "TRACE":
return log.TraceLevel
case "DEB", "D", "DEBUG":
return log.DebugLevel
default:
return log.InfoLevel
}
}
func initLog() {
logToFile := config.Log.LogToFile()
if help {
logToFile = false
}
if config.Log.JSONFormat {
jsonFormatter := new(log.JSONFormatter)
jsonFormatter.TimestampFormat = "2006-01-02 15:04:05.000"
jsonFormatter.CallerPrettyfier = func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
format := " %s:%d"
if logToFile {
format = "%s:%d"
}
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(format, filename, f.Line)
}
log.SetFormatter(jsonFormatter)
} else {
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = "2006-01-02 15:04:05.000"
Formatter.FullTimestamp = true
Formatter.DisableLevelTruncation = false
Formatter.ForceColors = !logToFile
Formatter.CallerPrettyfier = func(f *runtime.Frame) (string, string) {
filename := path.Base(f.File)
format := " %s:%d"
if logToFile {
format = "%s:%d"
}
return fmt.Sprintf("%s()", f.Function), fmt.Sprintf(format, filename, f.Line)
}
log.SetFormatter(Formatter)
}
log.SetReportCaller(config.Log.LogProgramInfo)
log.SetLevel(validLogLevel(config.Log.Level))
if logToFile {
lJack := &lumberjack.Logger{
Filename: config.Log.FileName,
MaxBackups: config.Log.MaxBackups,
MaxAge: config.Log.MaxAge,
MaxSize: config.Log.MaxSize,
Compress: true,
}
if config.Log.Quiet {
log.SetOutput(lJack)
} else {
mWriter := io.MultiWriter(os.Stdout, lJack)
log.SetOutput(mWriter)
}
} else {
if config.Log.Quiet {
log.SetLevel(log.PanicLevel)
}
}
if !help {
log.WithFields(log.Fields{
"ApplicationName": applicationName,
"RuntimeVersion": runtime.Version(),
"CPUs": runtime.NumCPU(),
"Arch": runtime.GOARCH,
}).Info("application Initializing")
}
}