Skip to content

Commit 16e1394

Browse files
committed
feat: enhance logging with caller information and add corresponding tests
- Add `path/filepath` and `runtime` imports - Modify logging functions to include caller information - Add `addCallerInfo` method to append file and line number to log messages - Create a new test file `slog_test.go` - Add tests for `addCallerInfo` method to verify it appends caller information correctly Signed-off-by: appleboy <[email protected]>
1 parent a29f1d0 commit 16e1394

File tree

2 files changed

+46
-8
lines changed

2 files changed

+46
-8
lines changed

slog/slog.go

+19-8
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
"log/slog"
77
"os"
8+
"path/filepath"
9+
"runtime"
810
)
911

1012
// New to create new interface for logger
@@ -20,35 +22,44 @@ type Manager struct {
2022
}
2123

2224
func (l Manager) Infof(format string, args ...interface{}) {
23-
l.logger.InfoContext(context.Background(), fmt.Sprintf(format, args...))
25+
l.logger.InfoContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
2426
}
2527

2628
func (l Manager) Errorf(format string, args ...interface{}) {
27-
l.logger.ErrorContext(context.Background(), fmt.Sprintf(format, args...))
29+
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
2830
}
2931

3032
func (l Manager) Fatalf(format string, args ...interface{}) {
31-
l.logger.ErrorContext(context.Background(), fmt.Sprintf(format, args...))
33+
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
3234
os.Exit(1)
3335
}
3436

3537
func (l Manager) Debugf(format string, args ...interface{}) {
36-
l.logger.DebugContext(context.Background(), fmt.Sprintf(format, args...))
38+
l.logger.DebugContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
3739
}
3840

3941
func (l Manager) Info(args ...interface{}) {
40-
l.logger.InfoContext(context.Background(), fmt.Sprint(args...))
42+
l.logger.InfoContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
4143
}
4244

4345
func (l Manager) Error(args ...interface{}) {
44-
l.logger.ErrorContext(context.Background(), fmt.Sprint(args...))
46+
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
4547
}
4648

4749
func (l Manager) Fatal(args ...interface{}) {
48-
l.logger.ErrorContext(context.Background(), fmt.Sprint(args...))
50+
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
4951
os.Exit(1)
5052
}
5153

5254
func (l Manager) Debug(args ...interface{}) {
53-
l.logger.DebugContext(context.Background(), fmt.Sprint(args...))
55+
l.logger.DebugContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
56+
}
57+
58+
func (l Manager) addCallerInfo(msg string) string {
59+
_, file, line, ok := runtime.Caller(2)
60+
if !ok {
61+
return msg
62+
}
63+
file = filepath.Base(file)
64+
return fmt.Sprintf("%s:%d %s", file, line, msg)
5465
}

slog/slog_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package slog
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestAddCallerInfo(t *testing.T) {
8+
manager := New()
9+
10+
tests := []struct {
11+
name string
12+
msg string
13+
}{
14+
{"Test with simple message", "This is a test message"},
15+
{"Test with empty message", ""},
16+
{"Test with special characters", "Special characters !@#$%^&*()"},
17+
}
18+
19+
for _, tt := range tests {
20+
t.Run(tt.name, func(t *testing.T) {
21+
result := manager.addCallerInfo(tt.msg)
22+
if result == tt.msg {
23+
t.Errorf("Expected caller info to be added, but got: %s", result)
24+
}
25+
})
26+
}
27+
}

0 commit comments

Comments
 (0)