mirror of
https://github.com/luxfi/log.git
synced 2026-10-30 16:21:06 +00:00
Features:
- Zero-allocation logging (~8ns/op, 0 allocs)
- Zerolog-style chaining API: log.Info().Str("k","v").Msg("m")
- Geth-style traditional API: logger.Info("m", logger.String("k","v"))
- Structured logging with type-safe field constructors
- Leveled logging (Trace, Debug, Info, Warn, Error, Fatal, Panic)
- Context injection via With()
- Pretty console output with colors
- Sampling and hooks support
Based on rs/zerolog with added geth-style API layer.
125 lines
3.6 KiB
Go
125 lines
3.6 KiB
Go
package json
|
|
|
|
import (
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
// Import from logger/global.go
|
|
timeFormatUnix = ""
|
|
timeFormatUnixMs = "UNIXMS"
|
|
timeFormatUnixMicro = "UNIXMICRO"
|
|
timeFormatUnixNano = "UNIXNANO"
|
|
durationFormatFloat = "float"
|
|
durationFormatInt = "int"
|
|
durationFormatString = "string"
|
|
)
|
|
|
|
// AppendTime formats the input time with the given format
|
|
// and appends the encoded string to the input byte slice.
|
|
func (e Encoder) AppendTime(dst []byte, t time.Time, format string) []byte {
|
|
switch format {
|
|
case timeFormatUnix:
|
|
return e.AppendInt64(dst, t.Unix())
|
|
case timeFormatUnixMs:
|
|
return e.AppendInt64(dst, t.UnixNano()/1000000)
|
|
case timeFormatUnixMicro:
|
|
return e.AppendInt64(dst, t.UnixNano()/1000)
|
|
case timeFormatUnixNano:
|
|
return e.AppendInt64(dst, t.UnixNano())
|
|
}
|
|
return append(t.AppendFormat(append(dst, '"'), format), '"')
|
|
}
|
|
|
|
// AppendTimes converts the input times with the given format
|
|
// and appends the encoded string list to the input byte slice.
|
|
func (Encoder) AppendTimes(dst []byte, vals []time.Time, format string) []byte {
|
|
switch format {
|
|
case timeFormatUnix:
|
|
return appendUnixTimes(dst, vals)
|
|
case timeFormatUnixMs:
|
|
return appendUnixNanoTimes(dst, vals, 1000000)
|
|
case timeFormatUnixMicro:
|
|
return appendUnixNanoTimes(dst, vals, 1000)
|
|
case timeFormatUnixNano:
|
|
return appendUnixNanoTimes(dst, vals, 1)
|
|
}
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = append(vals[0].AppendFormat(append(dst, '"'), format), '"')
|
|
if len(vals) > 1 {
|
|
for _, t := range vals[1:] {
|
|
dst = append(t.AppendFormat(append(dst, ',', '"'), format), '"')
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
func appendUnixTimes(dst []byte, vals []time.Time) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendInt(dst, vals[0].Unix(), 10)
|
|
if len(vals) > 1 {
|
|
for _, t := range vals[1:] {
|
|
dst = strconv.AppendInt(append(dst, ','), t.Unix(), 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
func appendUnixNanoTimes(dst []byte, vals []time.Time, div int64) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = strconv.AppendInt(dst, vals[0].UnixNano()/div, 10)
|
|
if len(vals) > 1 {
|
|
for _, t := range vals[1:] {
|
|
dst = strconv.AppendInt(append(dst, ','), t.UnixNano()/div, 10)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|
|
|
|
// AppendDuration formats the input duration with the given unit & format
|
|
// and appends the encoded string to the input byte slice.
|
|
func (e Encoder) AppendDuration(dst []byte, d time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte {
|
|
if useInt {
|
|
return strconv.AppendInt(dst, int64(d/unit), 10)
|
|
}
|
|
switch format {
|
|
case durationFormatFloat:
|
|
return e.AppendFloat64(dst, float64(d)/float64(unit), precision)
|
|
case durationFormatInt:
|
|
return e.AppendInt64(dst, int64(d/unit))
|
|
case durationFormatString:
|
|
return e.AppendString(dst, d.String())
|
|
}
|
|
return e.AppendFloat64(dst, float64(d)/float64(unit), precision)
|
|
}
|
|
|
|
// AppendDurations formats the input durations with the given unit & format
|
|
// and appends the encoded string list to the input byte slice.
|
|
func (e Encoder) AppendDurations(dst []byte, vals []time.Duration, unit time.Duration, format string, useInt bool, precision int) []byte {
|
|
if len(vals) == 0 {
|
|
return append(dst, '[', ']')
|
|
}
|
|
dst = append(dst, '[')
|
|
dst = e.AppendDuration(dst, vals[0], unit, format, useInt, precision)
|
|
if len(vals) > 1 {
|
|
for _, d := range vals[1:] {
|
|
dst = e.AppendDuration(append(dst, ','), d, unit, format, useInt, precision)
|
|
}
|
|
}
|
|
dst = append(dst, ']')
|
|
return dst
|
|
}
|