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 2025/12/21 23:39:42 UTC

(dubbo-go) branch develop updated: test(logger): add unit tests to improve coverage (#3119)

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

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


The following commit(s) were added to refs/heads/develop by this push:
     new 8e7a35b99 test(logger): add unit tests to improve coverage (#3119)
8e7a35b99 is described below

commit 8e7a35b990be5a937da0998dc62978808070003a
Author: Tsukikage <65...@users.noreply.github.com>
AuthorDate: Mon Dec 22 07:39:36 2025 +0800

    test(logger): add unit tests to improve coverage (#3119)
    
    - zap: add tests for NewDefault, encoderConfig, text/default format,
      console and file appenders, invalid level handling (74.1% -> 100%)
    - logrus: add tests for text/default format, console and file appenders,
      valid level handling (95.2% -> 100%)
    - logger: add tests for SetLoggerLevel with non-OpsLogger
      (90.6% -> 93.8%)
---
 logger/core/logrus/logrus_test.go | 53 +++++++++++++++++++++++
 logger/core/zap/zap_test.go       | 88 +++++++++++++++++++++++++++++++++++++++
 logger/logger_test.go             | 23 ++++++++++
 3 files changed, 164 insertions(+)

diff --git a/logger/core/logrus/logrus_test.go b/logger/core/logrus/logrus_test.go
index c00091d86..336869bc4 100644
--- a/logger/core/logrus/logrus_test.go
+++ b/logger/core/logrus/logrus_test.go
@@ -53,3 +53,56 @@ func TestInstantiateLogrus_FileAppenderAndJson(t *testing.T) {
 		t.Fatalf("expected logrus file logger, err=%v", err)
 	}
 }
+
+func TestInstantiateLogrus_TextFormat(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:    []string{"info"},
+		constant.LoggerAppenderKey: []string{"console"},
+		constant.LoggerFormatKey:   []string{"text"},
+	})
+	lg, err := instantiate(u)
+	if err != nil || lg == nil {
+		t.Fatalf("expected logrus logger with text format, err=%v", err)
+	}
+}
+
+func TestInstantiateLogrus_DefaultFormat(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:    []string{"info"},
+		constant.LoggerAppenderKey: []string{"console"},
+		constant.LoggerFormatKey:   []string{"unknown-format"},
+	})
+	lg, err := instantiate(u)
+	if err != nil || lg == nil {
+		t.Fatalf("expected logrus logger with default format fallback, err=%v", err)
+	}
+}
+
+func TestInstantiateLogrus_ConsoleAndFileAppender(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:       []string{"debug"},
+		constant.LoggerAppenderKey:    []string{"console,file"},
+		constant.LoggerFormatKey:      []string{"json"},
+		constant.LoggerFileNameKey:    []string{"test_combined.log"},
+		constant.LoggerFileNaxSizeKey: []string{"1"},
+	})
+	lg, err := instantiate(u)
+	if err != nil || lg == nil {
+		t.Fatalf("expected logrus logger with console and file appenders, err=%v", err)
+	}
+}
+
+func TestInstantiateLogrus_ValidLevel(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:    []string{"debug"},
+		constant.LoggerAppenderKey: []string{"console"},
+	})
+	lg, err := instantiate(u)
+	if err != nil || lg == nil {
+		t.Fatalf("expected logrus logger with valid level, err=%v", err)
+	}
+}
diff --git a/logger/core/zap/zap_test.go b/logger/core/zap/zap_test.go
index 84ded0f52..62069c6cf 100644
--- a/logger/core/zap/zap_test.go
+++ b/logger/core/zap/zap_test.go
@@ -51,3 +51,91 @@ func TestInstantiateZap_FileAppender(t *testing.T) {
 		t.Fatalf("expected zap file logger, err=%v", err)
 	}
 }
+
+func TestInstantiateZap_TextFormat(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:    []string{"info"},
+		constant.LoggerAppenderKey: []string{"console"},
+		constant.LoggerFormatKey:   []string{"text"},
+	})
+	lg, err := instantiate(u)
+	if err != nil || lg == nil {
+		t.Fatalf("expected zap logger with text format, err=%v", err)
+	}
+}
+
+func TestInstantiateZap_DefaultFormat(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:    []string{"info"},
+		constant.LoggerAppenderKey: []string{"console"},
+		constant.LoggerFormatKey:   []string{"unknown-format"},
+	})
+	lg, err := instantiate(u)
+	if err != nil || lg == nil {
+		t.Fatalf("expected zap logger with default format fallback, err=%v", err)
+	}
+}
+
+func TestInstantiateZap_ConsoleAndFileAppender(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:       []string{"debug"},
+		constant.LoggerAppenderKey:    []string{"console,file"},
+		constant.LoggerFormatKey:      []string{"json"},
+		constant.LoggerFileNameKey:    []string{"test_combined.log"},
+		constant.LoggerFileNaxSizeKey: []string{"1"},
+	})
+	lg, err := instantiate(u)
+	if err != nil || lg == nil {
+		t.Fatalf("expected zap logger with console and file appenders, err=%v", err)
+	}
+}
+
+func TestInstantiateZap_InvalidLevel(t *testing.T) {
+	u := &common.URL{}
+	u.ReplaceParams(url.Values{
+		constant.LoggerLevelKey:    []string{"not-a-valid-level"},
+		constant.LoggerAppenderKey: []string{"console"},
+	})
+	lg, err := instantiate(u)
+	if err == nil {
+		t.Fatalf("expected error for invalid level, got nil")
+	}
+	if lg != nil {
+		t.Fatalf("expected nil logger for invalid level, got %v", lg)
+	}
+}
+
+func TestNewDefault(t *testing.T) {
+	lg := NewDefault()
+	if lg == nil {
+		t.Fatalf("expected non-nil default logger")
+	}
+	if lg.Logger == nil {
+		t.Fatalf("expected non-nil underlying logger")
+	}
+}
+
+func TestEncoderConfig(t *testing.T) {
+	ec := encoderConfig()
+	if ec.MessageKey != "msg" {
+		t.Fatalf("expected MessageKey 'msg', got %q", ec.MessageKey)
+	}
+	if ec.LevelKey != "level" {
+		t.Fatalf("expected LevelKey 'level', got %q", ec.LevelKey)
+	}
+	if ec.TimeKey != "time" {
+		t.Fatalf("expected TimeKey 'time', got %q", ec.TimeKey)
+	}
+	if ec.CallerKey != "line" {
+		t.Fatalf("expected CallerKey 'line', got %q", ec.CallerKey)
+	}
+	if ec.NameKey != "logger" {
+		t.Fatalf("expected NameKey 'logger', got %q", ec.NameKey)
+	}
+	if ec.StacktraceKey != "stacktrace" {
+		t.Fatalf("expected StacktraceKey 'stacktrace', got %q", ec.StacktraceKey)
+	}
+}
diff --git a/logger/logger_test.go b/logger/logger_test.go
index eb5b44230..a5e7c0653 100644
--- a/logger/logger_test.go
+++ b/logger/logger_test.go
@@ -58,3 +58,26 @@ func TestSetLoggerLevel(t *testing.T) {
 		t.Fatalf("expected level 'warn', got %q", m.level)
 	}
 }
+
+// simpleLogger implements Logger but NOT OpsLogger
+type simpleLogger struct{}
+
+func (s *simpleLogger) Debug(args ...any)                   {}
+func (s *simpleLogger) Debugf(template string, args ...any) {}
+func (s *simpleLogger) Info(args ...any)                    {}
+func (s *simpleLogger) Infof(template string, args ...any)  {}
+func (s *simpleLogger) Warn(args ...any)                    {}
+func (s *simpleLogger) Warnf(template string, args ...any)  {}
+func (s *simpleLogger) Error(args ...any)                   {}
+func (s *simpleLogger) Errorf(template string, args ...any) {}
+func (s *simpleLogger) Fatal(args ...any)                   {}
+func (s *simpleLogger) Fatalf(fmt string, args ...any)      {}
+
+func TestSetLoggerLevel_NotOpsLogger(t *testing.T) {
+	s := &simpleLogger{}
+	SetLogger(s)
+	ok := SetLoggerLevel("debug")
+	if ok {
+		t.Fatalf("expected SetLoggerLevel to return false for non-OpsLogger")
+	}
+}