From f8b97f675b32dfad02edae12d83053c720720b5b Mon Sep 17 00:00:00 2001 From: DCjanus Date: Tue, 5 Nov 2024 15:24:21 +0800 Subject: [PATCH] feat(log): add Logger() method for Helper (#3443) * feat(log): add Logger() method for Helper * chore(log): add test case for Helper.Logger() --- log/helper.go | 5 +++++ log/helper_test.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/log/helper.go b/log/helper.go index 19fd613bc89..7eb5b25de96 100644 --- a/log/helper.go +++ b/log/helper.go @@ -75,6 +75,11 @@ func (h *Helper) Enabled(level Level) bool { return true } +// Logger returns logger in the helper. +func (h *Helper) Logger() Logger { + return h.logger +} + // Log Print log by level and keyvals. func (h *Helper) Log(level Level, keyvals ...interface{}) { _ = h.logger.Log(level, keyvals...) diff --git a/log/helper_test.go b/log/helper_test.go index 750710c2d81..c056fd88a0e 100644 --- a/log/helper_test.go +++ b/log/helper_test.go @@ -8,7 +8,12 @@ import ( ) func TestHelper(_ *testing.T) { - logger := With(DefaultLogger, "ts", DefaultTimestamp, "caller", DefaultCaller) + logger := With( + DefaultLogger, + "ts", DefaultTimestamp, + "caller", DefaultCaller, + "module", "test", + ) log := NewHelper(logger) log.Log(LevelDebug, "msg", "test debug") @@ -19,6 +24,12 @@ func TestHelper(_ *testing.T) { log.Warn("test warn") log.Warnf("test %s", "warn") log.Warnw("log", "test warn") + + subLogger := With(log.Logger(), + "module", "sub", + ) + subLog := NewHelper(subLogger) + subLog.Infof("sub logger test with level %s", "info") } func TestHelperWithMsgKey(_ *testing.T) {