-
Notifications
You must be signed in to change notification settings - Fork 31
/
errors.go
145 lines (126 loc) · 3.05 KB
/
errors.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
package webgo
import (
"errors"
"io"
"log"
"os"
)
var (
// ErrInvalidPort is the error returned when the port number provided in the config file is invalid
ErrInvalidPort = errors.New("port number not provided or is invalid (should be between 0 - 65535)")
lh *logHandler
)
type logCfg string
const (
// LogCfgDisableDebug is used to disable debug logs
LogCfgDisableDebug = logCfg("disable-debug")
// LogCfgDisableInfo is used to disable info logs
LogCfgDisableInfo = logCfg("disable-info")
// LogCfgDisableWarn is used to disable warning logs
LogCfgDisableWarn = logCfg("disable-warn")
// LogCfgDisableError is used to disable error logs
LogCfgDisableError = logCfg("disable-err")
// LogCfgDisableFatal is used to disable fatal logs
LogCfgDisableFatal = logCfg("disable-fatal")
)
// Logger defines all the logging methods to be implemented
type Logger interface {
Debug(data ...interface{})
Info(data ...interface{})
Warn(data ...interface{})
Error(data ...interface{})
Fatal(data ...interface{})
}
// logHandler has all the log writer handlers
type logHandler struct {
debug *log.Logger
info *log.Logger
warn *log.Logger
err *log.Logger
fatal *log.Logger
}
// Debug prints log of severity 5
func (lh *logHandler) Debug(data ...interface{}) {
if lh.debug == nil {
return
}
lh.debug.Println(data...)
}
// Info prints logs of severity 4
func (lh *logHandler) Info(data ...interface{}) {
if lh.info == nil {
return
}
lh.info.Println(data...)
}
// Warn prints log of severity 3
func (lh *logHandler) Warn(data ...interface{}) {
if lh.warn == nil {
return
}
lh.warn.Println(data...)
}
// Error prints log of severity 2
func (lh *logHandler) Error(data ...interface{}) {
if lh.err == nil {
return
}
lh.err.Println(data...)
}
// Fatal prints log of severity 1
func (lh *logHandler) Fatal(data ...interface{}) {
if lh.fatal == nil {
return
}
lh.fatal.Fatalln(data...)
}
// LOGHANDLER is a global variable which webgo uses to log messages
var LOGHANDLER Logger
func init() {
GlobalLoggerConfig(nil, nil)
}
func loggerWithCfg(stdout io.Writer, stderr io.Writer, cfgs ...logCfg) *logHandler {
lh = &logHandler{
debug: log.New(stdout, "Debug ", log.LstdFlags),
info: log.New(stdout, "Info ", log.LstdFlags),
warn: log.New(stderr, "Warning ", log.LstdFlags),
err: log.New(stderr, "Error ", log.LstdFlags),
fatal: log.New(stderr, "Fatal ", log.LstdFlags|log.Llongfile),
}
for _, c := range cfgs {
switch c {
case LogCfgDisableDebug:
{
lh.debug = nil
}
case LogCfgDisableInfo:
{
lh.info = nil
}
case LogCfgDisableWarn:
{
lh.warn = nil
}
case LogCfgDisableError:
{
lh.err = nil
}
case LogCfgDisableFatal:
{
lh.fatal = nil
}
}
}
return lh
}
// GlobalLoggerConfig is used to configure the global/default logger of webgo
// IMPORTANT: This is not concurrent safe
func GlobalLoggerConfig(stdout io.Writer, stderr io.Writer, cfgs ...logCfg) {
if stdout == nil {
stdout = os.Stdout
}
if stderr == nil {
stderr = os.Stderr
}
LOGHANDLER = loggerWithCfg(stdout, stderr, cfgs...)
}