You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by al...@apache.org on 2021/09/26 05:02:28 UTC

[dubbo-go] branch config-enhance updated: Ftr: add fatal method for logger(config-enhance branch) (#1482)

This is an automated email from the ASF dual-hosted git repository.

alexstocks pushed a commit to branch config-enhance
in repository https://gitbox.apache.org/repos/asf/dubbo-go.git


The following commit(s) were added to refs/heads/config-enhance by this push:
     new 26aa12b  Ftr: add fatal method for logger(config-enhance branch) (#1482)
26aa12b is described below

commit 26aa12b8b070927bd09a70f81c6f909218419652
Author: 氕氘氚 <cj...@163.com>
AuthorDate: Sun Sep 26 13:02:24 2021 +0800

    Ftr: add fatal method for logger(config-enhance branch) (#1482)
    
    * feat: add fatal method for logger
    
    * test: add test for logger fatal & fatalf
---
 common/logger/logger.go      |  2 ++
 common/logger/logging.go     | 10 ++++++++++
 config/logger_config_test.go | 14 ++++++++++++++
 go.mod                       |  1 +
 go.sum                       |  2 ++
 5 files changed, 29 insertions(+)

diff --git a/common/logger/logger.go b/common/logger/logger.go
index e5c999f..6d4f207 100644
--- a/common/logger/logger.go
+++ b/common/logger/logger.go
@@ -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
diff --git a/common/logger/logging.go b/common/logger/logging.go
index 7a31ece..d114423 100644
--- a/common/logger/logging.go
+++ b/common/logger/logging.go
@@ -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...)
+}
diff --git a/config/logger_config_test.go b/config/logger_config_test.go
index 94e464f..8be1854 100644
--- a/config/logger_config_test.go
+++ b/config/logger_config_test.go
@@ -18,10 +18,12 @@
 package config
 
 import (
+	"os"
 	"testing"
 )
 
 import (
+	"bou.ke/monkey"
 	"github.com/stretchr/testify/assert"
 )
 
@@ -29,6 +31,10 @@ 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"))
@@ -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) {
diff --git a/go.mod b/go.mod
index 8ba6c83..d41d5c5 100644
--- a/go.mod
+++ b/go.mod
@@ -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
diff --git a/go.sum b/go.sum
index 5519d42..e479db5 100644
--- a/go.sum
+++ b/go.sum
@@ -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=