-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathoptions_logger.go
More file actions
64 lines (50 loc) · 1.66 KB
/
options_logger.go
File metadata and controls
64 lines (50 loc) · 1.66 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
package frame
import (
"context"
"log/slog"
"github.com/pitabwire/util"
config2 "github.com/pitabwire/frame/config"
)
// WithLogger Option that helps with initialization of our internal dbLogger.
func WithLogger(opts ...util.Option) Option {
return func(ctx context.Context, s *Service) {
s.registerPlugin("logger")
var configOpts []util.Option
// Add telemetry log handler if available
if s.telemetryManager != nil {
configOpts = append(configOpts, util.WithLogHandler(s.telemetryManager.LogHandler()))
}
// Early return if no config is available
if s.Config() == nil {
log := util.NewLogger(ctx, append(configOpts, opts...)...)
s.logger = log.WithField("service", s.Name())
return
}
config, ok := s.Config().(config2.ConfigurationLogLevel)
if !ok {
log := util.NewLogger(ctx, append(configOpts, opts...)...)
s.logger = log.WithField("service", s.Name())
return
}
// Parse log level
if logLevel, err := util.ParseLevel(config.LoggingLevel()); err == nil {
configOpts = append(configOpts, util.WithLogLevel(logLevel))
}
// Add standard config options
configOpts = append(configOpts,
util.WithLogTimeFormat(config.LoggingTimeFormat()),
util.WithLogNoColor(!config.LoggingColored()))
// Add stack trace if enabled
if config.LoggingShowStackTrace() {
configOpts = append(configOpts, util.WithLogStackTrace())
}
log := util.NewLogger(ctx, append(configOpts, opts...)...)
s.logger = log.WithField("service", s.Name())
}
}
func (s *Service) Log(ctx context.Context) *util.LogEntry {
return s.logger.WithContext(ctx)
}
func (s *Service) SLog(ctx context.Context) *slog.Logger {
return s.Log(ctx).SLog()
}