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/10/06 07:55:22 UTC

[dubbo-go] branch 1.5 updated: Ftr: add fatal method for logger(1.5 branch) (#1483)

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

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


The following commit(s) were added to refs/heads/1.5 by this push:
     new 1dbb573  Ftr: add fatal method for logger(1.5 branch) (#1483)
1dbb573 is described below

commit 1dbb573c2951822ca5dee603618f5f0b138aa742
Author: 氕氘氚 <cj...@163.com>
AuthorDate: Wed Oct 6 15:55:17 2021 +0800

    Ftr: add fatal method for logger(1.5 branch) (#1483)
    
    * feat: add fatal method & union test
    
    * fix: replace xml to yml
    
    * feat: test fatal by redo test
---
 common/logger/logger.go      |  2 ++
 common/logger/logger_test.go | 34 ++++++++++++++++++++++++++++++++++
 common/logger/logging.go     | 10 ++++++++++
 3 files changed, 46 insertions(+)

diff --git a/common/logger/logger.go b/common/logger/logger.go
index 8519543..669a611 100644
--- a/common/logger/logger.go
+++ b/common/logger/logger.go
@@ -53,11 +53,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{})
 }
 
 func init() {
diff --git a/common/logger/logger_test.go b/common/logger/logger_test.go
index 6081f71..f9190dc 100644
--- a/common/logger/logger_test.go
+++ b/common/logger/logger_test.go
@@ -19,6 +19,8 @@ package logger
 
 import (
 	"fmt"
+	"os"
+	"os/exec"
 	"path/filepath"
 	"runtime"
 	"testing"
@@ -81,3 +83,35 @@ func TestSetLevel(t *testing.T) {
 	Debug("debug")
 	Info("info")
 }
+
+func TestFatal(t *testing.T) {
+	err := InitLog("./log.yml")
+	assert.NoError(t, err)
+	if os.Getenv("BE_Fatal") == "1" {
+		Fatal("fool")
+		return
+	}
+	cmd := exec.Command(os.Args[0], "-test.run=TestFatal")
+	cmd.Env = append(os.Environ(), "BE_Fatal=1")
+	err = cmd.Run()
+	if e, ok := err.(*exec.ExitError); ok && !e.Success() {
+		return
+	}
+	t.Fatalf("process ran with err %v, want exit status 1", err)
+}
+
+func TestFatalf(t *testing.T) {
+	err := InitLog("./log.yml")
+	assert.NoError(t, err)
+	if os.Getenv("BE_Fatalf") == "1" {
+		Fatalf("%s", "foolf")
+		return
+	}
+	cmd := exec.Command(os.Args[0], "-test.run=TestFatalf")
+	cmd.Env = append(os.Environ(), "BE_Fatalf=1")
+	err = cmd.Run()
+	if e, ok := err.(*exec.ExitError); ok && !e.Success() {
+		return
+	}
+	t.Fatalf("process ran with err %v, want exit status 1", err)
+}
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...)
+}