-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathoptions.go
137 lines (118 loc) · 3.3 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
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
package log
import (
"context"
"io"
"os"
"runtime"
"strconv"
"golang.org/x/term"
"go.opentelemetry.io/otel/trace"
)
type (
// LogOption is a function that applies a configuration option to a logger.
LogOption func(*options)
// DisableBufferingFunc is a function that returns true if the logger
// should disable buffering for the given context.
DisableBufferingFunc func(context.Context) bool
// FormatFunc is a function that formats a log entry.
FormatFunc func(e *Entry) []byte
options struct {
disableBuffering DisableBufferingFunc
debug bool
w io.Writer
format FormatFunc
keyvals kvList
kvfuncs []func(context.Context) []KV
maxsize int
}
)
// DefaultMaxSize is the default maximum size of a single log message or value
// in bytes. It's also the maximum number of elements in a slice value.
const DefaultMaxSize = 1024
// IsTracing returns true if the context contains a trace created via the
// go.opentelemetry.io/otel/trace package. It is the default
// DisableBufferingFunc used by newly created loggers.
func IsTracing(ctx context.Context) bool {
span := trace.SpanFromContext(ctx)
return span.SpanContext().IsValid()
}
// WithDisableBuffering sets the DisableBufferingFunc called to assess whether
// buffering should be disabled.
func WithDisableBuffering(fn DisableBufferingFunc) LogOption {
return func(o *options) {
o.disableBuffering = fn
}
}
// WithDebug enables debug logging and disables buffering.
func WithDebug() LogOption {
return func(o *options) {
o.debug = true
}
}
// WithNoDebug disables debug logging.
func WithNoDebug() LogOption {
return func(o *options) {
o.debug = false
}
}
// WithOutput sets the log output.
func WithOutput(w io.Writer) LogOption {
return func(o *options) {
o.w = w
}
}
// WithFormat sets the log format.
func WithFormat(fn FormatFunc) LogOption {
return func(o *options) {
o.format = fn
}
}
// WithMaxSize sets the maximum size of a single log message or value.
func WithMaxSize(n int) LogOption {
return func(o *options) {
o.maxsize = n
}
}
// WithFileLocation adds the "file" key to each log entry with the parent
// directory, file and line number of the caller: "file=dir/file.go:123".
func WithFileLocation() LogOption {
return WithFunc(func(context.Context) []KV {
_, file, line, _ := runtime.Caller(4)
short := file
second := false
for i := len(file) - 1; i > 0; i-- {
if file[i] == '/' {
if second {
short = file[i+1:]
break
}
second = true
}
}
return []KV{{"file", short + ":" + strconv.Itoa(line)}}
})
}
// WithFunc sets a key/value pair generator function to be called with every
// log entry. The generated key/value pairs are added to the log entry.
func WithFunc(fn func(context.Context) []KV) LogOption {
return func(o *options) {
o.kvfuncs = append(o.kvfuncs, fn)
}
}
// IsTerminal returns true if the process is running in a terminal.
func IsTerminal() bool {
return term.IsTerminal(int(os.Stdout.Fd()))
}
// defaultOptions returns a new options struct with default values.
func defaultOptions() *options {
format := FormatText
if IsTerminal() {
format = FormatTerminal
}
return &options{
disableBuffering: IsTracing,
w: os.Stdout,
format: format,
maxsize: DefaultMaxSize,
}
}