Skip to content

Commit

Permalink
fix #531: An extra log will be printed when using a custom log driver
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Dec 12, 2024
1 parent c9c91c9 commit 855e703
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 54 deletions.
24 changes: 14 additions & 10 deletions contracts/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ const (
DebugLevel
)

type Data map[string]any

type Log interface {
// WithContext adds a context to the logger.
WithContext(ctx context.Context) Writer
Expand Down Expand Up @@ -96,28 +98,30 @@ type Hook interface {
}

type Entry interface {
// Code returns the associated code.
Code() string
// Context returns the context of the entry.
Context() context.Context
// Data returns the data of the entry.
Data() Data
// Level returns the level of the entry.
Level() Level
// Time returns the timestamp of the entry.
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
// Tags returns the list of tags.
Tags() []string
// Time returns the timestamp of the entry.
Time() time.Time
// Trace returns the stack trace or trace data.
Trace() map[string]any
// User returns the user information.
User() any
// With returns additional context data.
With() map[string]any
}
12 changes: 3 additions & 9 deletions log/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,7 @@ type Application struct {
}

func NewApplication(config config.Config, json foundation.Json) (*Application, error) {
instance := logrus.New()
instance.SetLevel(logrus.DebugLevel)

instance := NewLogrus()
if config != nil {
if channel := config.GetString("logging.default"); channel != "" {
if err := registerHook(config, json, instance, channel); err != nil {
Expand All @@ -47,9 +45,7 @@ func (r *Application) Channel(channel string) log.Writer {
return r.Writer
}

instance := logrus.New()
instance.SetLevel(logrus.DebugLevel)

instance := NewLogrus()
if err := registerHook(r.config, r.json, instance, channel); err != nil {
color.Errorln(err)
return nil
Expand All @@ -63,9 +59,7 @@ func (r *Application) Stack(channels []string) log.Writer {
return r.Writer
}

instance := logrus.New()
instance.SetLevel(logrus.DebugLevel)

instance := NewLogrus()
for _, channel := range channels {
if channel == "" {
continue
Expand Down
41 changes: 23 additions & 18 deletions log/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

type Entry struct {
ctx context.Context
data log.Data
level log.Level
time time.Time
message string
Expand All @@ -22,50 +23,54 @@ type Entry struct {
stacktrace map[string]any
}

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

func (r *Entry) Context() context.Context {
return r.ctx
}

func (r *Entry) Level() log.Level {
return r.level
func (r *Entry) Data() log.Data {
return r.data

Check warning on line 35 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L34-L35

Added lines #L34 - L35 were not covered by tests
}

func (r *Entry) Time() time.Time {
return r.time
func (r *Entry) Level() log.Level {
return r.level
}

func (r *Entry) Message() string {
return r.message
}

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

Check warning on line 47 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L46-L47

Added lines #L46 - L47 were not covered by tests
}

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

Check warning on line 51 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L50-L51

Added lines #L50 - L51 were not covered by tests
}

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

Check warning on line 55 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L54-L55

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

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

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

Check warning on line 63 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L62-L63

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

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

Check warning on line 67 in log/entry.go

View check run for this annotation

Codecov / codecov/patch

log/entry.go#L66-L67

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

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

func (r *Entry) Trace() map[string]any {
return r.stacktrace
func (r *Entry) With() map[string]any {
return r.with
}
41 changes: 27 additions & 14 deletions log/logrus_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"fmt"
"io"
"os"

"github.com/rotisserie/eris"
"github.com/sirupsen/logrus"
Expand All @@ -17,6 +18,14 @@ import (
"github.com/goravel/framework/log/logger"
)

func NewLogrus() *logrus.Logger {
instance := logrus.New()
instance.SetLevel(logrus.DebugLevel)
instance.SetOutput(io.Discard)

return instance
}

type Writer struct {
code string

Expand Down Expand Up @@ -294,11 +303,14 @@ func (r *Writer) toMap() map[string]any {
}

func registerHook(config config.Config, json foundation.Json, instance *logrus.Logger, channel string) error {
channelPath := "logging.channels." + channel
driver := config.GetString(channelPath + ".driver")
var (
hook logrus.Hook
err error

channelPath = "logging.channels." + channel
driver = config.GetString(channelPath + ".driver")
)

var hook logrus.Hook
var err error
switch driver {
case log.StackDriver:
for _, stackChannel := range config.Get(channelPath + ".channels").([]string) {
Expand All @@ -313,25 +325,27 @@ func registerHook(config config.Config, json foundation.Json, instance *logrus.L

return nil
case log.SingleDriver:
if !config.GetBool(channelPath + ".print") {
instance.SetOutput(io.Discard)
}

logLogger := logger.NewSingle(config, json)
hook, err = logLogger.Handle(channelPath)
if err != nil {
return err
}
case log.DailyDriver:
if !config.GetBool(channelPath + ".print") {
instance.SetOutput(io.Discard)
}

if config.GetBool(channelPath + ".print") {
instance.SetOutput(os.Stdout)
instance.SetFormatter(formatter.NewGeneral(config, json))
}
case log.DailyDriver:
logLogger := logger.NewDaily(config, json)
hook, err = logLogger.Handle(channelPath)
if err != nil {
return err
}

if config.GetBool(channelPath + ".print") {
instance.SetOutput(os.Stdout)
instance.SetFormatter(formatter.NewGeneral(config, json))
}
case log.CustomDriver:
logLogger := config.Get(channelPath + ".via").(log.Logger)
logHook, err := logLogger.Handle(channelPath)
Expand All @@ -344,8 +358,6 @@ func registerHook(config config.Config, json foundation.Json, instance *logrus.L
return errors.LogDriverNotSupported.Args(channel)
}

instance.SetFormatter(formatter.NewGeneral(config, json))

instance.AddHook(hook)

return nil
Expand All @@ -368,6 +380,7 @@ func (h *Hook) Levels() []logrus.Level {
func (h *Hook) Fire(entry *logrus.Entry) error {
e := &Entry{
ctx: entry.Context,
data: map[string]any(entry.Data),
level: log.Level(entry.Level),
time: entry.Time,
message: entry.Message,
Expand Down
2 changes: 0 additions & 2 deletions log/logrus_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ func TestLogrus(t *testing.T) {
setup: func() {
mockConfig.On("GetString", "logging.channels.daily.level").Return("info").Once()
mockConfig.On("GetString", "logging.channels.single.level").Return("info").Once()
mockConfig.On("GetString", "app.timezone").Return("UTC").Once()
mockConfig.On("GetString", "app.env").Return("test").Once()
log, err = NewApplication(mockConfig, j)
log.Debug("No Debug Goravel")
},
Expand Down
47 changes: 47 additions & 0 deletions mocks/log/Entry.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion support/constant.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package support

const Version string = "v1.14.7"
const Version string = "v1.15.0"

const (
EnvRuntime = "runtime"
Expand Down

0 comments on commit 855e703

Please sign in to comment.