forked from taggledevel2/ratchet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
99 lines (85 loc) · 2.47 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
// Package logger is a simple but customizable logger used by ratchet.
package logger
import (
"io"
"log"
"os"
"runtime"
)
const (
LevelDebug = iota
LevelInfo
LevelError
LevelStatus
LevelSilent
)
// RatchetNotifier is an interface for receiving log events. See the
// Notifier variable.
type RatchetNotifier interface {
RatchetNotify(lvl int, trace []byte, v ...interface{})
}
// RatchetNotifier can be set to receive log events in your external
// implementation code. Useful for doing custom alerting, etc.
var Notifier RatchetNotifier
// LogLevel can be set to one of:
// logger.LevelDebug, logger.LevelInfo, logger.LevelError, logger.LevelStatus, or logger.LevelSilent
var LogLevel = LevelInfo
var defaultLogger = log.New(os.Stdout, "", log.LstdFlags)
// Debug logs output when LogLevel is set to at least Debug level
func Debug(v ...interface{}) {
logit(LevelDebug, v)
if Notifier != nil {
Notifier.RatchetNotify(LevelDebug, nil, v)
}
}
// Info logs output when LogLevel is set to at least Info level
func Info(v ...interface{}) {
logit(LevelInfo, v)
if Notifier != nil {
Notifier.RatchetNotify(LevelInfo, nil, v)
}
}
// Error logs output when LogLevel is set to at least Error level
func Error(v ...interface{}) {
logit(LevelError, v)
if Notifier != nil {
trace := make([]byte, 4096)
runtime.Stack(trace, true)
Notifier.RatchetNotify(LevelError, trace, v)
}
}
// ErrorWithoutTrace logs output when LogLevel is set to at least Error level
// but doesn't send the stack trace to Notifier. This is useful only when
// using a RatchetNotifier implementation.
func ErrorWithoutTrace(v ...interface{}) {
logit(LevelError, v)
if Notifier != nil {
Notifier.RatchetNotify(LevelError, nil, v)
}
}
// Status logs output when LogLevel is set to at least Status level
// Status output is high-level status events like stages starting/completing.
func Status(v ...interface{}) {
logit(LevelStatus, v)
if Notifier != nil {
Notifier.RatchetNotify(LevelStatus, nil, v)
}
}
func logit(lvl int, v ...interface{}) {
if lvl >= LogLevel {
defaultLogger.Println(v...)
}
}
// SetLogfile can be used to log to a file as well as Stdoud.
func SetLogfile(filepath string) {
f, err := os.OpenFile(filepath, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
panic(err.Error())
}
out := io.MultiWriter(os.Stdout, f)
SetOutput(out)
}
// SetOutput allows setting log output to any custom io.Writer.
func SetOutput(out io.Writer) {
defaultLogger = log.New(out, "", log.LstdFlags)
}