Skip to content

Commit

Permalink
feat: enhance logging with caller information and add corresponding t…
Browse files Browse the repository at this point in the history
…ests

- 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]>
  • Loading branch information
appleboy committed Jan 19, 2025
1 parent a29f1d0 commit 16e1394
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
27 changes: 19 additions & 8 deletions slog/slog.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log/slog"

Check failure on line 6 in slog/slog.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest @ Go 1.18

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.18.10/x64/src/log/slog)

Check failure on line 6 in slog/slog.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest @ Go 1.19

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.19.13/x64/src/log/slog)

Check failure on line 6 in slog/slog.go

View workflow job for this annotation

GitHub Actions / ubuntu-latest @ Go 1.20

package log/slog is not in GOROOT (/opt/hostedtoolcache/go/1.20.14/x64/src/log/slog)
"os"
"path/filepath"
"runtime"
)

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

func (l Manager) Infof(format string, args ...interface{}) {
l.logger.InfoContext(context.Background(), fmt.Sprintf(format, args...))
l.logger.InfoContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
}

func (l Manager) Errorf(format string, args ...interface{}) {
l.logger.ErrorContext(context.Background(), fmt.Sprintf(format, args...))
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
}

func (l Manager) Fatalf(format string, args ...interface{}) {
l.logger.ErrorContext(context.Background(), fmt.Sprintf(format, args...))
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
os.Exit(1)
}

func (l Manager) Debugf(format string, args ...interface{}) {
l.logger.DebugContext(context.Background(), fmt.Sprintf(format, args...))
l.logger.DebugContext(context.Background(), l.addCallerInfo(fmt.Sprintf(format, args...)))
}

func (l Manager) Info(args ...interface{}) {
l.logger.InfoContext(context.Background(), fmt.Sprint(args...))
l.logger.InfoContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
}

func (l Manager) Error(args ...interface{}) {
l.logger.ErrorContext(context.Background(), fmt.Sprint(args...))
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
}

func (l Manager) Fatal(args ...interface{}) {
l.logger.ErrorContext(context.Background(), fmt.Sprint(args...))
l.logger.ErrorContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
os.Exit(1)
}

func (l Manager) Debug(args ...interface{}) {
l.logger.DebugContext(context.Background(), fmt.Sprint(args...))
l.logger.DebugContext(context.Background(), l.addCallerInfo(fmt.Sprint(args...)))
}

func (l Manager) addCallerInfo(msg string) string {
_, file, line, ok := runtime.Caller(2)
if !ok {
return msg
}
file = filepath.Base(file)
return fmt.Sprintf("%s:%d %s", file, line, msg)
}
27 changes: 27 additions & 0 deletions slog/slog_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package slog

import (
"testing"
)

func TestAddCallerInfo(t *testing.T) {
manager := New()

tests := []struct {
name string
msg string
}{
{"Test with simple message", "This is a test message"},
{"Test with empty message", ""},
{"Test with special characters", "Special characters !@#$%^&*()"},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := manager.addCallerInfo(tt.msg)
if result == tt.msg {
t.Errorf("Expected caller info to be added, but got: %s", result)
}
})
}
}

0 comments on commit 16e1394

Please sign in to comment.