forked from cloudfoundry-attic/bosh-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
151 lines (125 loc) · 3.17 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package logger
import (
"fmt"
"io"
"log"
"os"
"runtime/debug"
"strings"
)
type LogLevel int
const (
LevelDebug LogLevel = iota
LevelInfo
LevelWarn
LevelError
LevelNone LogLevel = 99
)
var levels = map[string]LogLevel{
"DEBUG": LevelDebug,
"INFO": LevelInfo,
"WARN": LevelWarn,
"ERROR": LevelError,
"NONE": LevelNone,
}
var levelKeys = []string{"DEBUG", "INFO", "WARN", "ERROR", "NONE"}
func Levelify(levelString string) (LogLevel, error) {
upperLevelString := strings.ToUpper(levelString)
level, ok := levels[upperLevelString]
if !ok {
expected := strings.Join(levelKeys, ", ")
return level, fmt.Errorf("Unknown LogLevel string '%s', expected one of [%s]", levelString, expected)
}
return level, nil
}
type Logger struct {
level LogLevel
out *log.Logger
err *log.Logger
}
func New(level LogLevel, out, err *log.Logger) Logger {
return Logger{
level: level,
out: out,
err: err,
}
}
func NewLogger(level LogLevel) Logger {
return NewWriterLogger(level, os.Stdout, os.Stderr)
}
func NewWriterLogger(level LogLevel, out, err io.Writer) Logger {
return Logger{
level: level,
out: log.New(out, "", log.LstdFlags),
err: log.New(err, "", log.LstdFlags),
}
}
func (l Logger) Debug(tag, msg string, args ...interface{}) {
if l.level > LevelDebug {
return
}
msg = fmt.Sprintf("DEBUG - %s", msg)
l.getOutLogger(tag).Printf(msg, args...)
}
// DebugWithDetails will automatically change the format of the message
// to insert a block of text after the log
func (l Logger) DebugWithDetails(tag, msg string, args ...interface{}) {
msg = msg + "\n********************\n%s\n********************"
l.Debug(tag, msg, args...)
}
func (l Logger) Info(tag, msg string, args ...interface{}) {
if l.level > LevelInfo {
return
}
msg = fmt.Sprintf("INFO - %s", msg)
l.getOutLogger(tag).Printf(msg, args...)
}
func (l Logger) Warn(tag, msg string, args ...interface{}) {
if l.level > LevelWarn {
return
}
msg = fmt.Sprintf("WARN - %s", msg)
l.getErrLogger(tag).Printf(msg, args...)
}
func (l Logger) Error(tag, msg string, args ...interface{}) {
if l.level > LevelError {
return
}
msg = fmt.Sprintf("ERROR - %s", msg)
l.getErrLogger(tag).Printf(msg, args...)
}
// ErrorWithDetails will automatically change the format of the message
// to insert a block of text after the log
func (l Logger) ErrorWithDetails(tag, msg string, args ...interface{}) {
msg = msg + "\n********************\n%s\n********************"
l.Error(tag, msg, args...)
}
func (l Logger) HandlePanic(tag string) {
panic := recover()
if panic != nil {
var msg string
switch obj := panic.(type) {
case string:
msg = obj
case fmt.Stringer:
msg = obj.String()
case error:
msg = obj.Error()
default:
msg = fmt.Sprintf("%#v", obj)
}
l.ErrorWithDetails(tag, "Panic: %s", msg, debug.Stack())
os.Exit(2)
}
}
func (l Logger) getOutLogger(tag string) (logger *log.Logger) {
return l.updateLogger(l.out, tag)
}
func (l Logger) getErrLogger(tag string) (logger *log.Logger) {
return l.updateLogger(l.err, tag)
}
func (l Logger) updateLogger(logger *log.Logger, tag string) *log.Logger {
prefix := fmt.Sprintf("[%s] ", tag)
logger.SetPrefix(prefix)
return logger
}