mirror of
https://github.com/foomo/keel.git
synced 2025-10-16 12:35:34 +00:00
feat: add json log field
This commit is contained in:
parent
dbafcc1fa3
commit
4bc776267e
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user