-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlogger.go
53 lines (46 loc) · 1.21 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
package main
import (
log "github.com/sirupsen/logrus"
"os"
"path"
"runtime"
"strconv"
)
func initLogger(logLevel string, logFile string) {
var formatter log.Formatter
level, _ := log.ParseLevel(logLevel)
log.SetLevel(level)
if logFile != "" {
//f, err := os.Create(logFile)
f, err := os.OpenFile(logFile, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0640)
if err != nil {
panic("Failed to create log file: " + logFile)
}
log.SetOutput(f)
formatter = &log.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05.000",
}
if log.GetLevel() == log.TraceLevel {
log.SetReportCaller(true)
formatter.(*log.JSONFormatter).CallerPrettyfier = LogPrettyTrace
}
} else {
formatter = &log.TextFormatter{
FullTimestamp: true,
TimestampFormat: "2006-01-02 15:04:05.000",
PadLevelText: true,
QuoteEmptyFields: true,
}
if log.GetLevel() == log.TraceLevel {
log.SetReportCaller(true)
formatter.(*log.TextFormatter).CallerPrettyfier = LogPrettyTrace
}
}
log.SetFormatter(formatter)
}
func LogPrettyTrace(f *runtime.Frame) (function string, file string) {
//function = path.Base(f.Function)
function = ""
file = " " + path.Base(f.File) + ":" + strconv.Itoa(f.Line)
return function, file
}