Skip to content
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 6 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions contracts/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,20 @@ type Entry interface {
Time() time.Time
// Message returns the message of the entry.
Message() string
// Code returns the associated code.
Code() string
// With returns additional context data.
With() map[string]any
// User returns the user information.
User() any
// Tags returns the list of tags.
Tags() []string
// Owner returns the log's owner.
Owner() any
// Request returns the request data.
Request() map[string]any
// Response returns the response data.
Response() map[string]any
// Trace returns the stack trace or trace data.
Trace() map[string]any
}
48 changes: 44 additions & 4 deletions log/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@
)

type Entry struct {
ctx context.Context
level log.Level
time time.Time
message string
ctx context.Context
level log.Level
time time.Time
message string
code string
user any
tags []string
owner any
request map[string]any
response map[string]any
with map[string]any
stacktrace map[string]any
}

func (r *Entry) Context() context.Context {
Expand All @@ -29,3 +37,35 @@
func (r *Entry) Message() string {
return r.message
}

func (r *Entry) Code() string {
return r.code
}

func (r *Entry) With() map[string]any {
return r.with
}

func (r *Entry) User() any {
return r.user
}

func (r *Entry) Tags() []string {
return r.tags

Check warning on line 54 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L53-L54

Added lines #L53 - L54 were not covered by tests
}

func (r *Entry) Owner() any {
return r.owner

Check warning on line 58 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L57-L58

Added lines #L57 - L58 were not covered by tests
}

func (r *Entry) Request() map[string]any {
return r.request

Check warning on line 62 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L61-L62

Added lines #L61 - L62 were not covered by tests
}

func (r *Entry) Response() map[string]any {
return r.response

Check warning on line 66 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L65-L66

Added lines #L65 - L66 were not covered by tests
}

func (r *Entry) Trace() map[string]any {
return r.stacktrace

Check warning on line 70 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L69-L70

Added lines #L69 - L70 were not covered by tests
}
43 changes: 41 additions & 2 deletions log/logrus_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"github.com/rotisserie/eris"
"github.com/sirupsen/logrus"
"github.com/spf13/cast"

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/foundation"
Expand Down Expand Up @@ -365,10 +366,48 @@
}

func (h *Hook) Fire(entry *logrus.Entry) error {
Copy link
Contributor

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?

return h.instance.Fire(&Entry{
e := &Entry{
ctx: entry.Context,
level: log.Level(entry.Level),
time: entry.Time,
message: entry.Message,
})
}

data := entry.Data
if len(data) > 0 {
root, err := cast.ToStringMapE(data["root"])
if err != nil {
return err

Check warning on line 380 in log/logrus_writer.go

View check run for this annotation

Codecov / codecov/patch

log/logrus_writer.go#L380

Added line #L380 was not covered by tests
}

if code, ok := cast.ToStringE(root["code"]); ok == nil {
e.code = code
}

e.user = root["user"]

if tags, ok := cast.ToStringSliceE(root["tags"]); ok == nil {
e.tags = tags

Check warning on line 390 in log/logrus_writer.go

View check run for this annotation

Codecov / codecov/patch

log/logrus_writer.go#L390

Added line #L390 was not covered by tests
}

e.owner = root["owner"]

if req, err := cast.ToStringMapE(root["tags"]); err == nil {
e.request = req

Check warning on line 396 in log/logrus_writer.go

View check run for this annotation

Codecov / codecov/patch

log/logrus_writer.go#L396

Added line #L396 was not covered by tests
}

if res, err := cast.ToStringMapE(root["tags"]); err == nil {
e.response = res

Check warning on line 400 in log/logrus_writer.go

View check run for this annotation

Codecov / codecov/patch

log/logrus_writer.go#L400

Added line #L400 was not covered by tests
}

if context, ok := cast.ToStringMapE(root["context"]); ok == nil {
e.with = context
}

if stacktrace, ok := cast.ToStringMapE(root["stacktrace"]); ok == nil {
e.stacktrace = stacktrace
}
}

return h.instance.Fire(e)
}
71 changes: 71 additions & 0 deletions log/logrus_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Loading
Loading