64 Star 187 Fork 3

Gitee 极速下载/hyperledger-fabric

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/hyperledger/fabric
Clone or Download
encoder.go 2.10 KB
Copy Edit Raw Blame History
Matthew Sykes authored 2018-12-13 05:40 +08:00 . [FAB-12942] use logfmt format for log fields
/*
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/mirrors/hyperledger-fabric.git
git@gitee.com:mirrors/hyperledger-fabric.git
mirrors
hyperledger-fabric
hyperledger-fabric
v2.1.1

Search