-
Notifications
You must be signed in to change notification settings - Fork 116
/
options.go
79 lines (69 loc) · 1.84 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package log
import (
"io"
)
// Option is the type to specify settings modifier
// for the logger operation.
type Option func(s *settings)
// SetLevel sets the level for the logger.
// The level defaults to the lowest level, trce.
func SetLevel(level Level) Option {
return func(s *settings) {
if level == DoNotChange {
return
}
s.level = &level
}
}
// SetCallerFile enables or disables logging the caller file.
// The default is disabled.
func SetCallerFile(enabled bool) Option {
return func(s *settings) {
s.caller.file = &enabled
}
}
// SetCallerLine enables or disables logging the caller line number.
// The default is disabled.
func SetCallerLine(enabled bool) Option {
return func(s *settings) {
s.caller.line = &enabled
}
}
// SetCallerFunc enables or disables logging the caller function.
// The default is disabled.
func SetCallerFunc(enabled bool) Option {
return func(s *settings) {
s.caller.funC = &enabled
}
}
// SetFormat set the format for the logger.
// The format defaults to FormatConsole.
func SetFormat(format Format) Option {
return func(s *settings) {
s.format = &format
}
}
// SetWriter set the writer for the logger.
// The writer defaults to os.Stdout.
func SetWriter(writer io.Writer) Option {
return func(s *settings) {
s.writer = writer
}
}
// AddContext adds the context for the logger as a key values pair.
// It adds them in order. If a key already exists, the value is added to the
// existing values.
func AddContext(key, value string) Option {
return func(s *settings) {
for i := range s.context {
if s.context[i].key == key {
s.context[i].values = append(s.context[i].values, value)
return
}
}
newKV := contextKeyValues{key: key, values: []string{value}}
s.context = append(s.context, newKV)
}
}