-
-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(log): Helper implemented io.Writer (#1927)
* feat(log): Add writerWrapper implementing io.Writer * fix(kratos): fix format has wrong type * fix(log): rename writerOption to WriterOptionFn.
- Loading branch information
Showing
3 changed files
with
96 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package log | ||
|
||
import "io" | ||
|
||
type writerWrapper struct { | ||
helper *Helper | ||
level Level | ||
} | ||
|
||
type WriterOptionFn func(w *writerWrapper) | ||
|
||
// WithWriteLevel set writerWrapper level. | ||
func WithWriterLevel(level Level) WriterOptionFn { | ||
return func(w *writerWrapper) { | ||
w.level = level | ||
} | ||
} | ||
|
||
// WithWriteMessageKey set writerWrapper helper message key. | ||
func WithWriteMessageKey(key string) WriterOptionFn { | ||
return func(w *writerWrapper) { | ||
if key != "" { | ||
w.helper.msgKey = key | ||
} | ||
} | ||
} | ||
|
||
// NewWriter return a writer wrapper. | ||
func NewWriter(logger Logger, opts ...WriterOptionFn) io.Writer { | ||
ww := &writerWrapper{ | ||
helper: NewHelper(logger, WithMessageKey(DefaultMessageKey)), | ||
level: LevelInfo, // default level | ||
} | ||
for _, opt := range opts { | ||
opt(ww) | ||
} | ||
return ww | ||
} | ||
|
||
func (ww *writerWrapper) Write(p []byte) (int, error) { | ||
ww.helper.Log(ww.level, ww.helper.msgKey, string(p)) | ||
return 0, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package log | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func TestWriterWrapper(t *testing.T) { | ||
var buf bytes.Buffer | ||
logger := NewStdLogger(&buf) | ||
content := "ThisIsSomeTestLogMessage" | ||
testCases := []struct { | ||
w io.Writer | ||
acceptLevel Level | ||
acceptMessageKey string | ||
}{ | ||
{ | ||
w: NewWriter(logger), | ||
acceptLevel: LevelInfo, // default level | ||
acceptMessageKey: DefaultMessageKey, | ||
}, | ||
{ | ||
w: NewWriter(logger, WithWriterLevel(LevelDebug)), | ||
acceptLevel: LevelDebug, | ||
acceptMessageKey: DefaultMessageKey, | ||
}, | ||
{ | ||
w: NewWriter(logger, WithWriteMessageKey("XxXxX")), | ||
acceptLevel: LevelInfo, // default level | ||
acceptMessageKey: "XxXxX", | ||
}, | ||
{ | ||
w: NewWriter(logger, WithWriterLevel(LevelError), WithWriteMessageKey("XxXxX")), | ||
acceptLevel: LevelError, | ||
acceptMessageKey: "XxXxX", | ||
}, | ||
} | ||
for _, tc := range testCases { | ||
_, err := tc.w.Write([]byte(content)) | ||
if err != nil { | ||
t.Fatalf("unexpected error: %v", err) | ||
} | ||
if !strings.Contains(buf.String(), tc.acceptLevel.String()) { | ||
t.Errorf("expected level: %s, got: %s", tc.acceptLevel, buf.String()) | ||
} | ||
if !strings.Contains(buf.String(), tc.acceptMessageKey) { | ||
t.Errorf("expected message key: %s, got: %s", tc.acceptMessageKey, buf.String()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters