-
Notifications
You must be signed in to change notification settings - Fork 6
/
logger.go
100 lines (81 loc) · 1.78 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
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
package logger
import (
"os"
"github.com/sirupsen/logrus"
)
// Fatalf ...
func Fatalf(s string, i ...interface{}) {
logrus.Fatalf(s, i...)
}
// Fatal ...
func Fatal(s string) {
logrus.Fatal(s)
}
// Errorf ...
func Errorf(s string, i ...interface{}) {
logrus.Errorf(s, i...)
}
// Error ...
func Error(s string) {
logrus.Errorf(s)
}
// Infof ...
func Infof(s string, i ...interface{}) {
logrus.Infof(s, i...)
}
// Info ...
func Info(s string) {
logrus.Infof(s)
}
// Printf ...
func Printf(s string, i ...interface{}) {
logrus.Infof(s, i...)
}
// Print ...
func Print(s string) {
logrus.Infof(s)
}
// Println ...
func Println(i ...interface{}) {
logrus.Infoln(i...)
}
// Warnf ...
func Warnf(s string, i ...interface{}) {
logrus.Warnf(s, i...)
}
// Warn ...
func Warn(s string) {
logrus.Warnf(s)
}
// Set modifies the logger
func Set(hook ContextHook, fileLoc string, setReport bool) error {
logrus.SetReportCaller(setReport)
logrus.AddHook(hook)
if fileLoc != "" {
// note: need to pass back some way to file.Close()?
file, err := os.OpenFile(fileLoc, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
logrus.Errorf("err opening file %s:\n%v", fileLoc, err)
return err
}
// note: writing to this from multiple writers is threadsafe?
// https://stackoverflow.com/a/29981337/3512709
logrus.SetOutput(file)
}
return nil
}
// SetLoggerLoc sets the file location of the logger
func SetLoggerLoc(loc string) error {
if loc != "" {
// note: need to pass back some way to file.Close()?
file, err := os.OpenFile(loc, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
logrus.Errorf("err opening file %s:\n%v", loc, err)
return err
}
// note: threadsafe?
// https://stackoverflow.com/a/29981337/3512709
logrus.SetOutput(file)
}
return nil
}