-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
82 lines (67 loc) · 1.57 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
package vabastegi
import (
"fmt"
"io"
)
const (
debugLevel = "Debug"
infoLevel = "Info"
errorLevel = "Error"
warnLevel = "Warn"
)
type logLevel int
// String version of LogLevel.
func (l logLevel) String() string {
switch l {
case DebugLogLevel:
return debugLevel
case InfoLogLevel:
return infoLevel
case ErrorLogLevel:
return errorLevel
case WarnLogLevel:
return warnLevel
}
return "Unknown"
}
const (
DebugLogLevel logLevel = iota - 1
InfoLogLevel
WarnLogLevel
ErrorLogLevel
)
var _ EventHandler = &EventLogger{}
// EventLogger is use event of vabastegi and log them.
type EventLogger struct {
writer io.Writer
level logLevel
}
// NewEventLogger create new instance of EventLogger.
func NewEventLogger(writer io.Writer, level logLevel) *EventLogger {
return &EventLogger{writer: writer, level: level}
}
func (l *EventLogger) IsEnable(level logLevel) bool {
return level >= l.level
}
func (l *EventLogger) log(level logLevel, message string, args ...interface{}) {
if !l.IsEnable(level) {
return
}
fmt.Fprintf(l.writer, "[Vabastegi] ["+level.String()+"] "+message+"\n", args...)
}
func (l *EventLogger) OnEvent(event Event) {
switch e := event.(type) {
case *OnBuildExecuted:
if e.Err != nil {
l.log(ErrorLogLevel, e.ProviderName+" ✕")
return
}
l.log(InfoLogLevel, e.ProviderName+" ✓")
case *OnShutdownExecuting:
l.log(InfoLogLevel, "Shutting Down %s", e.ProviderName)
case *OnApplicationShutdownExecuting:
l.log(InfoLogLevel, "Shutting Down Application: %s", e.Reason)
case *OnLog:
l.log(e.Level, e.Message, e.Args...)
}
}