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

Control log print #41

Merged
merged 2 commits into from
Feb 18, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
13 changes: 6 additions & 7 deletions log/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,18 @@ type Logrus struct {
log.Writer
}

func NewApplication(writer log.Writer) log.Log {
func NewApplication(writer log.Writer) *Logrus {
return &Logrus{
Writer: writer,
}
}

func NewLogrusApplication() log.Log {
logrus := newLogrus()
func NewLogrusApplication() *Logrus {
instance := newLogrus()

return &Logrus{
instance: logrus,
Writer: NewWriter(logrus.WithContext(context.Background())),
instance: instance,
Writer: NewWriter(instance.WithContext(context.Background())),
}
}

Expand All @@ -44,8 +44,7 @@ func newLogrus() *logrus.Logger {
instance.SetLevel(logrus.DebugLevel)

if facades.Config != nil {
logging := facades.Config.GetString("logging.default")
if logging != "" {
if logging := facades.Config.GetString("logging.default"); logging != "" {
if err := registerHook(instance, logging); err != nil {
color.Redln("Init facades.Log error: " + err.Error())

Expand Down
9 changes: 9 additions & 0 deletions log/logrus_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"errors"
"io/ioutil"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -86,12 +87,20 @@ func registerHook(instance *logrus.Logger, channel string) error {

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

logLogger := &logger.Single{}
hook, err = logLogger.Handle(channelPath)
if err != nil {
return err
}
case log.DailyDriver:
if !facades.Config.GetBool(channelPath + ".print") {
instance.SetOutput(ioutil.Discard)
}

logLogger := &logger.Daily{}
hook, err = logLogger.Handle(channelPath)
if err != nil {
Expand Down
141 changes: 72 additions & 69 deletions log/logrus_writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ import (
"reflect"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

configmocks "github.com/goravel/framework/contracts/config/mocks"
"github.com/goravel/framework/facades"
"github.com/goravel/framework/support/file"
"github.com/goravel/framework/support/time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

var singleLog = "storage/logs/goravel.log"
Expand All @@ -33,7 +33,10 @@ func (s *LogrusTestSuite) SetupTest() {
}

func (s *LogrusTestSuite) TestLogrus() {
var mockConfig *configmocks.Config
var (
mockConfig *configmocks.Config
log *Logrus
)

beforeEach := func() {
mockConfig = initMockConfig()
Expand All @@ -42,30 +45,30 @@ func (s *LogrusTestSuite) TestLogrus() {
tests := []struct {
name string
setup func()
assert func(name string)
assert func()
}{
{
name: "WithContext",
setup: func() {
mockConfig.On("GetString", "logging.channels.daily.level").Return("debug").Once()
mockConfig.On("GetString", "logging.channels.single.level").Return("debug").Once()

initFacadesLog()
log = NewLogrusApplication()
},
assert: func(name string) {
writer := facades.Log.WithContext(context.Background())
assert.Equal(s.T(), reflect.TypeOf(writer).String(), reflect.TypeOf(&Writer{}).String(), name)
assert: func() {
writer := log.WithContext(context.Background())
assert.Equal(s.T(), reflect.TypeOf(writer).String(), reflect.TypeOf(&Writer{}).String())
},
},
{
name: "Debug",
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Debug("Goravel")
log = NewLogrusApplication()
log.Debug("Goravel")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.debug: Goravel"))
Expand All @@ -78,10 +81,10 @@ func (s *LogrusTestSuite) TestLogrus() {
mockConfig.On("GetString", "logging.channels.daily.level").Return("info").Once()
mockConfig.On("GetString", "logging.channels.single.level").Return("info").Once()

initFacadesLog()
facades.Log.Debug("Goravel")
log = NewLogrusApplication()
log.Debug("Goravel")
},
assert: func(name string) {
assert: func() {
assert.False(s.T(), file.Exists(dailyLog))
assert.False(s.T(), file.Exists(singleLog))
},
Expand All @@ -91,10 +94,10 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Debugf("Goravel: %s", "World")
log = NewLogrusApplication()
log.Debugf("Goravel: %s", "World")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.debug: Goravel: World"))
Expand All @@ -106,10 +109,10 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Info("Goravel")
log = NewLogrusApplication()
log.Info("Goravel")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.info: Goravel"))
Expand All @@ -121,10 +124,10 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Infof("Goravel: %s", "World")
log = NewLogrusApplication()
log.Infof("Goravel: %s", "World")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.info: Goravel: World"))
Expand All @@ -136,10 +139,10 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Warning("Goravel")
log = NewLogrusApplication()
log.Warning("Goravel")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.warning: Goravel"))
Expand All @@ -151,10 +154,10 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Warningf("Goravel: %s", "World")
log = NewLogrusApplication()
log.Warningf("Goravel: %s", "World")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.warning: Goravel: World"))
Expand All @@ -166,10 +169,10 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Error("Goravel")
log = NewLogrusApplication()
log.Error("Goravel")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.error: Goravel"))
Expand All @@ -181,10 +184,10 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
facades.Log.Errorf("Goravel: %s", "World")
log = NewLogrusApplication()
log.Errorf("Goravel: %s", "World")
},
assert: func(name string) {
assert: func() {
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
assert.True(s.T(), file.Contain(singleLog, "test.error: Goravel: World"))
Expand All @@ -196,11 +199,11 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
log = NewLogrusApplication()
},
assert: func(name string) {
assert: func() {
assert.Panics(s.T(), func() {
facades.Log.Panic("Goravel")
log.Panic("Goravel")
})
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
Expand All @@ -213,11 +216,11 @@ func (s *LogrusTestSuite) TestLogrus() {
setup: func() {
mockDriverConfig(mockConfig)

initFacadesLog()
log = NewLogrusApplication()
},
assert: func(name string) {
assert: func() {
assert.Panics(s.T(), func() {
facades.Log.Panicf("Goravel: %s", "World")
log.Panicf("Goravel: %s", "World")
})
assert.True(s.T(), file.Exists(dailyLog))
assert.True(s.T(), file.Exists(singleLog))
Expand All @@ -228,40 +231,42 @@ func (s *LogrusTestSuite) TestLogrus() {
}

for _, test := range tests {
beforeEach()
test.setup()
test.assert(test.name)
mockConfig.AssertExpectations(s.T())
file.Remove("storage")
s.Run(test.name, func() {
beforeEach()
test.setup()
test.assert()
mockConfig.AssertExpectations(s.T())
file.Remove("storage")
})
}
}

func (s *LogrusTestSuite) TestTestWriter() {
facades.Log = NewApplication(NewTestWriter())
assert.Equal(s.T(), facades.Log.WithContext(context.Background()), &TestWriter{})
log := NewApplication(NewTestWriter())
assert.Equal(s.T(), log.WithContext(context.Background()), &TestWriter{})
assert.NotPanics(s.T(), func() {
facades.Log.Debug("Goravel")
facades.Log.Debugf("Goravel")
facades.Log.Info("Goravel")
facades.Log.Infof("Goravel")
facades.Log.Warning("Goravel")
facades.Log.Warningf("Goravel")
facades.Log.Error("Goravel")
facades.Log.Errorf("Goravel")
facades.Log.Fatal("Goravel")
facades.Log.Fatalf("Goravel")
facades.Log.Panic("Goravel")
facades.Log.Panicf("Goravel")
log.Debug("Goravel")
log.Debugf("Goravel")
log.Info("Goravel")
log.Infof("Goravel")
log.Warning("Goravel")
log.Warningf("Goravel")
log.Error("Goravel")
log.Errorf("Goravel")
log.Fatal("Goravel")
log.Fatalf("Goravel")
log.Panic("Goravel")
log.Panicf("Goravel")
})
}

func TestLogrus_Fatal(t *testing.T) {
mockConfig := initMockConfig()
mockDriverConfig(mockConfig)
initFacadesLog()
log := NewLogrusApplication()

if os.Getenv("FATAL") == "1" {
facades.Log.Fatal("Goravel")
log.Fatal("Goravel")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestLogrus_Fatal")
Expand All @@ -279,10 +284,10 @@ func TestLogrus_Fatal(t *testing.T) {
func TestLogrus_Fatalf(t *testing.T) {
mockConfig := initMockConfig()
mockDriverConfig(mockConfig)
initFacadesLog()
log := NewLogrusApplication()

if os.Getenv("FATAL") == "1" {
facades.Log.Fatalf("Goravel")
log.Fatalf("Goravel")
return
}
cmd := exec.Command(os.Args[0], "-test.run=TestLogrus_Fatal")
Expand All @@ -307,8 +312,10 @@ func initMockConfig() *configmocks.Config {
mockConfig.On("GetString", "logging.channels.daily.driver").Return("daily").Once()
mockConfig.On("GetString", "logging.channels.daily.path").Return(singleLog).Once()
mockConfig.On("GetInt", "logging.channels.daily.days").Return(7).Once()
mockConfig.On("GetBool", "logging.channels.daily.print").Return(false).Once()
mockConfig.On("GetString", "logging.channels.single.driver").Return("single").Once()
mockConfig.On("GetString", "logging.channels.single.path").Return(singleLog).Once()
mockConfig.On("GetBool", "logging.channels.single.print").Return(false).Once()

return mockConfig
}
Expand All @@ -319,7 +326,3 @@ func mockDriverConfig(mockConfig *configmocks.Config) {
mockConfig.On("GetString", "app.timezone").Return("UTC")
mockConfig.On("GetString", "app.env").Return("test")
}

func initFacadesLog() {
facades.Log = NewLogrusApplication()
}