-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
133 lines (117 loc) · 3.18 KB
/
log.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
// Package log is simple extended version of standard 'log' package
// based on logLevel
package log
import (
"io"
"io/ioutil"
"log"
"os"
)
// logLevel is a severity level at which logger works
type logLevel int
// InfoLevel const value 0
// WarningLevel const value 1
// ErrorLevel const value 2
// FatalLevel const value 3
// PanicLevel const value 4
const (
InfoLevel logLevel = iota
WarningLevel
ErrorLevel
FatalLevel
PanicLevel
)
var (
infoLogger *log.Logger
warningLogger *log.Logger
errorLogger *log.Logger
fatalLogger *log.Logger
panicLogger *log.Logger
)
// Levels map ...
var Levels = map[string]logLevel{
"INFO": InfoLevel,
"WARNING": WarningLevel,
"ERROR": ErrorLevel,
"FATAL": FatalLevel,
"PANIC": PanicLevel,
}
func init() {
Init(InfoLevel, nil)
}
// Init initiates the logger
// multiHandler supports papertrail, logdna, loggly
func Init(lev logLevel, multiHandler io.Writer) {
infoHandler := ioutil.Discard
warningHandler := ioutil.Discard
errorHandler := ioutil.Discard
fatalHandler := ioutil.Discard
panicHandler := ioutil.Discard
switch lev {
case InfoLevel:
infoHandler = os.Stdout
warningHandler = os.Stdout
errorHandler = os.Stderr
fatalHandler = os.Stderr
panicHandler = os.Stdout
case WarningLevel:
warningHandler = os.Stdout
errorHandler = os.Stderr
fatalHandler = os.Stderr
panicHandler = os.Stdout
case ErrorLevel:
errorHandler = os.Stderr
fatalHandler = os.Stderr
panicHandler = os.Stdout
case FatalLevel:
fatalHandler = os.Stderr
panicHandler = os.Stdout
case PanicLevel:
panicHandler = os.Stdout
default:
log.Fatal("log: Invalid log level should be (0-4)")
}
if multiHandler != nil {
if infoHandler == os.Stdout {
infoHandler = io.MultiWriter(infoHandler, multiHandler)
}
if warningHandler == os.Stdout {
warningHandler = io.MultiWriter(warningHandler, multiHandler)
}
if errorHandler == os.Stderr {
errorHandler = io.MultiWriter(errorHandler, multiHandler)
}
if fatalHandler == os.Stderr {
fatalHandler = io.MultiWriter(fatalHandler, multiHandler)
}
if panicHandler == os.Stderr {
panicHandler = io.MultiWriter(panicHandler, multiHandler)
}
}
infoLogger = log.New(infoHandler, "INFO: ", log.Ldate|log.Ltime|log.Lshortfile)
warningLogger = log.New(warningHandler, "WARNING: ", log.Ldate|log.Ltime|log.Lshortfile)
errorLogger = log.New(errorHandler, "ERROR: ", log.Ldate|log.Ltime|log.Lshortfile)
fatalLogger = log.New(fatalHandler, "FATAL: ", log.Ldate|log.Ltime|log.Lshortfile)
panicLogger = log.New(panicHandler, "PANIC: ", log.Ldate|log.Ltime|log.Lshortfile)
}
// Wrapper for changing logger's output writer anytime
// InfoSetOutput updates the info output writer
func InfoSetOutput(w io.Writer) {
infoLogger.SetOutput(w)
}
// WarningSetOutput updates the warning output writer
func WarningSetOutput(w io.Writer) {
warningLogger.SetOutput(w)
}
// ErrorSetOutput updates the error output writer
func ErrorSetOutput(w io.Writer) {
errorLogger.SetOutput(w)
}
// FatalSetOutput updates the fatal output writer
func FatalSetOutput(w io.Writer) {
fatalLogger.SetOutput(w)
}
// PanicSetOutput updates the Panic output writer
func PanicSetOutput(w io.Writer) {
panicLogger.SetOutput(w)
}