-
Notifications
You must be signed in to change notification settings - Fork 32
/
logger.go
81 lines (65 loc) · 1.65 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
package logger
// Originally from github.com/motemen/ghq/utils
import (
"fmt"
"os"
colorine "github.com/motemen/go-colorine"
)
// Logger is wrapped go-colorine logger for mkr
type Logger struct {
logger *colorine.Logger
}
// New is constructor for new colorine logger
func New() *Logger {
logger := &colorine.Logger{
Prefixes: colorine.Prefixes{
"warning": colorine.Warn,
"error": colorine.Error,
"": colorine.Info,
"info": colorine.Info,
"created": colorine.Info,
"updated": colorine.Info,
"thrown": colorine.Info,
"retired": colorine.Info,
},
}
// Default output
logger.SetOutput(os.Stderr)
return &Logger{logger: logger}
}
// Log outputs `message` with `prefix` by go-colorine
func (l *Logger) Log(prefix, message string) {
l.logger.Log(prefix, message)
}
// Logf outputs `message` with `prefix` by go-colorine
func (l *Logger) Logf(prefix, message string, args ...interface{}) {
msg := fmt.Sprintf(message, args...)
l.logger.Log(prefix, msg)
}
// Error outputs log given non-nil `err`
func (l *Logger) Error(err error) {
l.Log("error", err.Error())
}
var defaultLogger = New()
// Log outputs `message` with `prefix` by go-colorine
func Log(prefix, message string) {
defaultLogger.Log(prefix, message)
}
// Logf outputs `message` with `prefix` by go-colorine
func Logf(prefix, message string, args ...interface{}) {
defaultLogger.Logf(prefix, message, args...)
}
// ErrorIf outputs log if `err` occurs.
func ErrorIf(err error) bool {
if err == nil {
return false
}
defaultLogger.Error(err)
return true
}
// DieIf outputs log and exit(1) if `err` occurs.
func DieIf(err error) {
if ErrorIf(err) {
os.Exit(1)
}
}