|
1 | 1 | package global // import "go.opentelemetry.io/ebpf-profiler/internal/global" |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "fmt" |
5 | 6 | "log/slog" |
6 | 7 | "os" |
@@ -37,52 +38,68 @@ func GetLogger() *slog.Logger { |
37 | 38 | // formatting the message as a string for backward compatibility with |
38 | 39 | // previous unstructured logging. |
39 | 40 | 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 | + } |
41 | 44 | } |
42 | 45 |
|
43 | 46 | // Info logs informational messages about the general state of the profiler. |
44 | 47 | // This is a wrapper around Infof for convenience. |
45 | 48 | func Info(msg string) { |
46 | | - GetLogger().Info(msg) |
| 49 | + if GetLogger().Enabled(context.Background(), slog.LevelInfo) { |
| 50 | + GetLogger().Info(msg) |
| 51 | + } |
47 | 52 | } |
48 | 53 |
|
49 | 54 | // Errorf logs error messages about exceptional states of the profiler. |
50 | 55 | // This wrapper formats structured log data into a string message for |
51 | 56 | // backward compatibility with older unstructured logs. |
52 | 57 | 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 | + } |
54 | 61 | } |
55 | 62 |
|
56 | 63 | // Error logs error messages about exceptional states of the profiler. |
57 | 64 | // This is a wrapper around Errorf for convenience. |
58 | 65 | func Error(msg error) { |
59 | | - GetLogger().Error(msg.Error()) |
| 66 | + if GetLogger().Enabled(context.Background(), slog.LevelError) { |
| 67 | + GetLogger().Error(msg.Error()) |
| 68 | + } |
60 | 69 | } |
61 | 70 |
|
62 | 71 | // Debugf logs detailed debugging information about internal profiler behavior. |
63 | 72 | // This wrapper converts structured log data into a string message for |
64 | 73 | // backward compatibility with older unstructured logs. |
65 | 74 | 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 | + } |
67 | 78 | } |
68 | 79 |
|
69 | 80 | // Debug logs detailed debugging information about internal profiler behavior. |
70 | 81 | // This is a wrapper around Debugf for convenience. |
71 | 82 | func Debug(msg string) { |
72 | | - GetLogger().Debug(msg) |
| 83 | + if GetLogger().Enabled(context.Background(), slog.LevelDebug) { |
| 84 | + GetLogger().Debug(msg) |
| 85 | + } |
73 | 86 | } |
74 | 87 |
|
75 | 88 | // Warnf logs warnings in the profiler — not errors, but likely more important |
76 | 89 | // than informational messages. This wrapper preserves backward compatibility |
77 | 90 | // by string-formatting structured log data. |
78 | 91 | 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 | + } |
80 | 95 | } |
81 | 96 |
|
82 | 97 | // Warn logs warnings in the profiler — not errors, but likely more important |
83 | 98 | // than informational messages. This is a wrapper around Warnf for convenience. |
84 | 99 | func Warn(msg string) { |
85 | | - GetLogger().Warn(msg) |
| 100 | + if GetLogger().Enabled(context.Background(), slog.LevelWarn) { |
| 101 | + GetLogger().Warn(msg) |
| 102 | + } |
86 | 103 | } |
87 | 104 |
|
88 | 105 | // Fatalf logs a fatal error message and exits the program. |
|
0 commit comments