diff --git a/log/hook.go b/log/hook.go index 5268edce3..a03e4acbb 100644 --- a/log/hook.go +++ b/log/hook.go @@ -26,8 +26,8 @@ func (h *Hook) Fire(entry *logrus.Entry) error { ctx: entry.Context, data: map[string]any(entry.Data), level: log.Level(entry.Level), - time: entry.Time, message: entry.Message, + time: entry.Time, } data := entry.Data diff --git a/log/hook_test.go b/log/hook_test.go new file mode 100644 index 000000000..76fc2463a --- /dev/null +++ b/log/hook_test.go @@ -0,0 +1,149 @@ +package log + +import ( + "context" + "testing" + "time" + + "github.com/sirupsen/logrus" + "github.com/stretchr/testify/assert" + + "github.com/goravel/framework/contracts/log" + mockslog "github.com/goravel/framework/mocks/log" +) + +func TestHook_Fire(t *testing.T) { + var ( + mockHook *mockslog.Hook + + ctx = context.Background() + code = "123" + domain = "example.com" + hint = "hint" + level = logrus.InfoLevel + message = "message" + owner = "owner" + request = map[string]any{"key": "value"} + response = map[string]any{"key": "value"} + stacktrace = map[string]any{"key": "value"} + now = time.Now() + tags = []string{"tag1", "tag2"} + user = "user" + with = map[string]any{"key": "value"} + ) + + tests := []struct { + name string + entry *logrus.Entry + setup func() + expectError error + }{ + { + name: "full data", + entry: &logrus.Entry{ + Context: ctx, + Data: logrus.Fields{ + "root": map[string]any{ + "code": code, + "domain": domain, + "hint": hint, + "owner": owner, + "request": request, + "response": response, + "stacktrace": stacktrace, + "tags": tags, + "user": user, + "with": with, + }, + }, + Level: level, + Time: now, + Message: message, + }, + setup: func() { + mockHook.EXPECT().Fire(&Entry{ + ctx: ctx, + code: code, + data: map[string]any{ + "root": map[string]any{ + "code": code, + "domain": domain, + "hint": hint, + "owner": owner, + "request": request, + "response": response, + "stacktrace": stacktrace, + "tags": tags, + "user": user, + "with": with, + }, + }, + domain: domain, + hint: hint, + level: log.Level(level), + message: message, + owner: owner, + request: request, + response: response, + stacktrace: stacktrace, + tags: tags, + time: now, + user: user, + with: with, + }).Return(nil).Once() + }, + }, + { + name: "empty data", + entry: &logrus.Entry{ + Context: ctx, + Data: logrus.Fields{}, + Level: level, + Time: now, + Message: message, + }, + setup: func() { + mockHook.EXPECT().Fire(&Entry{ + ctx: ctx, + data: map[string]any{}, + level: log.Level(level), + message: message, + time: now, + }).Return(nil).Once() + }, + }, + { + name: "Fire returns error", + entry: &logrus.Entry{ + Context: ctx, + Data: logrus.Fields{}, + Level: level, + Time: now, + Message: message, + }, + setup: func() { + mockHook.EXPECT().Fire(&Entry{ + ctx: ctx, + data: map[string]any{}, + level: log.Level(level), + message: message, + time: now, + }).Return(assert.AnError).Once() + }, + expectError: assert.AnError, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + mockHook = mockslog.NewHook(t) + tt.setup() + + hook := &Hook{instance: mockHook} + + err := hook.Fire(tt.entry) + + assert.Equal(t, tt.expectError, err) + }) + } +} diff --git a/log/logrus_writer.go b/log/logrus_writer.go index e6ad00cc4..e33b7a635 100644 --- a/log/logrus_writer.go +++ b/log/logrus_writer.go @@ -5,14 +5,15 @@ import ( "io" "os" + "github.com/rotisserie/eris" + "github.com/sirupsen/logrus" + "github.com/goravel/framework/contracts/config" "github.com/goravel/framework/contracts/foundation" "github.com/goravel/framework/contracts/http" "github.com/goravel/framework/contracts/log" "github.com/goravel/framework/errors" "github.com/goravel/framework/log/logger" - "github.com/rotisserie/eris" - "github.com/sirupsen/logrus" ) func NewLogrus() *logrus.Logger { diff --git a/log/logrus_writer_test.go b/log/logrus_writer_test.go index 60263140b..860d4a08e 100644 --- a/log/logrus_writer_test.go +++ b/log/logrus_writer_test.go @@ -54,7 +54,7 @@ func TestLogrus(t *testing.T) { log, err = NewApplication(mockConfig, j) ctx := context.Background() - ctx = context.WithValue(ctx, "key", "value") + ctx = context.WithValue(ctx, testContextKey("key"), "value") log.WithContext(ctx).Info("Goravel") }, assert: func() { diff --git a/log/utils_test.go b/log/utils_test.go index be99d7dbc..8f9d9ecb5 100644 --- a/log/utils_test.go +++ b/log/utils_test.go @@ -7,20 +7,22 @@ import ( "github.com/gookit/goutil/testutil/assert" ) +type testContextKey any + func TestGetContextValues(t *testing.T) { ctx := context.Background() values := make(map[any]any) getContextValues(ctx, values) assert.Equal(t, make(map[any]any), values) - ctx = context.WithValue(ctx, "a", "b") - ctx = context.WithValue(ctx, 1, 2) - ctx = context.WithValue(ctx, "c", map[string]any{"d": "e"}) + ctx = context.WithValue(ctx, testContextKey("a"), "b") + ctx = context.WithValue(ctx, testContextKey(1), 2) + ctx = context.WithValue(ctx, testContextKey("c"), map[string]any{"d": "e"}) type T struct { A string } - ctx = context.WithValue(ctx, "d", T{A: "a"}) + ctx = context.WithValue(ctx, testContextKey("d"), T{A: "a"}) values = make(map[any]any) getContextValues(ctx, values) diff --git a/support/file/file.go b/support/file/file.go index 876325f33..5d42cc029 100644 --- a/support/file/file.go +++ b/support/file/file.go @@ -1,7 +1,6 @@ package file import ( - "fmt" "os" "path/filepath" "strings" @@ -19,7 +18,6 @@ func ClientOriginalExtension(file string) string { func Contain(file string, search string) bool { if Exists(file) { data, err := os.ReadFile(file) - fmt.Println(string(data)) if err != nil { return false }