-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenlog.go
83 lines (73 loc) · 2.09 KB
/
enlog.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
package enlog
// Colors used to color the terminal output
const (
ColorRed uint8 = 31
ColorGreen uint8 = 32
ColorYellow uint8 = 33
ColorBlue uint8 = 34
ColorMagenta uint8 = 35
ColorCyan uint8 = 36
ColorWhite uint8 = 37
)
// An Enlogger contains all methods that
// may be called to log messages.
type Enlogger interface {
Info(string)
Infof(log string, args ...interface{})
Debug(string)
Debugf(log string, args ...interface{})
Error(string)
Errorf(log string, args ...interface{})
Trace(string)
Tracef(log string, args ...interface{})
SetAfterLogEvent(AfterLogEvent)
}
// AfterLogEvent is a method called after
// every log event. It is right place to
// add custom behaviour after logging, for
// instance to send an email after error.
type AfterLogEvent func(logType string, message string)
// Enlog struct contains all logger types
// provided by this package.
type Enlog struct {
InfoLog *logger
DebugLog *logger
ErrorLog *logger
TraceLog *logger
AfterLog AfterLogEvent
}
func newLog(prefix string, useFile bool, filePath string, color uint8) *logger {
l := &logger{
handler: nil,
prefix: prefix,
useFile: useFile,
filePath: filePath,
color: color,
}
l.handler = l.NewHandler()
return l
}
// New creates an Enlog struct. The struct contains
// fields with all log types provided by this package:
// InfoLog, DebugLog, ErrorLog, TraceLog
// Read more: https://malekim.github.io/enlog
func New() *Enlog {
l := &Enlog{}
l.InfoLog = newLog("INFO", false, "info.log", ColorGreen)
l.DebugLog = newLog("DEBUG", false, "debug.log", ColorCyan)
l.ErrorLog = newLog("ERROR", false, "error.log", ColorRed)
l.TraceLog = newLog("TRACE", false, "trace.log", ColorMagenta)
// default event
l.SetAfterLogEvent(func(logType string, message string) {
return
})
return l
}
// SetAfterLogEvent is a method to set AfterLogEvent.
// AfterLogEvent is a method called after
// every log event. It is right place to
// add custom behaviour after logging, for
// instance to send an email after error.
func (l *Enlog) SetAfterLogEvent(afterLog AfterLogEvent) {
l.AfterLog = afterLog
}