From c8e1126b75bda77562883a59103e2cbd73198826 Mon Sep 17 00:00:00 2001 From: thinkgos Date: Wed, 17 Apr 2024 21:41:12 +0800 Subject: [PATCH] fix: fix the code --- benchmark_json_test.go | 54 +++++++++++++++++++----------------------- benchmark_text_test.go | 54 +++++++++++++++++++----------------------- logger.go | 1 + 3 files changed, 49 insertions(+), 60 deletions(-) diff --git a/benchmark_json_test.go b/benchmark_json_test.go index bf37bc6..4de357b 100644 --- a/benchmark_json_test.go +++ b/benchmark_json_test.go @@ -62,14 +62,13 @@ func Benchmark_Json_Logger(b *testing.B) { ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { - logger. - InfoxContext( - ctx, - "success", - log.String("name", "jack"), - log.Int("age", 18), - dfltCtx(ctx), - ) + logger.InfoxContext( + ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + dfltCtx(ctx), + ) } } @@ -81,13 +80,12 @@ func Benchmark_Json_Logger_Use_Hook(b *testing.B) { ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { - logger. - InfoxContext( - ctx, - "success", - log.String("name", "jack"), - log.Int("age", 18), - ) + logger.InfoxContext( + ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + ) } } @@ -111,7 +109,7 @@ func Benchmark_Json_NativeSugar(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePair(b *testing.B) { +func Benchmark_Json_KeyValuePair(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -127,7 +125,7 @@ func Benchmark_Json_SugarKeyValuePair(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePairFields(b *testing.B) { +func Benchmark_Json_KeyValuePairFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -143,7 +141,7 @@ func Benchmark_Json_SugarKeyValuePairFields(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePairFields_Use_Hook(b *testing.B) { +func Benchmark_Json_KeyValuePairFields_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -159,7 +157,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_Hook(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields(b *testing.B) { +func Benchmark_Json_KeyValuePairFields_Use_WithFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -174,7 +172,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) { +func Benchmark_Json_KeyValuePairFields_Use_WithFields_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -189,7 +187,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) { +func Benchmark_Json_KeyValuePairFields_Use_WithValuer(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -204,7 +202,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) { } } -func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { +func Benchmark_Json_KeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -219,7 +217,7 @@ func Benchmark_Json_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { } } -func Benchmark_Json_SugarFormat(b *testing.B) { +func Benchmark_Json_Format(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -234,13 +232,11 @@ func Benchmark_Json_SugarFormat(b *testing.B) { return log.Int("age", 18) }, dfltCtx, - ).InfofContext(ctx, - "success", - ) + ).InfofContext(ctx, "success") } } -func Benchmark_Json_SugarFormat_Use_Hook(b *testing.B) { +func Benchmark_Json_Format_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatJson) @@ -251,8 +247,6 @@ func Benchmark_Json_SugarFormat_Use_Hook(b *testing.B) { logger.WithValuer( log.ImmutString("name", "jack"), log.ImmutInt("age", 18), - ).InfofContext(ctx, - "success", - ) + ).InfofContext(ctx, "success") } } diff --git a/benchmark_text_test.go b/benchmark_text_test.go index adaae28..3cf4c25 100644 --- a/benchmark_text_test.go +++ b/benchmark_text_test.go @@ -37,14 +37,13 @@ func Benchmark_Text_Logger(b *testing.B) { ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { - logger. - InfoxContext( - ctx, - "success", - log.String("name", "jack"), - log.Int("age", 18), - dfltCtx(ctx), - ) + logger.InfoxContext( + ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + dfltCtx(ctx), + ) } } @@ -56,13 +55,12 @@ func Benchmark_Text_Logger_Use_Hook(b *testing.B) { ctx := context.Background() b.StartTimer() for i := 0; i < b.N; i++ { - logger. - InfoxContext( - ctx, - "success", - log.String("name", "jack"), - log.Int("age", 18), - ) + logger.InfoxContext( + ctx, + "success", + log.String("name", "jack"), + log.Int("age", 18), + ) } } @@ -86,7 +84,7 @@ func Benchmark_Text_NativeSugar(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePair(b *testing.B) { +func Benchmark_Text_KeyValuePair(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -102,7 +100,7 @@ func Benchmark_Text_SugarKeyValuePair(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePairFields(b *testing.B) { +func Benchmark_Text_KeyValuePairFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -118,7 +116,7 @@ func Benchmark_Text_SugarKeyValuePairFields(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePairFields_Use_Hook(b *testing.B) { +func Benchmark_Text_KeyValuePairFields_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -134,7 +132,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_Hook(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields(b *testing.B) { +func Benchmark_Text_KeyValuePairFields_Use_WithFields(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -149,7 +147,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) { +func Benchmark_Text_KeyValuePairFields_Use_WithFields_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -164,7 +162,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithFields_Hook(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) { +func Benchmark_Text_KeyValuePairFields_Use_WithValuer(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -179,7 +177,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer(b *testing.B) { } } -func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { +func Benchmark_Text_KeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -194,7 +192,7 @@ func Benchmark_Text_SugarKeyValuePairFields_Use_WithValuer_Hook(b *testing.B) { } } -func Benchmark_Text_SugarFormat(b *testing.B) { +func Benchmark_Text_Format(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -209,13 +207,11 @@ func Benchmark_Text_SugarFormat(b *testing.B) { return log.Int("age", 18) }, dfltCtx, - ).InfofContext(ctx, - "success", - ) + ).InfofContext(ctx, "success") } } -func Benchmark_Text_SugarFormat_Use_Hook(b *testing.B) { +func Benchmark_Text_Format_Use_Hook(b *testing.B) { b.ReportAllocs() b.StopTimer() logger := newDiscardLogger(log.FormatConsole) @@ -226,8 +222,6 @@ func Benchmark_Text_SugarFormat_Use_Hook(b *testing.B) { logger.WithValuer( log.ImmutString("name", "jack"), log.ImmutInt("age", 18), - ).InfofContext(ctx, - "success", - ) + ).InfofContext(ctx, "success") } } diff --git a/logger.go b/logger.go index 5298034..3927f38 100644 --- a/logger.go +++ b/logger.go @@ -507,6 +507,7 @@ func formatMessage(template string, fmtArgs []any) string { return fmt.Sprint(fmtArgs...) } +// copy from zap(sugar.go) func (l *Log) appendSweetenFields(fields []Field, args []any) []Field { if len(args) == 0 { return nil