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: [#282] Print the values in ctx when calling facades.Log().WithContext(ctx).Info() #759

Merged
merged 8 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions contracts/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,10 @@ type Entry interface {
Context() context.Context
// Data returns the data of the entry.
Data() Data
// Domain returns the domain of the entry.
Domain() string
// Hint returns the hint of the entry.
Hint() string
// Level returns the level of the entry.
Level() Level
// Message returns the message of the entry.
Expand Down
5 changes: 5 additions & 0 deletions log/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

"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/support/color"
)
Expand Down Expand Up @@ -37,6 +38,10 @@
}

func (r *Application) WithContext(ctx context.Context) log.Writer {
if httpCtx, ok := ctx.(http.Context); ok {
return NewWriter(r.instance.WithContext(httpCtx.Context()))
}

Check warning on line 43 in log/application.go

View check run for this annotation

Codecov / codecov/patch

log/application.go#L42-L43

Added lines #L42 - L43 were not covered by tests

return NewWriter(r.instance.WithContext(ctx))
}

Expand Down
20 changes: 15 additions & 5 deletions log/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@
)

type Entry struct {
code string
ctx context.Context
data log.Data
domain string
hint string
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
tags []string
time time.Time
user any
with map[string]any
}

func (r *Entry) Code() string {
Expand All @@ -35,6 +37,14 @@
return r.data
}

func (r *Entry) Domain() string {
return r.domain

Check warning on line 41 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L40-L41

Added lines #L40 - L41 were not covered by tests
}

func (r *Entry) Hint() string {
return r.hint

Check warning on line 45 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L44-L45

Added lines #L44 - L45 were not covered by tests
}

func (r *Entry) Level() log.Level {
return r.level
}
Expand Down
12 changes: 4 additions & 8 deletions log/formatter/general.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/support/str"
)

type General struct {
Expand Down Expand Up @@ -82,14 +83,9 @@ func (general *General) formatData(data logrus.Fields) (string, error) {
return "", err
}

for _, key := range []string{"code", "context", "domain", "hint", "owner", "request", "response", "tags", "user"} {
for _, key := range []string{"hint", "tags", "owner", "context", "with", "domain", "code", "request", "response", "user"} {
if value, exists := root[key]; exists && value != nil {
v, err := general.json.Marshal(value)
Copy link
Contributor Author

@hwbrzzl hwbrzzl Dec 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not all values can be marshaled to json, we can print them simply.

if err != nil {
return "", err
}

builder.WriteString(fmt.Sprintf("%s: %v\n", key, string(v)))
builder.WriteString(fmt.Sprintf("%s: %+v\n", str.Of(key).UcFirst().String(), value))
}
}

Expand Down Expand Up @@ -118,7 +114,7 @@ func (general *General) formatStackTraces(stackTraces any) (string, error) {
if err != nil {
return "", err
}
formattedTraces.WriteString("trace:\n")
formattedTraces.WriteString("Trace:\n")
root := traces.Root
if len(root.Stack) > 0 {
for _, stackStr := range root.Stack {
Expand Down
43 changes: 10 additions & 33 deletions log/formatter/general_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,10 @@ func (s *GeneralTestSuite) TestFormat() {
assert: func() {
formatLog, err := general.Format(s.entry)
s.Nil(err)
s.Contains(string(formatLog), "code: \"200\"")
s.Contains(string(formatLog), "domain: \"example.com\"")
s.Contains(string(formatLog), "owner: \"owner\"")
s.Contains(string(formatLog), "user: \"user1\"")
s.Contains(string(formatLog), "Code: 200")
s.Contains(string(formatLog), "Domain: example.com")
s.Contains(string(formatLog), "Owner: owner")
s.Contains(string(formatLog), "User: user1")
},
},
}
Expand Down Expand Up @@ -120,29 +120,6 @@ func (s *GeneralTestSuite) TestFormatData() {
s.Empty(formattedData)
},
},
{
name: "Invalid data type",
setup: func() {
data = logrus.Fields{
"root": map[string]any{
"code": "123",
"context": "sample",
"domain": "example.com",
"hint": make(chan int), // Invalid data type that will cause an error during value extraction
"owner": "owner",
"request": map[string]any{"method": "GET", "uri": "http://localhost"},
"response": map[string]any{"status": 200},
"tags": []string{"tag1", "tag2"},
"user": "user1",
},
}
},
assert: func() {
formattedData, err := general.formatData(data)
s.NotNil(err)
s.Empty(formattedData)
},
},
{
name: "Data is not empty",
setup: func() {
Expand All @@ -158,10 +135,10 @@ func (s *GeneralTestSuite) TestFormatData() {
assert: func() {
formattedData, err := general.formatData(data)
s.Nil(err)
s.Contains(formattedData, "code: \"200\"")
s.Contains(formattedData, "domain: \"example.com\"")
s.Contains(formattedData, "owner: \"owner\"")
s.Contains(formattedData, "user: \"user1\"")
s.Contains(formattedData, "Code: 200")
s.Contains(formattedData, "Domain: example.com")
s.Contains(formattedData, "Owner: owner")
s.Contains(formattedData, "User: user1")
},
},
}
Expand Down Expand Up @@ -190,7 +167,7 @@ func (s *GeneralTestSuite) TestFormatStackTraces() {
assert: func() {
traces, err := general.formatStackTraces(stackTraces)
s.Nil(err)
s.Equal("trace:\n", traces)
s.Equal("Trace:\n", traces)
},
},
{
Expand Down Expand Up @@ -223,7 +200,7 @@ func (s *GeneralTestSuite) TestFormatStackTraces() {
"/dummy/examples/logging/example.go:29 [main.(*Request).Validate]",
"/dummy/examples/logging/example.go:28 [main.(*Request).Validate]",
}
formattedStackTraces := "trace:\n" + strings.Join(stackTraces, "\n") + "\n"
formattedStackTraces := "Trace:\n" + strings.Join(stackTraces, "\n") + "\n"

s.Equal(formattedStackTraces, traces)
},
Expand Down
73 changes: 73 additions & 0 deletions log/hook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package log

import (
"github.com/sirupsen/logrus"
"github.com/spf13/cast"

"github.com/goravel/framework/contracts/log"
)

type Hook struct {
instance log.Hook
}

func (h *Hook) Levels() []logrus.Level {
levels := h.instance.Levels()
var logrusLevels []logrus.Level
for _, item := range levels {
logrusLevels = append(logrusLevels, logrus.Level(item))
}

return logrusLevels
}

func (h *Hook) Fire(entry *logrus.Entry) error {
e := &Entry{
ctx: entry.Context,
data: map[string]any(entry.Data),
level: log.Level(entry.Level),
message: entry.Message,
time: entry.Time,
}
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved

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

Check warning on line 38 in log/hook.go

View check run for this annotation

Codecov / codecov/patch

log/hook.go#L37-L38

Added lines #L37 - L38 were not covered by tests
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved

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

e.owner = root["owner"]

if request, err := cast.ToStringMapE(root["request"]); err == nil {
e.request = request
}
if response, err := cast.ToStringMapE(root["response"]); err == nil {
e.response = response
}
if stacktrace, ok := cast.ToStringMapE(root["stacktrace"]); ok == nil {
e.stacktrace = stacktrace
}
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
if tags, err := cast.ToStringSliceE(root["tags"]); err == nil {
e.tags = tags
}

e.user = root["user"]

if with, ok := cast.ToStringMapE(root["with"]); ok == nil {
e.with = with
}
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
}

return h.instance.Fire(e)
}
Loading
Loading