Skip to content

Commit 01d645f

Browse files
committed
github.com/qiniu/x/log.CanOutput
1 parent ec13252 commit 01d645f

File tree

1 file changed

+41
-5
lines changed

1 file changed

+41
-5
lines changed

log/logext.go

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ const (
2929
) // [prefix][time][level][module][shortfile|longfile]
3030

3131
const (
32+
// Ldebug is a log output level that prints debug information.
3233
Ldebug = iota
34+
// Linfo is a log output level that prints prompting information.
3335
Linfo
36+
// Lwarn is a log output level that prints warning information.
3437
Lwarn
38+
// Lerror is a log output level that prints error information.
3539
Lerror
40+
// Lpanic is a log output level that prints error information.
3641
Lpanic
42+
// Lfatal is a log output level that prints fatal information.
3743
Lfatal
3844
)
3945

@@ -68,6 +74,7 @@ func New(out io.Writer, prefix string, flag int) *Logger {
6874
return &Logger{out: out, prefix: prefix, Level: 1, flag: flag}
6975
}
7076

77+
// Std *Logger.
7178
var Std = New(os.Stderr, "", Ldefault)
7279

7380
// Cheap integer to fixed-width decimal ASCII. Give a negative width to avoid zero-padding.
@@ -107,7 +114,7 @@ func shortFile(file string, flag int) string {
107114
return file
108115
}
109116

110-
func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line int, lvl int, reqId string) {
117+
func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line int, lvl int, reqID string) {
111118
if l.prefix != "" {
112119
buf.WriteString(l.prefix)
113120
}
@@ -135,9 +142,9 @@ func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line
135142
buf.WriteByte(' ')
136143
}
137144
}
138-
if reqId != "" {
145+
if reqID != "" {
139146
buf.WriteByte('[')
140-
buf.WriteString(reqId)
147+
buf.WriteString(reqID)
141148
buf.WriteByte(']')
142149
}
143150
if l.flag&Llevel != 0 {
@@ -161,7 +168,7 @@ func (l *Logger) formatHeader(buf *bytes.Buffer, t time.Time, file string, line
161168
// already a newline. Calldepth is used to recover the PC and is
162169
// provided for generality, although at the moment on all pre-defined
163170
// paths it will be 2.
164-
func (l *Logger) Output(reqId string, lvl int, calldepth int, s string) error {
171+
func (l *Logger) Output(reqID string, lvl int, calldepth int, s string) error {
165172
if lvl < l.Level {
166173
return nil
167174
}
@@ -183,7 +190,7 @@ func (l *Logger) Output(reqId string, lvl int, calldepth int, s string) error {
183190
}
184191
l.levelStats[lvl]++
185192
l.buf.Reset()
186-
l.formatHeader(&l.buf, now, file, line, lvl, reqId)
193+
l.formatHeader(&l.buf, now, file, line, lvl, reqID)
187194
l.buf.WriteString(s)
188195
if len(s) > 0 && s[len(s)-1] != '\n' {
189196
l.buf.WriteByte('\n')
@@ -210,13 +217,15 @@ func (l *Logger) Println(v ...interface{}) { l.Output("", Linfo, 2, fmt.Sprintln
210217

211218
// -----------------------------------------
212219

220+
// Debugf prints a debug information.
213221
func (l *Logger) Debugf(format string, v ...interface{}) {
214222
if Ldebug < l.Level {
215223
return
216224
}
217225
l.Output("", Ldebug, 2, fmt.Sprintf(format, v...))
218226
}
219227

228+
// Debug prints a debug information.
220229
func (l *Logger) Debug(v ...interface{}) {
221230
if Ldebug < l.Level {
222231
return
@@ -226,13 +235,15 @@ func (l *Logger) Debug(v ...interface{}) {
226235

227236
// -----------------------------------------
228237

238+
// Infof prints a prompting information.
229239
func (l *Logger) Infof(format string, v ...interface{}) {
230240
if Linfo < l.Level {
231241
return
232242
}
233243
l.Output("", Linfo, 2, fmt.Sprintf(format, v...))
234244
}
235245

246+
// Info prints a prompting information.
236247
func (l *Logger) Info(v ...interface{}) {
237248
if Linfo < l.Level {
238249
return
@@ -242,22 +253,27 @@ func (l *Logger) Info(v ...interface{}) {
242253

243254
// -----------------------------------------
244255

256+
// Warnf prints a warning information.
245257
func (l *Logger) Warnf(format string, v ...interface{}) {
246258
l.Output("", Lwarn, 2, fmt.Sprintf(format, v...))
247259
}
248260

261+
// Warn prints a warning information.
249262
func (l *Logger) Warn(v ...interface{}) { l.Output("", Lwarn, 2, fmt.Sprintln(v...)) }
250263

251264
// -----------------------------------------
252265

266+
// Errorf prints an error information.
253267
func (l *Logger) Errorf(format string, v ...interface{}) {
254268
l.Output("", Lerror, 2, fmt.Sprintf(format, v...))
255269
}
256270

271+
// Error prints an error information.
257272
func (l *Logger) Error(v ...interface{}) { l.Output("", Lerror, 2, fmt.Sprintln(v...)) }
258273

259274
// -----------------------------------------
260275

276+
// Fatal prints an error information and exit app.
261277
func (l *Logger) Fatal(v ...interface{}) {
262278
l.Output("", Lfatal, 2, fmt.Sprint(v...))
263279
os.Exit(1)
@@ -300,6 +316,7 @@ func (l *Logger) Panicln(v ...interface{}) {
300316

301317
// -----------------------------------------
302318

319+
// Stack prints a stack trace of all goroutines.
303320
func (l *Logger) Stack(v ...interface{}) {
304321
s := fmt.Sprint(v...)
305322
s += "\n"
@@ -310,6 +327,7 @@ func (l *Logger) Stack(v ...interface{}) {
310327
l.Output("", Lerror, 2, s)
311328
}
312329

330+
// SingleStack prints a stack trace of the calling goroutines.
313331
func (l *Logger) SingleStack(v ...interface{}) {
314332
s := fmt.Sprint(v...)
315333
s += "\n"
@@ -322,6 +340,7 @@ func (l *Logger) SingleStack(v ...interface{}) {
322340

323341
// -----------------------------------------
324342

343+
// Stat func.
325344
func (l *Logger) Stat() (stats []int64) {
326345
l.mu.Lock()
327346
v := l.levelStats
@@ -391,14 +410,21 @@ func SetPrefix(prefix string) {
391410
Std.SetPrefix(prefix)
392411
}
393412

413+
// SetOutputLevel sets output level.
394414
func SetOutputLevel(lvl int) {
395415
Std.SetOutputLevel(lvl)
396416
}
397417

418+
// GetOutputLevel returns output level.
398419
func GetOutputLevel() int {
399420
return Std.Level
400421
}
401422

423+
// CanOutput returns to output a message or not.
424+
func CanOutput(lvl int) bool {
425+
return lvl >= Std.Level
426+
}
427+
402428
// -----------------------------------------
403429

404430
// Print calls Output to print to the standard logger.
@@ -421,13 +447,15 @@ func Println(v ...interface{}) {
421447

422448
// -----------------------------------------
423449

450+
// Debugf prints a debug information.
424451
func Debugf(format string, v ...interface{}) {
425452
if Ldebug < Std.Level {
426453
return
427454
}
428455
Std.Output("", Ldebug, 2, fmt.Sprintf(format, v...))
429456
}
430457

458+
// Debug prints a debug information.
431459
func Debug(v ...interface{}) {
432460
if Ldebug < Std.Level {
433461
return
@@ -437,13 +465,15 @@ func Debug(v ...interface{}) {
437465

438466
// -----------------------------------------
439467

468+
// Infof prints a prompting information.
440469
func Infof(format string, v ...interface{}) {
441470
if Linfo < Std.Level {
442471
return
443472
}
444473
Std.Output("", Linfo, 2, fmt.Sprintf(format, v...))
445474
}
446475

476+
// Info prints a prompting information.
447477
func Info(v ...interface{}) {
448478
if Linfo < Std.Level {
449479
return
@@ -453,18 +483,22 @@ func Info(v ...interface{}) {
453483

454484
// -----------------------------------------
455485

486+
// Warnf prints a warning information.
456487
func Warnf(format string, v ...interface{}) {
457488
Std.Output("", Lwarn, 2, fmt.Sprintf(format, v...))
458489
}
459490

491+
// Warn prints a warning information.
460492
func Warn(v ...interface{}) { Std.Output("", Lwarn, 2, fmt.Sprintln(v...)) }
461493

462494
// -----------------------------------------
463495

496+
// Errorf prints an error information.
464497
func Errorf(format string, v ...interface{}) {
465498
Std.Output("", Lerror, 2, fmt.Sprintf(format, v...))
466499
}
467500

501+
// Error prints an error information.
468502
func Error(v ...interface{}) { Std.Output("", Lerror, 2, fmt.Sprintln(v...)) }
469503

470504
// -----------------------------------------
@@ -512,10 +546,12 @@ func Panicln(v ...interface{}) {
512546

513547
// -----------------------------------------
514548

549+
// Stack prints a stack trace of all goroutines.
515550
func Stack(v ...interface{}) {
516551
Std.Stack(v...)
517552
}
518553

554+
// SingleStack prints a stack trace of the calling goroutines.
519555
func SingleStack(v ...interface{}) {
520556
Std.SingleStack(v...)
521557
}

0 commit comments

Comments
 (0)