-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
152 lines (143 loc) · 3.3 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
import (
"fmt"
log "github.com/sirupsen/logrus"
"gopkg.in/natefinch/lumberjack.v2"
"io"
"os"
"runtime"
"sort"
"strings"
)
const (
identificationId = "id"
identificationIdOrder = "00__id__"
)
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 prettyFile(f *runtime.Frame) (string, string) {
skip := 1
var routine string
var file string
var line int
var callFunc string
var ok bool
var pc uintptr
for {
pc, file, line, ok = runtime.Caller(skip)
if skip > 10 {
ok = false
}
if !ok {
file = "---"
routine = "---"
line = 0
callFunc = "---"
break
}
if strings.Contains(file, "logrus") || strings.Contains(runtime.FuncForPC(pc).Name(), "logrus") {
skip++
continue
}
slash := strings.LastIndex(file, "/")
routine = file[slash+1:]
callFunc = runtime.FuncForPC(pc).Name()
slash = strings.LastIndex(callFunc, ".")
callFunc = callFunc[slash+1:]
break
}
format := " %s:%d"
if config.Log.LogToFile() {
format = "%s:%d"
}
return fmt.Sprintf("%s()", callFunc), fmt.Sprintf(format, routine, line)
}
func sortLogFields(i []string) {
if len(i) < 2 {
return
}
idx := -1
for j, s := range i {
if s == identificationId {
idx = j
}
}
if idx > -1 && idx < len(i) {
i[idx] = identificationIdOrder
}
sort.Strings(i)
idx = -1
for j, s := range i {
if s == identificationIdOrder {
idx = j
}
}
if idx > -1 && idx < len(i) {
i[idx] = identificationId
}
}
func initLog() {
if config.Log.JSONFormat {
jsonFormatter := new(log.JSONFormatter)
jsonFormatter.TimestampFormat = DateTimeFormat
jsonFormatter.CallerPrettyfier = prettyFile
log.SetFormatter(jsonFormatter)
} else {
Formatter := new(log.TextFormatter)
Formatter.TimestampFormat = DateTimeFormat
Formatter.FullTimestamp = true
Formatter.DisableLevelTruncation = false
Formatter.ForceColors = !config.Log.LogToFile()
Formatter.SortingFunc = sortLogFields
Formatter.CallerPrettyfier = prettyFile
log.SetFormatter(Formatter)
}
log.SetReportCaller(config.Log.LogProgramInfo)
lvl := validLogLevel(config.Log.Level)
log.SetLevel(lvl)
if config.Log.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)
}
}
log.WithFields(log.Fields{
"ApplicationName": applicationName,
"RuntimeVersion": runtime.Version(),
"CPUs": runtime.NumCPU(),
"Arch": runtime.GOARCH,
}).Info("Application Initializing")
}
func VersionDetail() string {
return fmt.Sprintf("Version details\r\n\tApplication Name: %s\r\n\tRuntime Version: %s\r\n\tCPUs: %d\r\n\tArchitectire: %s",
applicationName, runtime.Version(), runtime.NumCPU(), runtime.GOARCH)
}