feat: add json log field

This commit is contained in:
franklin 2022-12-19 15:39:58 +01:00
parent dbafcc1fa3
commit 4bc776267e

View File

@ -1,28 +1,44 @@
package log
import (
"encoding/json"
"fmt"
"go.uber.org/zap"
)
const (
// NumKey - generic number attribute
NumKey = "num"
// NameKey - generic name attribute
NameKey = "name"
// ValueKey represents a generic value attribute.
// ValueKey - generic value attribute
ValueKey = "value"
// JSONKey - generic json attribute
JSONKey = "json"
)
// FNum - returns zap field
func FNum(num int) zap.Field {
return zap.Int(NumKey, num)
}
// FName - returns zap field
func FName(name string) zap.Field {
return zap.String(NameKey, name)
}
// FValue - returns zap field
func FValue(value interface{}) zap.Field {
return zap.String(ValueKey, fmt.Sprintf("%v", value))
}
// FJSON - returns zap field
func FJSON(v interface{}) zap.Field {
if out, err := json.Marshal(v); err != nil {
return zap.String(JSONKey+"_error", err.Error())
} else {
raw := json.RawMessage(out)
return zap.Any(JSONKey, &raw)
}
}