116 lines
3.0 KiB
Go
116 lines
3.0 KiB
Go
|
package logutils
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"github.com/ethereum/go-ethereum/log"
|
||
|
"go.uber.org/zap/zapcore"
|
||
|
"math"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
type gethLoggerCore struct {
|
||
|
zapcore.LevelEnabler
|
||
|
fields []zapcore.Field
|
||
|
logger log.Logger
|
||
|
}
|
||
|
|
||
|
func (c gethLoggerCore) With(fields []zapcore.Field) zapcore.Core {
|
||
|
clone := c.clone()
|
||
|
clone.fields = append(clone.fields, fields...)
|
||
|
return clone
|
||
|
}
|
||
|
|
||
|
func (c gethLoggerCore) Check(ent zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
|
||
|
if c.Enabled(ent.Level) {
|
||
|
return ce.AddCore(ent, c)
|
||
|
}
|
||
|
return ce
|
||
|
}
|
||
|
func (c gethLoggerCore) Write(ent zapcore.Entry, fields []zapcore.Field) error { // nolint: gocyclo
|
||
|
fields = append(c.fields[:], fields...)
|
||
|
|
||
|
var args []interface{}
|
||
|
for _, f := range fields {
|
||
|
switch f.Type {
|
||
|
case zapcore.ArrayMarshalerType,
|
||
|
zapcore.ObjectMarshalerType,
|
||
|
zapcore.BinaryType,
|
||
|
zapcore.ByteStringType,
|
||
|
zapcore.Complex128Type,
|
||
|
zapcore.ReflectType,
|
||
|
zapcore.StringerType,
|
||
|
zapcore.ErrorType:
|
||
|
args = append(args, f.Key, f.Interface)
|
||
|
case zapcore.BoolType:
|
||
|
args = append(args, f.Key, f.Integer == 1)
|
||
|
case zapcore.DurationType:
|
||
|
args = append(args, f.Key, time.Duration(f.Integer))
|
||
|
case zapcore.Float64Type:
|
||
|
args = append(args, f.Key, math.Float64frombits(uint64(f.Integer)))
|
||
|
case zapcore.Float32Type:
|
||
|
args = append(args, f.Key, math.Float32frombits(uint32(f.Integer)))
|
||
|
case zapcore.Int64Type,
|
||
|
zapcore.Int32Type,
|
||
|
zapcore.Int16Type,
|
||
|
zapcore.Int8Type,
|
||
|
zapcore.Uint64Type,
|
||
|
zapcore.Uint32Type,
|
||
|
zapcore.Uint16Type,
|
||
|
zapcore.Uint8Type:
|
||
|
args = append(args, f.Key, f.Integer)
|
||
|
case zapcore.UintptrType:
|
||
|
args = append(args, f.Key, uintptr(f.Integer))
|
||
|
case zapcore.StringType:
|
||
|
args = append(args, f.Key, f.String)
|
||
|
case zapcore.TimeType:
|
||
|
if f.Interface != nil {
|
||
|
args = append(args, f.Key, time.Unix(0, f.Integer).In(f.Interface.(*time.Location)))
|
||
|
} else {
|
||
|
// Fall back to UTC if location is nil.
|
||
|
args = append(args, f.Key, time.Unix(0, f.Integer))
|
||
|
}
|
||
|
case zapcore.NamespaceType:
|
||
|
args = append(args, "namespace", f.Key)
|
||
|
case zapcore.SkipType:
|
||
|
break
|
||
|
default:
|
||
|
panic(fmt.Sprintf("unknown field type: %v", f))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
switch ent.Level {
|
||
|
case zapcore.DebugLevel:
|
||
|
c.logger.Debug(ent.Message, args...)
|
||
|
case zapcore.InfoLevel:
|
||
|
c.logger.Info(ent.Message, args...)
|
||
|
case zapcore.WarnLevel:
|
||
|
c.logger.Warn(ent.Message, args...)
|
||
|
case zapcore.ErrorLevel:
|
||
|
c.logger.Error(ent.Message, args...)
|
||
|
case zapcore.DPanicLevel, zapcore.PanicLevel, zapcore.FatalLevel:
|
||
|
c.logger.Crit(ent.Message, args...)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (gethLoggerCore) Sync() error {
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (c *gethLoggerCore) clone() *gethLoggerCore {
|
||
|
return &gethLoggerCore{
|
||
|
LevelEnabler: c.LevelEnabler,
|
||
|
fields: c.fields,
|
||
|
logger: c.logger,
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// NewZapAdapter returns a new zapcore.Core interface which forwards logs to log.Logger.
|
||
|
func NewZapAdapter(logger log.Logger, enab zapcore.LevelEnabler) zapcore.Core {
|
||
|
return &gethLoggerCore{
|
||
|
LevelEnabler: enab,
|
||
|
logger: logger,
|
||
|
}
|
||
|
}
|