From 16d3e04fd9ab56c47bbe231eb7b234a659d47d0b Mon Sep 17 00:00:00 2001 From: andrey Date: Thu, 4 Apr 2024 12:04:45 +0300 Subject: [PATCH] use dependency format log --- writer_example_test.go | 2 +- writter.go | 21 ++++++++++----------- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/writer_example_test.go b/writer_example_test.go index dfe6672..dc2355f 100644 --- a/writer_example_test.go +++ b/writer_example_test.go @@ -20,7 +20,7 @@ func exampleWithTime(key, format string) log.Middleware { func ExampleFormatWithBracket() { ctx := context.Background() - logger := log.New(log.WithFormat(log.FormatWithBracket()), log.WithStdout()).With( + logger := log.New(log.WithFormat(log.FormatWithBracket(field.NewEncoderText())), log.WithStdout()).With( log.WithSource(10, filepath.Base), // log.WithTime(log.KeyTime, time.RFC3339), exampleWithTime(log.KeyTime, time.RFC3339), diff --git a/writter.go b/writter.go index 7db5f7b..6f17440 100644 --- a/writter.go +++ b/writter.go @@ -43,12 +43,12 @@ func WithStdout() func(*option) { // WithStringFormat sets format as simple string. func WithStringFormat() func(*option) { - return WithFormat(formatString()) + return WithFormat(FormatString(field.NewEncoderText())) } // WithJSONFormat sets json output format. func WithJSONFormat() func(*option) { - return WithFormat(formatJSON()) + return WithFormat(FormatJSON(field.NewEncoderJSON())) } // WithFormat sets custom output format. @@ -66,7 +66,7 @@ type option struct { // New creates standart logger. func New(opts ...func(*option)) Logger { log := option{ - format: formatString(), + format: FormatString(field.NewEncoderText()), out: os.Stderr, } @@ -79,9 +79,12 @@ func New(opts ...func(*option)) Logger { } } -func FormatWithBracket() func(io.Writer, *entry.Entry) (int, error) { - enc := field.NewEncoderText() +type Encoder interface { + AppendValue(dst []byte, val field.Value) []byte + AppendField(dst []byte, val field.Field) []byte +} +func FormatWithBracket(enc Encoder) func(io.Writer, *entry.Entry) (int, error) { appendValue := func(buf *buffer.Buffer, data field.Fields, key, prefix, suffix string) *buffer.Buffer { data.Fields( func(f field.Field) bool { @@ -133,9 +136,7 @@ func FormatWithBracket() func(io.Writer, *entry.Entry) (int, error) { } } -func formatString() func(io.Writer, *entry.Entry) (int, error) { - enc := field.NewEncoderText() - +func FormatString(enc Encoder) func(io.Writer, *entry.Entry) (int, error) { return func(w io.Writer, entry *entry.Entry) (int, error) { buf := buffer.New() defer func() { @@ -159,9 +160,7 @@ func formatString() func(io.Writer, *entry.Entry) (int, error) { } } -func formatJSON() func(w io.Writer, entry *entry.Entry) (int, error) { - enc := field.NewEncoderJSON() - +func FormatJSON(enc Encoder) func(w io.Writer, entry *entry.Entry) (int, error) { return func(w io.Writer, entry *entry.Entry) (int, error) { buf := buffer.New() defer func() { -- 2.30.2