-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.go
67 lines (55 loc) · 1.8 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
package radium
import (
"runtime"
"strings"
"github.com/sirupsen/logrus"
)
// Logger implementation should provide logging
// functionality to the radium instance. Log levels
// should be managed externally.
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warnf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
}
// defaultLogger implements Logger using log package
type defaultLogger struct {
logger *logrus.Logger
}
func (dl defaultLogger) Debugf(format string, args ...interface{}) {
dl.logger.WithFields(logrus.Fields(getSourceInfoFields())).Debugf(format, args...)
}
func (dl defaultLogger) Infof(format string, args ...interface{}) {
dl.logger.WithFields(logrus.Fields(getSourceInfoFields())).Infof(format, args...)
}
func (dl defaultLogger) Warnf(format string, args ...interface{}) {
dl.logger.WithFields(logrus.Fields(getSourceInfoFields())).Warnf(format, args...)
}
func (dl defaultLogger) Errorf(format string, args ...interface{}) {
dl.logger.WithFields(logrus.Fields(getSourceInfoFields())).Errorf(format, args...)
}
func (dl defaultLogger) Fatalf(format string, args ...interface{}) {
dl.logger.WithFields(logrus.Fields(getSourceInfoFields())).Fatalf(format, args...)
}
func getSourceInfoFields() map[string]interface{} {
file, line := getFileInfo(3)
m := map[string]interface{}{
"file": file,
"line": line,
}
return m
}
func getFileInfo(subtractStackLevels int) (string, int) {
_, file, line, _ := runtime.Caller(subtractStackLevels)
return chopPath(file), line
}
// return the source filename after the last slash
func chopPath(original string) string {
i := strings.LastIndex(original, "/")
if i != -1 {
return original[i+1:]
}
return original
}