-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
86 lines (74 loc) · 1.83 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
package logger
import (
"encoding/json"
"fmt"
"github.com/sirupsen/logrus"
"os"
)
// FieldLogger interface
type FieldLogger interface {
Logger
WithField(string, interface{}) FieldLogger
WithFields(map[string]interface{}) FieldLogger
}
type Logger interface {
Debugf(string, ...interface{})
Infof(string, ...interface{})
Printf(string, ...interface{})
Warnf(string, ...interface{})
Errorf(string, ...interface{})
Fatalf(string, ...interface{})
Debug(...interface{})
Info(...interface{})
Warn(...interface{})
Error(...interface{})
Fatal(...interface{})
Panic(...interface{})
}
func ParseLevel(level string) (Level, error) {
l, err := logrus.ParseLevel(level)
return Level(l), err
}
// NewLogger based on the specified log level, defaults to "debug".
// See `New` for more details.
func NewLogger(level string) FieldLogger {
lvl, err := logrus.ParseLevel(level)
if err != nil {
lvl = logrus.DebugLevel
}
return New(lvl)
}
// New based on the specified log level, defaults to "debug".
// This logger will log to the STDOUT in a human readable,
// but parseable form.
/*
Example: time="2016-12-01T21:02:07-05:00" level=info duration=225.283µs human_size="106 B" method=GET path="/" render=199.79µs request_id=2265736089 size=106 status=200
*/
func New(lvl Level) FieldLogger {
e := os.Getenv("GO_ENV")
if len(e) == 0 {
e = "development"
}else if lvl == InfoLevel {
e = "production"
}
dev := e == "development"
l := logrus.New()
l.SetOutput(os.Stdout)
l.Level = lvl
l.Formatter = &textFormatter{
ForceColors: dev,
}
return Logrus{l}
}
func (l *StructuredLog) OnDebug(logging func()){
if l.level == DebugLevel {
logging()
}
}
func JSONMarshal(structValue interface{}) string {
s, _ := json.Marshal(structValue)
return string(s)
}
func Struct(structValue interface{}) string{
return fmt.Sprintf("%+v", structValue)
}