From 92e8e38bb8b06ea4987a8b34ac3c30f28927aa83 Mon Sep 17 00:00:00 2001 From: dmitriyselivanov Date: Wed, 2 Feb 2022 13:24:37 +0300 Subject: [PATCH] minor refactor (#10) --- logger/logger.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/logger/logger.go b/logger/logger.go index 14da8e4..6c4d851 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -128,14 +128,12 @@ func (z *Zaplog) PanicErr(e error) *Zaplog { return z } -func (z *Zaplog) WithCropIfNeeded(key string, value interface{}) *Zaplog { +// AnyCropped takes a key and an arbitrary value and chooses the best way to represent them as a field. +// It crops the content if it is larger than 200 characters. +func AnyCropped(key string, value interface{}) zap.Field { valueJson, err := json.Marshal(value) - if err != nil { - return z + if err != nil || len(valueJson) <= MaxAttributeChars { + return zap.Any(key, value) } - - if len(valueJson) <= MaxAttributeChars { - return z.With(zap.Any(key, value)) - } - return z.With(zap.ByteString(key, valueJson[0:MaxAttributeChars])) + return zap.ByteString(key, valueJson[0:MaxAttributeChars]) }