Skip to content

Commit

Permalink
add methods to entry
Browse files Browse the repository at this point in the history
  • Loading branch information
kkumar-gcc committed Sep 11, 2024
1 parent 4fd3f63 commit d4d2715
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 6 deletions.
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 @@ import (
)

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) Time() time.Time {
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
}

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

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

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

func (r *Entry) Trace() map[string]any {
return r.stacktrace
}
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 @@ import (

"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) Levels() []logrus.Level {
}

func (h *Hook) Fire(entry *logrus.Entry) error {
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
}

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
}

e.owner = root["owner"]

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

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

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

0 comments on commit d4d2715

Please sign in to comment.