-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
73 lines (67 loc) · 1.95 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package logger
import (
"fmt"
"os"
"runtime"
"strings"
"github.com/pkg/errors"
"github.com/rifflock/lfshook"
"github.com/sirupsen/logrus"
)
// Create a logger object with a set level of INFO, reporter toggle, and customized textformatter.
func CreateStandardLogger() *logrus.Logger {
logger := logrus.New()
logger.SetReportCaller(true)
logger = formatter(logger)
return logger
}
// Create a error logger object with a set level of ERROR, reporter toggle, customized textformatter, and a path to a log file.
func CreateErrorLogger(logFilePath string) (*logrus.Logger, error) {
// Setup error logger
_, err := os.OpenFile(logFilePath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return &logrus.Logger{}, errors.Wrap(err, "unable to open file")
}
errorLog := logrus.New()
// Set custom formatting
formatter(errorLog)
// Allow the logger to write ErrorLevel to an log file
pathMap := lfshook.PathMap{
logrus.ErrorLevel: logFilePath,
}
errorLog.SetReportCaller(true)
// Set a hook to format the content going into the log file
errorLog.Hooks.Add(lfshook.NewHook(pathMap, &logrus.TextFormatter{
TimestampFormat: "15:04:05",
ForceColors: false,
FullTimestamp: true,
DisableLevelTruncation: true,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
return formatStdOut(f.Function), fmt.Sprintf("%s:%d", formatStdOut(f.File), f.Line)
},
}))
errorLog.SetLevel(logrus.ErrorLevel)
return errorLog, nil
}
// Parses string to logrus level object
// Default log level is logrus.DebugLevel
func LogLevelFromString(logLevelString string) logrus.Level {
ll := logrus.InfoLevel
switch strings.ToLower(logLevelString) {
case "trace":
ll = logrus.TraceLevel
case "debug":
ll = logrus.DebugLevel
case "info":
ll = logrus.InfoLevel
case "warning":
ll = logrus.WarnLevel
case "error":
ll = logrus.ErrorLevel
case "fatal":
ll = logrus.FatalLevel
default:
ll = logrus.DebugLevel
}
return ll
}