Skip to content

Commit

Permalink
fix: [#531] An extra log will be printed when using a custom log driv…
Browse files Browse the repository at this point in the history
…er (#753)

* fix: [#531] An extra log will be printed when using a custom log driver

* chore: update mocks

* update version

* fix test

---------

Co-authored-by: hwbrzzl <[email protected]>
  • Loading branch information
hwbrzzl and hwbrzzl authored Dec 12, 2024
1 parent 20c6d6d commit 7bf551b
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 26 deletions.
4 changes: 4 additions & 0 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 @@ -98,6 +100,8 @@ type Hook interface {
type Entry interface {
// 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.
Expand Down
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 {
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 @@ -48,9 +46,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.Red().Println("Init facades.Log error: " + err.Error())
return nil
Expand All @@ -64,9 +60,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
5 changes: 5 additions & 0 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 @@ -18,6 +19,10 @@ func (r *Entry) Context() context.Context {
return r.ctx
}

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

func (r *Entry) Level() log.Level {
return r.level
}
Expand Down
41 changes: 27 additions & 14 deletions log/logrus_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"os"

"github.com/rotisserie/eris"
"github.com/sirupsen/logrus"
Expand All @@ -16,6 +17,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 @@ -293,11 +302,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 @@ -312,25 +324,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 @@ -343,8 +357,6 @@ func registerHook(config config.Config, json foundation.Json, instance *logrus.L
return errors.New("Error logging channel: " + channel)
}

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

instance.AddHook(hook)

return nil
Expand All @@ -367,6 +379,7 @@ func (h *Hook) Levels() []logrus.Level {
func (h *Hook) Fire(entry *logrus.Entry) error {
return h.instance.Fire(&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 @@ -72,8 +72,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 = 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.8"
const Version string = "v1.14.9"

const (
EnvRuntime = "runtime"
Expand Down

0 comments on commit 7bf551b

Please sign in to comment.