Skip to content

Commit

Permalink
feat: [#282] Print the values in ctx when calling facades.Log().WithC…
Browse files Browse the repository at this point in the history
…ontext(ctx).Info() (#759)

* feat: [#282] Print the values in ctx when calling facades.Log().WithContext(ctx).Info()

* optimmize tests

* fix http.Request

* optimize first letter

* optimize lint
  • Loading branch information
hwbrzzl authored Dec 17, 2024
1 parent 497e9c7 commit 9dddb62
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 214 deletions.
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 @@ import (

"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 NewApplication(config config.Config, json foundation.Json) (*Application, e
}

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

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 @@ import (
)

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 @@ func (r *Entry) Data() log.Data {
return r.data
}

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

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

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)
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,
}

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

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, err := cast.ToStringMapE(root["stacktrace"]); err == nil {
e.stacktrace = stacktrace
}
if tags, err := cast.ToStringSliceE(root["tags"]); err == nil {
e.tags = tags
}

e.user = root["user"]

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

return h.instance.Fire(e)
}
Loading

0 comments on commit 9dddb62

Please sign in to comment.