2019-07-30 18:39:16 +00:00
|
|
|
package zaputil
|
2019-07-17 22:25:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
2024-08-23 19:30:58 +00:00
|
|
|
"sync"
|
2019-07-17 22:25:42 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type jsonHexEncoder struct {
|
|
|
|
zapcore.Encoder
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewJSONHexEncoder creates a JSON logger based on zapcore.NewJSONEncoder
|
|
|
|
// but overwrites encoding of byte slices. Instead encoding them with base64,
|
|
|
|
// jsonHexEncoder uses hex-encoding.
|
|
|
|
// Each hex-encoded value is prefixed with 0x so that it's clear it's a hex string.
|
|
|
|
func NewJSONHexEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
|
|
|
|
jsonEncoder := zapcore.NewJSONEncoder(cfg)
|
|
|
|
return &jsonHexEncoder{
|
|
|
|
Encoder: jsonEncoder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (enc *jsonHexEncoder) AddBinary(key string, val []byte) {
|
|
|
|
enc.AddString(key, "0x"+hex.EncodeToString(val))
|
|
|
|
}
|
|
|
|
|
2019-07-30 18:39:16 +00:00
|
|
|
func (enc *jsonHexEncoder) Clone() zapcore.Encoder {
|
|
|
|
encoderClone := enc.Encoder.Clone()
|
|
|
|
return &jsonHexEncoder{Encoder: encoderClone}
|
|
|
|
}
|
|
|
|
|
2024-08-23 19:30:58 +00:00
|
|
|
var (
|
|
|
|
registerJSONHexEncoderOnce sync.Once
|
|
|
|
registerConsoleHexEncodeOnce sync.Once
|
|
|
|
)
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
// RegisterJSONHexEncoder registers a jsonHexEncoder under "json-hex" name.
|
|
|
|
// Later, this name can be used as a value for zap.Config.Encoding to enable
|
|
|
|
// jsonHexEncoder.
|
|
|
|
func RegisterJSONHexEncoder() error {
|
2024-08-23 19:30:58 +00:00
|
|
|
var err error
|
|
|
|
registerJSONHexEncoderOnce.Do(func() {
|
|
|
|
err = zap.RegisterEncoder("json-hex", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
|
|
|
|
return NewJSONHexEncoder(cfg), nil
|
|
|
|
})
|
2019-07-17 22:25:42 +00:00
|
|
|
})
|
2024-08-23 19:30:58 +00:00
|
|
|
return err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
2019-07-30 18:39:16 +00:00
|
|
|
|
|
|
|
type consoleHexEncoder struct {
|
|
|
|
zapcore.Encoder
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewConsoleHexEncoder(cfg zapcore.EncoderConfig) zapcore.Encoder {
|
|
|
|
consoleEncoder := zapcore.NewConsoleEncoder(cfg)
|
|
|
|
return &consoleHexEncoder{
|
|
|
|
Encoder: consoleEncoder,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (enc *consoleHexEncoder) AddBinary(key string, val []byte) {
|
|
|
|
enc.AddString(key, "0x"+hex.EncodeToString(val))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (enc *consoleHexEncoder) Clone() zapcore.Encoder {
|
|
|
|
encoderClone := enc.Encoder.Clone()
|
|
|
|
return &consoleHexEncoder{Encoder: encoderClone}
|
|
|
|
}
|
|
|
|
|
|
|
|
func RegisterConsoleHexEncoder() error {
|
2024-08-23 19:30:58 +00:00
|
|
|
var err error
|
|
|
|
registerConsoleHexEncodeOnce.Do(func() {
|
|
|
|
err = zap.RegisterEncoder("console-hex", func(cfg zapcore.EncoderConfig) (zapcore.Encoder, error) {
|
|
|
|
return NewConsoleHexEncoder(cfg), nil
|
|
|
|
})
|
2019-07-30 18:39:16 +00:00
|
|
|
})
|
2024-08-23 19:30:58 +00:00
|
|
|
return err
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|