-
Notifications
You must be signed in to change notification settings - Fork 88
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: [#462] Custom Log Channel cannot take values from some functions #640
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d4d2715
add methods to entry
kkumar-gcc 7581f6d
Merge branch 'master' into kkumar-gcc/#462
kkumar-gcc 46066d3
chore: update mocks
kkumar-gcc 33cdaff
Merge branch 'master' into kkumar-gcc/#462
kkumar-gcc 76cc71f
add test cases for log
kkumar-gcc aafe41a
Merge remote-tracking branch 'origin/kkumar-gcc/#462' into kkumar-gcc…
kkumar-gcc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,12 +8,15 @@ import ( | |
"os" | ||
"os/exec" | ||
"reflect" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/spf13/cast" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/goravel/framework/contracts/filesystem" | ||
contractshttp "github.com/goravel/framework/contracts/http" | ||
logcontracts "github.com/goravel/framework/contracts/log" | ||
contractsession "github.com/goravel/framework/contracts/session" | ||
"github.com/goravel/framework/contracts/validation" | ||
"github.com/goravel/framework/foundation/json" | ||
|
@@ -390,6 +393,30 @@ func TestLogrus(t *testing.T) { | |
_ = file.Remove("storage") | ||
} | ||
|
||
func TestLogrusWithCustomLogger(t *testing.T) { | ||
mockConfig := &configmock.Config{} | ||
mockConfig.On("GetString", "logging.default").Return("customLogger").Once() | ||
mockConfig.On("GetString", "logging.channels.customLogger.driver").Return("custom").Twice() | ||
mockConfig.On("Get", "logging.channels.customLogger.via").Return(&CustomLogger{}).Twice() | ||
mockConfig.On("GetString", "app.timezone").Return("UTC") | ||
mockConfig.On("GetString", "app.env").Return("test") | ||
|
||
filename := "custom.log" | ||
|
||
logger := NewApplication(mockConfig, json.NewJson()) | ||
logger.Channel("customLogger"). | ||
WithTrace(). | ||
With(map[string]any{"filename": filename}). | ||
User(map[string]any{"name": "kkumar-gcc"}). | ||
Owner("[email protected]"). | ||
Code("code").Info("Goravel") | ||
|
||
expectedContent := "info: Goravel\ncustom_code: code\ncustom_user: map[name:kkumar-gcc]\n" | ||
assert.True(t, file.Contain(filename, expectedContent), "Log file content does not match expected output") | ||
|
||
assert.Nil(t, file.Remove(filename)) | ||
} | ||
|
||
func TestLogrus_Fatal(t *testing.T) { | ||
mockConfig := initMockConfig() | ||
mockDriverConfig(mockConfig) | ||
|
@@ -521,6 +548,50 @@ func mockDriverConfig(mockConfig *configmock.Config) { | |
mockConfig.On("GetString", "app.env").Return("test") | ||
} | ||
|
||
type CustomLogger struct { | ||
} | ||
|
||
func (logger *CustomLogger) Handle(channel string) (logcontracts.Hook, error) { | ||
return &CustomHook{}, nil | ||
} | ||
|
||
type CustomHook struct { | ||
} | ||
|
||
func (h *CustomHook) Levels() []logcontracts.Level { | ||
return []logcontracts.Level{ | ||
logcontracts.InfoLevel, | ||
} | ||
} | ||
|
||
func (h *CustomHook) Fire(entry logcontracts.Entry) error { | ||
with := entry.With() | ||
filename, ok := with["filename"] | ||
if ok { | ||
var builder strings.Builder | ||
message := entry.Message() | ||
if len(message) > 0 { | ||
builder.WriteString(fmt.Sprintf("%s: %v\n", entry.Level(), message)) | ||
} | ||
|
||
code := entry.Code() | ||
if len(code) > 0 { | ||
builder.WriteString(fmt.Sprintf("custom_code: %v\n", code)) | ||
} | ||
|
||
user := entry.User() | ||
if user != nil { | ||
builder.WriteString(fmt.Sprintf("custom_user: %v\n", user)) | ||
} | ||
|
||
err := file.Create(cast.ToString(filename), builder.String()) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
type TestRequest struct{} | ||
|
||
func (r *TestRequest) Header(key string, defaultValue ...string) string { | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a test case for this function?