Skip to content

Commit 09267a9

Browse files
nurzhan-saktaganovKaymeKaydex
authored andcommitted
SlogLoggerf: don't fmt.Sprintf if logLevel is not enabled
1 parent cd0af0e commit 09267a9

File tree

2 files changed

+13
-0
lines changed

2 files changed

+13
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ CHANGES:
1111
* Bump go-tarantool from v2.2.1 to v2.3.0.
1212
* Make comments more go-style.
1313
* Router.cronDiscovery: log panic in another goroutine.
14+
* Tiny optimization in SlogLoggerf.
1415

1516
TESTS:
1617
* Fixed etcd overlapping ports.

providers.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,21 +101,33 @@ type SlogLoggerf struct {
101101

102102
// Debugf implements Debugf method for LogfProvider interface
103103
func (s *SlogLoggerf) Debugf(ctx context.Context, format string, v ...any) {
104+
if !s.Logger.Enabled(ctx, slog.LevelDebug) {
105+
return
106+
}
104107
s.Logger.DebugContext(ctx, fmt.Sprintf(format, v...))
105108
}
106109

107110
// Infof implements Infof method for LogfProvider interface
108111
func (s SlogLoggerf) Infof(ctx context.Context, format string, v ...any) {
112+
if !s.Logger.Enabled(ctx, slog.LevelInfo) {
113+
return
114+
}
109115
s.Logger.InfoContext(ctx, fmt.Sprintf(format, v...))
110116
}
111117

112118
// Warnf implements Warnf method for LogfProvider interface
113119
func (s SlogLoggerf) Warnf(ctx context.Context, format string, v ...any) {
120+
if !s.Logger.Enabled(ctx, slog.LevelWarn) {
121+
return
122+
}
114123
s.Logger.WarnContext(ctx, fmt.Sprintf(format, v...))
115124
}
116125

117126
// Errorf implements Errorf method for LogfProvider interface
118127
func (s SlogLoggerf) Errorf(ctx context.Context, format string, v ...any) {
128+
if !s.Logger.Enabled(ctx, slog.LevelError) {
129+
return
130+
}
119131
s.Logger.ErrorContext(ctx, fmt.Sprintf(format, v...))
120132
}
121133

0 commit comments

Comments
 (0)