generated from skeptycal/gorepotemplate
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.doc
221 lines (146 loc) · 6.08 KB
/
go.doc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package errorlogger // import "github.com/skeptycal/errorlogger"
Package errorlogger implements error logging to a variety of output formats.
The goal of this package is to provide a simple and efficient mechanism for
managing, testing, debugging, and changing options for error logging
throughout a program.
It is a drop-in replacement for the standard library log package and the
popular Logrus package.
Code like this works as expected without any changes:
log.Errorf("this is an error: %v", err)
log.Fatal("no input file provided.")
Usage
A global logger with a default logging function is supplied:
var log = errorlogger.Log
log.Error("sample log error message")
using the variable 'log' matches most code using the standard library 'log'
package or Logrus package.
Logging
The default global error logging function is supplied.
var Err = errorlogger.Err
Err wraps errors, adds custom messages, formats errors, and outputs messages
to the correct io.Writer as specified in the options.
Calling this function will perform all package-level logging and error
wrapping. It will then return the error otherwise unchanged and ready to
propagate up.
If you do not intend to use any options or disable the logger, it may be
more convenient to use only the function alias to call the most common
method, Err(), like this:
var Err = errorlogger.Err
then, just call the function within error blocks:
err := someProcess(stuff)
if err != nil {
return Err(err)
}
or
return Err(someProcess(stuff))
or even this
_ = Err(someProcess(stuff)) // log errors only and continue
if the error does not need to be propagated (bubbled) up. (This is not
generally recommended.)
Examples
file open
f, err := os.Open("myfile")
if err != nil {
return Err(err)
}
get environment variable
env := os.Getenv("PATH")
if env == "" {
return "", Err(os.ErrNotExist)
}
return env, nil
check return value while returning an error
return Err(os.Chmod("myfile", 420))
Defaults
The global defaults may be aliased if there is a concern about name
collisions:
var LogThatWontConflict = errorlogger.Log
var ErrThatWontConflict = errorlogger.Err
By default, logging is enabled and ANSI colorized text is sent to stderr of
the TTY. If it is changed and you wish to return to the default text
formatter, use
log.SetText()
Logging can also be redirected to a file or any io.Writer
log.SetLogOutput(w io.Writer)
To create a new logger with default behaviors, use:
var log = errorlogger.New()
and start logging!
(The defaults are output to os.Stderr, ANSI color, include timestamps,
logging enabled, default log level(INFO), no error wrapping, default log
function, and use default Logrus logger as pass-through.)
Customize
If you want to customize the logger, use:
NewWithOptions(enabled bool, fn LoggerFunc, wrap error, logger interface{}) ErrorLogger
Some additional features of this package include:
- easy configuration of JSON logging:
log.EnableJSON(true) // true for pretty printing
- return to the default text formatting
log.SetText() // change to default text formatter
- easy configuration of custom output formatting:
log.SetFormatter(myJSONformatter) // set a custom formatter
- easy configuration of numerous third party formatters.
- Set log level - the verbosity of the logging may be adjusted. Allowed
values are Panic, Fatal, Error, Warn, Info, Debug, Trace. The default is
"Info"
log.SetLogLevel("INFO") // Set log level - uppercase string ...
log.SetLogLevel("error") // ... or lowercase string accepted
Performance
Error logging may be disabled during performance critical operations:
log.Disable() // temporarily disable logging
defer log.Enable() // enable logging after critical code
In this case, the error function is replaced with a noop function. This
removed any enabled/disabled check and usually results in a performance gain
when compared to checking a flag during every possible operation that may
request logging.
Logging is deferred or reenabled with
log.Enable() // after performance sensitive portion, enable logging
This may be done at any time and as often as desired.
- SetLoggerFunc allows setting of a custom logger function. The default is
log.Error(), which is compatible with the standard library log package and
logrus.
log.SetLoggerFunc(fn LoggerFunc)
- SetErrorWrap allows ErrorLogger to wrap errors in a specified custom type
for use with errors.Is():
log.SetErrorWrap(wrap error)
For example, if you want all errors returned to be considered type
*os.PathError, use:
log.SetErrorWrap(&os.PathError{})
To wrap all errors in a custom type, use:
log.SetErrorWrap(myErrType{}) // wrap all errors in a custom type
const DefaultLogLevel Level = InfoLevel ...
var Log = New() ...
var ErrInvalid = errors.New("invalid argument") ...
var AllLevels []Level = logrus.AllLevels
var EOF = errors.New("EOF")
func Example()
func NewSyscallError(syscall string, err error) error
func NopCloser(r io.Reader) io.ReadCloser
func ReadAll(r Reader) ([]byte, error)
type Closer interface{ ... }
type Entry = logrus.Entry
type ErrorFunc = func(err error) error
type ErrorLogger interface{ ... }
func New() ErrorLogger
func NewWithOptions(enabled bool, msg string, fn LoggerFunc, wrap error, logger *Logger) ErrorLogger
type Fields = logrus.Fields
type Formatter interface{ ... }
var DefaultTextFormatter Formatter = NewTextFormatter()
type JSONFormatter struct{ ... }
func NewJSONFormatter(pretty bool) *JSONFormatter
type Level = logrus.Level
const PanicLevel Level = iota ...
type Logger = logrus.Logger
type LoggerFunc = func(args ...interface{})
type PathError struct{ ... }
type ReadCloser interface{ ... }
type ReadWriteCloser interface{ ... }
type ReadWriter interface{ ... }
type Reader interface{ ... }
type ReaderFrom interface{ ... }
type SyscallError struct{ ... }
type TextFormatter struct{ ... }
func NewTextFormatter() *TextFormatter
type WriteCloser interface{ ... }
type Writer interface{ ... }
var Discard Writer = discard{}
type WriterTo interface{ ... }