-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
63 lines (53 loc) · 1.41 KB
/
options.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
// Copyright (c) 2019,CAO HONGJU. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package xlog
import "strings"
// An Option configures a Logger.
type Option interface {
apply(*Logger)
}
// optionFunc wraps a func so it satisfies the Option interface.
type optionFunc func(*Logger)
func (f optionFunc) apply(log *Logger) {
f(log)
}
// Named adds a new path segment to the logger's name.
// Segments are joined by periods with '.'.
func Named(s string) Option {
return optionFunc(func(log *Logger) {
if s == "" {
return
}
if log.name == "" {
log.name = s
} else {
log.name = strings.Join([]string{log.name, s}, ".")
}
})
}
// Fields adds preset fields to the Logger.
func Fields(fs ...Field) Option {
return optionFunc(func(log *Logger) {
if len(fs) == 0 {
return
}
log.ctx = append(log.ctx, fs...)
})
}
// AddCaller configures the Logger to annotate each message with the filename
// and line number of caller.
func AddCaller() Option {
return optionFunc(func(log *Logger) {
log.addCaller = true
})
}
// AddCallerSkip increases the number of callers skipped by caller annotation
// (as enabled by the AddCaller option).
//
// When building wrappers around the Logger, use this option to set the wrapping depth.
func AddCallerSkip(skip int) Option {
return optionFunc(func(log *Logger) {
log.callerSkip += skip
})
}