Skip to content

Commit fc2c5f6

Browse files
rogercollflorianl
andcommitted
Update internal/global/logging.go
Co-authored-by: Florian Lehner <[email protected]>
1 parent 2facbb6 commit fc2c5f6

File tree

1 file changed

+25
-8
lines changed

1 file changed

+25
-8
lines changed

internal/global/logging.go

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package global // import "go.opentelemetry.io/ebpf-profiler/internal/global"
22

33
import (
4+
"context"
45
"fmt"
56
"log/slog"
67
"os"
@@ -37,52 +38,68 @@ func GetLogger() *slog.Logger {
3738
// formatting the message as a string for backward compatibility with
3839
// previous unstructured logging.
3940
func Infof(msg string, keysAndValues ...any) {
40-
GetLogger().Info(fmt.Sprintf(msg, keysAndValues...))
41+
if GetLogger().Enabled(context.Background(), slog.LevelInfo) {
42+
GetLogger().Info(fmt.Sprintf(msg, keysAndValues...))
43+
}
4144
}
4245

4346
// Info logs informational messages about the general state of the profiler.
4447
// This is a wrapper around Infof for convenience.
4548
func Info(msg string) {
46-
GetLogger().Info(msg)
49+
if GetLogger().Enabled(context.Background(), slog.LevelInfo) {
50+
GetLogger().Info(msg)
51+
}
4752
}
4853

4954
// Errorf logs error messages about exceptional states of the profiler.
5055
// This wrapper formats structured log data into a string message for
5156
// backward compatibility with older unstructured logs.
5257
func Errorf(msg string, keysAndValues ...any) {
53-
GetLogger().Error(fmt.Sprintf(msg, keysAndValues...))
58+
if GetLogger().Enabled(context.Background(), slog.LevelError) {
59+
GetLogger().Error(fmt.Sprintf(msg, keysAndValues...))
60+
}
5461
}
5562

5663
// Error logs error messages about exceptional states of the profiler.
5764
// This is a wrapper around Errorf for convenience.
5865
func Error(msg error) {
59-
GetLogger().Error(msg.Error())
66+
if GetLogger().Enabled(context.Background(), slog.LevelError) {
67+
GetLogger().Error(msg.Error())
68+
}
6069
}
6170

6271
// Debugf logs detailed debugging information about internal profiler behavior.
6372
// This wrapper converts structured log data into a string message for
6473
// backward compatibility with older unstructured logs.
6574
func Debugf(msg string, keysAndValues ...any) {
66-
GetLogger().Debug(fmt.Sprintf(msg, keysAndValues...))
75+
if GetLogger().Enabled(context.Background(), slog.LevelDebug) {
76+
GetLogger().Debug(fmt.Sprintf(msg, keysAndValues...))
77+
}
6778
}
6879

6980
// Debug logs detailed debugging information about internal profiler behavior.
7081
// This is a wrapper around Debugf for convenience.
7182
func Debug(msg string) {
72-
GetLogger().Debug(msg)
83+
if GetLogger().Enabled(context.Background(), slog.LevelDebug) {
84+
GetLogger().Debug(msg)
85+
}
7386
}
7487

7588
// Warnf logs warnings in the profiler — not errors, but likely more important
7689
// than informational messages. This wrapper preserves backward compatibility
7790
// by string-formatting structured log data.
7891
func Warnf(msg string, keysAndValues ...any) {
79-
GetLogger().Warn(fmt.Sprintf(msg, keysAndValues...))
92+
if GetLogger().Enabled(context.Background(), slog.LevelWarn) {
93+
GetLogger().Warn(fmt.Sprintf(msg, keysAndValues...))
94+
}
8095
}
8196

8297
// Warn logs warnings in the profiler — not errors, but likely more important
8398
// than informational messages. This is a wrapper around Warnf for convenience.
8499
func Warn(msg string) {
85-
GetLogger().Warn(msg)
100+
if GetLogger().Enabled(context.Background(), slog.LevelWarn) {
101+
GetLogger().Warn(msg)
102+
}
86103
}
87104

88105
// Fatalf logs a fatal error message and exits the program.

0 commit comments

Comments
 (0)