Skip to content

Commit

Permalink
Ftr: add fatal method for logger(config-enhance branch) (#1482)
Browse files Browse the repository at this point in the history
* feat: add fatal method for logger

* test: add test for logger fatal & fatalf
  • Loading branch information
cjphaha authored Sep 26, 2021
1 parent 2072171 commit 26aa12b
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions common/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ type Logger interface {
Warn(args ...interface{})
Error(args ...interface{})
Debug(args ...interface{})
Fatal(args ...interface{})

Infof(fmt string, args ...interface{})
Warnf(fmt string, args ...interface{})
Errorf(fmt string, args ...interface{})
Debugf(fmt string, args ...interface{})
Fatalf(fmt string, args ...interface{})
}

// InitLogger use for init logger by @conf
Expand Down
10 changes: 10 additions & 0 deletions common/logger/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,13 @@ func Errorf(fmt string, args ...interface{}) {
func Debugf(fmt string, args ...interface{}) {
logger.Debugf(fmt, args...)
}

// Fatal logs a message, then calls os.Exit.
func Fatal(args ...interface{}) {
logger.Fatal(args...)
}

// Fatalf logs a templated message, then calls os.Exit.
func Fatalf(fmt string, args ...interface{}) {
logger.Fatalf(fmt, args...)
}
14 changes: 14 additions & 0 deletions config/logger_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@
package config

import (
"os"
"testing"
)

import (
"bou.ke/monkey"
"github.com/stretchr/testify/assert"
)

import (
"dubbo.apache.org/dubbo-go/v3/common/logger"
)

func fakeExit(int) {
panic("os.Exit called")
}

func TestLoggerInit(t *testing.T) {
t.Run("empty use default", func(t *testing.T) {
err := Load(WithPath("./testdata/config/logger/empty_log.yaml"))
Expand All @@ -38,6 +44,14 @@ func TestLoggerInit(t *testing.T) {
assert.NotNil(t, loggerConfig)
assert.Equal(t, []string{"stderr"}, loggerConfig.ZapConfig.OutputPaths)
logger.Info("hello")
patch := monkey.Patch(os.Exit, fakeExit)
defer patch.Unpatch()
assert.PanicsWithValue(t, "os.Exit called", func() {
logger.Fatalf("%s", "error")
}, "os.Exit was not called")
assert.PanicsWithValue(t, "os.Exit called", func() {
logger.Fatal("error")
}, "os.Exit was not called")
})

t.Run("use config", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module dubbo.apache.org/dubbo-go/v3
go 1.15

require (
bou.ke/monkey v1.0.2
github.com/RoaringBitmap/roaring v0.7.1
github.com/Workiva/go-datastructures v1.0.52
github.com/afex/hystrix-go v0.0.0-20180502004556-fa1af6a1f4f5
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
bazil.org/fuse v0.0.0-20160811212531-371fbbdaa898/go.mod h1:Xbm+BRKSBEpa4q4hTSxohYNQpsxXPbPry4JJWOB3LB8=
bou.ke/monkey v1.0.2 h1:kWcnsrCNUatbxncxR/ThdYqbytgOIArtYWqcQLQzKLI=
bou.ke/monkey v1.0.2/go.mod h1:OqickVX3tNx6t33n1xvtTtu85YN5s6cKwVug+oHMaIA=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.38.0/go.mod h1:990N+gfupTy94rShfmMCWGDn0LpTmnzTp2qbd1dvSRU=
Expand Down

0 comments on commit 26aa12b

Please sign in to comment.