-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
221 lines (194 loc) · 5.08 KB
/
options.go
File metadata and controls
221 lines (194 loc) · 5.08 KB
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
//
// Copyright (C) 2021 - 2025 Dmitry Kolesnikov
//
// This file may be modified and distributed under the terms
// of the MIT license. See the LICENSE file for details.
// https://github.com/fogfish/logger
//
package logger
import (
"io"
"log/slog"
"os"
"strings"
"github.com/fogfish/logger/v3/internal/trie"
)
var (
// Preset for console logging
Console = []Option{
func(o *opts) { o.attributes = append(o.attributes, attrLogLevel7Shorten(true)) },
WithTimeFormat("[15:04:05.000]"),
WithLogLevel(NOTICE),
WithLogLevelFromEnv(),
WithSourceShorten(),
WithLogLevelForModFromEnv(),
}
// Preset for CloudWatch logging
CloudWatch = []Option{
WithLogLevelFromEnv(),
WithLogLevel7(),
WithSourceShorten(),
WithoutTimestamp(),
WithLogLevelForModFromEnv(),
}
)
// The logger config option
type Option func(*opts)
type opts struct {
writer io.Writer
level slog.Leveler
attributes Attributes
addSource bool
trie *trie.Node
}
func defaultOpts(preset ...Option) *opts {
opt := &opts{
writer: os.Stdout,
level: INFO,
attributes: Attributes{},
addSource: false,
}
for _, f := range preset {
f(opt)
}
return opt
}
// Config Log writer, default os.Stdout
func WithWriter(w io.Writer) Option {
return func(o *opts) {
o.writer = w
}
}
// Exclude timestamp, required by CloudWatch
func WithoutTimestamp() Option {
return func(o *opts) {
o.attributes = append(o.attributes, attrNoTimestamp)
}
}
// Configure the time format
func WithTimeFormat(format string) Option {
return func(o *opts) {
o.attributes = append(o.attributes, attrLogTimeFormat(format))
}
}
// Config Log Level, default INFO
func WithLogLevel(level slog.Leveler) Option {
return func(o *opts) {
o.level = level
}
}
// Config Log Level from env CONFIG_LOG_LEVEL, default INFO
//
// export CONFIG_LOG_LEVEL=DEBUG
func WithLogLevelFromEnv() Option {
return func(o *opts) {
level, defined := os.LookupEnv("CONFIG_LOG_LEVEL")
if !defined {
return
}
if lvl, has := longNames[level]; has {
o.level = lvl
}
}
}
// WithLevel7 enables from DEBUG to EMERGENCY levels
func WithLogLevel7() Option {
return func(o *opts) {
o.attributes = append(o.attributes, attrLogLevel7(false))
}
}
// Config Log Level to be 3 letters only from DEB to EMR
func WithLogLevelShorten() Option {
return func(o *opts) {
o.attributes = append(o.attributes, attrLogLevel7Shorten(false))
}
}
// The logger allows you to define log levels for different modules with
// flexible granularity. Log levels can be set explicitly using this configuration
// options.
//
// log.WithLogLevelForMod(map[string]slog.Level{
// "github.com/fogfish/logger": log.INFO,
// "github.com/you/application": log.DEBUG,
// })
//
// The logger uses prefix matching to determine the appropriate log level based
// on the source code path:
//
// * Per File: A log level defined for a specific file
// (e.g., github.com/fogfish/logger/logger.go) applies only to that file.
//
// * Per Module: A log level set for a module (e.g., github.com/fogfish/logger)
// applies to all files within that module.
//
// * Per User/Namespace: A log level defined at a higher level
// (e.g., github.com/fogfish) applies to all modules under that namespace.
func WithLogLevelForMod(mods map[string]slog.Level) Option {
return func(o *opts) {
o.trie = trie.New()
for mod, lvl := range mods {
o.trie.Append(mod, lvl)
}
}
}
// The logger allows you to define log levels for different modules with
// flexible granularity. Log levels can be set explicitly using environment
// variables CONFIG_LOG_LEVEL_{NAME}:
//
// export CONFIG_LOG_LEVEL_DEBUG=github.com/fogfish/logger:github.com/your/app
// export CONFIG_LOG_LEVEL_INFO=github.com/fogfish
//
// * Per File: A log level defined for a specific file
// (e.g., github.com/fogfish/logger/logger.go) applies only to that file.
//
// * Per Module: A log level set for a module (e.g., github.com/fogfish/logger)
// applies to all files within that module.
//
// * Per User/Namespace: A log level defined at a higher level
// (e.g., github.com/fogfish) applies to all modules under that namespace.
func WithLogLevelForModFromEnv() Option {
return func(o *opts) {
root := trie.New()
for lvl, name := range levelLongName {
for _, mod := range fromEnvMods("CONFIG_LOG_LEVEL_" + name) {
root.Append(mod, lvl)
}
}
if len(root.Heir) != 0 {
o.trie = root
}
}
}
func fromEnvMods(key string) []string {
value, defined := os.LookupEnv(key)
if !defined {
return nil
}
return strings.Split(value, ":")
}
// Logs file name of the source file only
func WithSourceFileName() Option {
return func(o *opts) {
o.addSource = true
o.attributes = append(o.attributes, attrSourceFileName)
}
}
// Shorten Source file to few letters only
func WithSourceShorten() Option {
return func(o *opts) {
o.addSource = true
o.attributes = append(o.attributes, attrSourceShorten)
}
}
// Enable default logging of source file
func WithSource() Option {
return func(o *opts) {
o.addSource = true
}
}
// Disable logging of source file
func WithoutSource() Option {
return func(o *opts) {
o.addSource = false
}
}