forked from hidevopsio/hiboot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logging.go
243 lines (210 loc) · 6.62 KB
/
logging.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
// Copyright 2018 John Deng (hi.devops.io@gmail.com).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"github.com/kataras/golog"
"github.com/kataras/pio"
"io"
)
//
// Available level names are:
// "disable"
// "fatal"
// "error"
// "warn"
// "info"
// "debug"
//
const (
DebugLevel = "debug"
InfoLevel = "info"
WarnLevel = "warn"
ErrorLevel = "error"
FatalLevel = "fatal"
Disable = "disable"
)
// NewLine can override the default package-level line breaker, "\n".
// It should be called (in-sync) before the print or leveled functions.
//
// See `github.com/kataras/pio#NewLine` and `Logger#NewLine` too.
func NewLine(newLineChar string) {
golog.NewLine(newLineChar)
}
// Reset re-sets the default logger to an empty one.
func Reset() {
golog.Reset()
}
// SetOutput overrides the golog.Logger's Printer's output with another `io.Writer`.
func SetOutput(w io.Writer) {
golog.SetOutput(w)
}
// AddOutput adds one or more `io.Writer` to the golog.Logger's Printer.
//
// If one of the "writers" is not a terminal-based (i.e File)
// then colors will be disabled for all outputs.
func AddOutput(writers ...io.Writer) {
golog.AddOutput(writers...)
}
// SetPrefix sets a prefix for the default package-level Logger.
//
// The prefix is the first space-separated
// word that is being presented to the output.
// It's written even before the log level text.
//
// Returns itself.
func SetPrefix(s string) *golog.Logger {
return golog.SetPrefix(s)
}
// SetTimeFormat sets time format for logs,
// if "s" is empty then time representation will be off.
func SetTimeFormat(s string) {
golog.SetTimeFormat(s)
}
// SetLevel accepts a string representation of
// a `Level` and returns a `Level` value based on that "levelName".
//
// Available level names are:
// "disable"
// "fatal"
// "error"
// "warn"
// "info"
// "debug"
//
// Alternatively you can use the exported `golog.Level` field, i.e `golog.Level = golog.ErrorLevel`
func SetLevel(levelName string) {
golog.SetLevel(levelName)
}
// Print prints a log message without levels and colors.
func Print(v ...interface{}) {
golog.Print(v...)
}
// Println prints a log message without levels and colors.
// It adds a new line at the end.
func Println(v ...interface{}) {
golog.Println(v...)
}
// Logf prints a leveled log message to the output.
// This method can be used to use custom log levels if needed.
// It adds a new line in the end.
func Logf(level golog.Level, format string, args ...interface{}) {
golog.Logf(level, format, args...)
}
// Fatal `os.Exit(1)` exit no matter the level of the logger.
// If the logger's level is fatal, error, warn, info or debug
// then it will print the log message too.
func Fatal(v ...interface{}) {
golog.Fatal(v...)
}
// Fatalf will `os.Exit(1)` no matter the level of the logger.
// If the logger's level is fatal, error, warn, info or debug
// then it will print the log message too.
func Fatalf(format string, args ...interface{}) {
golog.Fatalf(format, args...)
}
// Error will print only when logger's Level is error, warn, info or debug.
func Error(v ...interface{}) {
golog.Error(v...)
}
// Errorf will print only when logger's Level is error, warn, info or debug.
func Errorf(format string, args ...interface{}) {
golog.Errorf(format, args...)
}
// Warn will print when logger's Level is warn, info or debug.
func Warn(v ...interface{}) {
golog.Warn(v...)
}
// Warnf will print when logger's Level is warn, info or debug.
func Warnf(format string, args ...interface{}) {
golog.Warnf(format, args...)
}
// Info will print when logger's Level is info or debug.
func Info(v ...interface{}) {
golog.Info(v...)
}
// Infof will print when logger's Level is info or debug.
func Infof(format string, args ...interface{}) {
golog.Infof(format, args...)
}
// Debug will print when logger's Level is debug.
func Debug(v ...interface{}) {
golog.Debug(v...)
}
// Debugf will print when logger's Level is debug.
func Debugf(format string, args ...interface{}) {
golog.Debugf(format, args...)
}
// Install receives an external logger
// and automatically adapts its print functions.
//
// Install adds a golog handler to support third-party integrations,
// it can be used only once per `golog#Logger` instance.
//
// For example, if you want to print using a logrus
// logger you can do the following:
// `golog.Install(logrus.StandardLogger())`
//
// Look `golog#Handle` for more.
func Install(logger golog.ExternalLogger) {
golog.Install(logger)
}
// InstallStd receives a standard logger
// and automatically adapts its print functions.
//
// Install adds a golog handler to support third-party integrations,
// it can be used only once per `golog#Logger` instance.
//
// Example Code:
// import "log"
// myLogger := log.New(os.Stdout, "", 0)
// InstallStd(myLogger)
//
// Look `golog#Handle` for more.
func InstallStd(logger golog.StdLogger) {
golog.InstallStd(logger)
}
// Handle adds a log handler to the default logger.
//
// Handlers can be used to intercept the message between a log value
// and the actual print operation, it's called
// when one of the print functions called.
// If it's return value is true then it means that the specific
// handler handled the log by itself therefore no need to
// proceed with the default behavior of printing the log
// to the specified logger's output.
//
// It stops on the handler which returns true firstly.
// The `Log` value holds the level of the print operation as well.
func Handle(handler golog.Handler) {
golog.Handle(handler)
}
// Hijack adds a hijacker to the low-level logger's Printer.
// If you need to implement such as a low-level hijacker manually,
// then you have to make use of the pio library.
func Hijack(hijacker func(ctx *pio.Ctx)) {
golog.Hijack(hijacker)
}
// Scan scans everything from "r" and prints
// its new contents to the logger's Printer's Output,
// forever or until the returning "cancel" is fired, once.
func Scan(r io.Reader) (cancel func()) {
return golog.Scan(r)
}
// Child (creates if not exists and) returns a new child
// Logger based on the default package-level logger instance.
//
// Can be used to separate logs by category.
func Child(name string) *golog.Logger {
return golog.Child(name)
}