1 Star 0 Fork 0

妥協 / fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
encoder.go 2.10 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/
package fabenc
import (
"io"
"time"
zaplogfmt "github.com/sykesm/zap-logfmt"
"go.uber.org/zap/buffer"
"go.uber.org/zap/zapcore"
)
// A FormatEncoder is a zapcore.Encoder that formats log records according to a
// go-logging based format specifier.
type FormatEncoder struct {
zapcore.Encoder
formatters []Formatter
pool buffer.Pool
}
// A Formatter is used to format and write data from a zap log entry.
type Formatter interface {
Format(w io.Writer, entry zapcore.Entry, fields []zapcore.Field)
}
func NewFormatEncoder(formatters ...Formatter) *FormatEncoder {
return &FormatEncoder{
Encoder: zaplogfmt.NewEncoder(zapcore.EncoderConfig{
MessageKey: "", // disable
LevelKey: "", // disable
TimeKey: "", // disable
NameKey: "", // disable
CallerKey: "", // disable
StacktraceKey: "", // disable
LineEnding: "\n",
EncodeDuration: zapcore.StringDurationEncoder,
EncodeTime: func(t time.Time, enc zapcore.PrimitiveArrayEncoder) {
enc.AppendString(t.Format("2006-01-02T15:04:05.999Z07:00"))
},
}),
formatters: formatters,
pool: buffer.NewPool(),
}
}
// Clone creates a new instance of this encoder with the same configuration.
func (f *FormatEncoder) Clone() zapcore.Encoder {
return &FormatEncoder{
Encoder: f.Encoder.Clone(),
formatters: f.formatters,
pool: f.pool,
}
}
// EncodeEntry formats a zap log record. The structured fields are formatted by a
// zapcore.ConsoleEncoder and are appended as JSON to the end of the formatted entry.
// All entries are terminated by a newline.
func (f *FormatEncoder) EncodeEntry(entry zapcore.Entry, fields []zapcore.Field) (*buffer.Buffer, error) {
line := f.pool.Get()
for _, f := range f.formatters {
f.Format(line, entry, fields)
}
encodedFields, err := f.Encoder.EncodeEntry(entry, fields)
if err != nil {
return nil, err
}
if line.Len() > 0 && encodedFields.Len() != 1 {
line.AppendString(" ")
}
line.AppendString(encodedFields.String())
encodedFields.Free()
return line, nil
}
1
https://gitee.com/liurenhao/fabric.git
git@gitee.com:liurenhao/fabric.git
liurenhao
fabric
fabric
v2.1.1

搜索帮助