mirror of
https://github.com/superseriousbusiness/gotosocial
synced 2025-06-05 21:59:39 +02:00
Upstep Go dependencies (#340)
* Upstep Go dependencies * tiny linter fix * Tidy
This commit is contained in:
71
vendor/codeberg.org/gruf/go-errors/data.go
generated
vendored
71
vendor/codeberg.org/gruf/go-errors/data.go
generated
vendored
@@ -1,6 +1,7 @@
|
||||
package errors
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"codeberg.org/gruf/go-bytes"
|
||||
@@ -9,10 +10,9 @@ import (
|
||||
|
||||
// global logfmt data formatter.
|
||||
var logfmt = logger.TextFormat{
|
||||
Strict: logger.DefaultTextFormat.Strict,
|
||||
MaxDepth: logger.DefaultTextFormat.MaxDepth,
|
||||
Levels: nil,
|
||||
TimeFormat: logger.DefaultTextFormat.TimeFormat,
|
||||
Strict: false,
|
||||
Verbose: true,
|
||||
MaxDepth: 10,
|
||||
}
|
||||
|
||||
// KV is a structure for setting key-value pairs in ErrorData.
|
||||
@@ -30,50 +30,77 @@ type ErrorData interface {
|
||||
// Append adds the supplied key-values to ErrorData, similar keys DO overwrite
|
||||
Append(...KV)
|
||||
|
||||
// String returns a string representation of the ErrorData
|
||||
String() string
|
||||
// Implement byte slice representation formatter.
|
||||
logger.Formattable
|
||||
|
||||
// Implement string representation formatter.
|
||||
fmt.Stringer
|
||||
}
|
||||
|
||||
// NewData returns a new ErrorData implementation.
|
||||
func NewData() ErrorData {
|
||||
return &errorData{
|
||||
data: make(map[string]interface{}, 10),
|
||||
data: make([]KV, 0, 10),
|
||||
}
|
||||
}
|
||||
|
||||
// errorData is our ErrorData implementation, this is essentially
|
||||
// just a thread-safe string-interface map implementation.
|
||||
type errorData struct {
|
||||
data map[string]interface{}
|
||||
buf bytes.Buffer
|
||||
data []KV
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
func (d *errorData) set(key string, value interface{}) {
|
||||
for i := range d.data {
|
||||
if d.data[i].Key == key {
|
||||
// Found existing, update!
|
||||
d.data[i].Value = value
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Add new KV entry to slice
|
||||
d.data = append(d.data, KV{
|
||||
Key: key,
|
||||
Value: value,
|
||||
})
|
||||
}
|
||||
|
||||
func (d *errorData) Value(key string) (interface{}, bool) {
|
||||
d.mu.Lock()
|
||||
v, ok := d.data[key]
|
||||
for i := range d.data {
|
||||
if d.data[i].Key == key {
|
||||
v := d.data[i].Value
|
||||
d.mu.Unlock()
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
d.mu.Unlock()
|
||||
return v, ok
|
||||
return nil, false
|
||||
}
|
||||
|
||||
func (d *errorData) Append(kvs ...KV) {
|
||||
d.mu.Lock()
|
||||
for i := range kvs {
|
||||
k := kvs[i].Key
|
||||
v := kvs[i].Value
|
||||
d.data[k] = v
|
||||
d.set(kvs[i].Key, kvs[i].Value)
|
||||
}
|
||||
d.mu.Unlock()
|
||||
}
|
||||
|
||||
func (d *errorData) String() string {
|
||||
func (d *errorData) AppendFormat(b []byte) []byte {
|
||||
buf := bytes.Buffer{B: b}
|
||||
d.mu.Lock()
|
||||
|
||||
d.buf.Reset()
|
||||
d.buf.B = append(d.buf.B, '{')
|
||||
logfmt.AppendFields(&d.buf, d.data)
|
||||
d.buf.B = append(d.buf.B, '}')
|
||||
|
||||
buf.B = append(buf.B, '{')
|
||||
for i := range d.data {
|
||||
logfmt.AppendKey(&buf, d.data[i].Key)
|
||||
logfmt.AppendValue(&buf, d.data[i].Value)
|
||||
}
|
||||
buf.B = append(buf.B, '}')
|
||||
d.mu.Unlock()
|
||||
return d.buf.StringPtr()
|
||||
return buf.B
|
||||
}
|
||||
|
||||
func (d *errorData) String() string {
|
||||
return string(d.AppendFormat(nil))
|
||||
}
|
||||
|
20
vendor/codeberg.org/gruf/go-logger/default.go
generated
vendored
20
vendor/codeberg.org/gruf/go-logger/default.go
generated
vendored
@@ -76,6 +76,16 @@ func Logf(lvl LEVEL, s string, a ...interface{}) {
|
||||
Default().Logf(lvl, s, a...)
|
||||
}
|
||||
|
||||
// LogFields prints the provided fields formatted as key-value pairs at the supplied log level to the global Logger instance.
|
||||
func LogFields(lvl LEVEL, fields map[string]interface{}) {
|
||||
Default().LogFields(lvl, fields)
|
||||
}
|
||||
|
||||
// LogValues prints the provided values formatted as-so at the supplied log level to the global Logger instance.
|
||||
func LogValues(lvl LEVEL, a ...interface{}) {
|
||||
Default().LogValues(lvl, a...)
|
||||
}
|
||||
|
||||
// Print simply prints provided arguments to the global Logger instance.
|
||||
func Print(a ...interface{}) {
|
||||
Default().Print(a...)
|
||||
@@ -85,3 +95,13 @@ func Print(a ...interface{}) {
|
||||
func Printf(s string, a ...interface{}) {
|
||||
Default().Printf(s, a...)
|
||||
}
|
||||
|
||||
// PrintFields prints the provided fields formatted as key-value pairs to the global Logger instance.
|
||||
func PrintFields(fields map[string]interface{}) {
|
||||
Default().PrintFields(fields)
|
||||
}
|
||||
|
||||
// PrintValues prints the provided values formatted as-so to the global Logger instance.
|
||||
func PrintValues(a ...interface{}) {
|
||||
Default().PrintValues(a...)
|
||||
}
|
||||
|
257
vendor/codeberg.org/gruf/go-logger/entry.go
generated
vendored
257
vendor/codeberg.org/gruf/go-logger/entry.go
generated
vendored
@@ -2,6 +2,7 @@ package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"codeberg.org/gruf/go-bytes"
|
||||
@@ -61,149 +62,281 @@ func (e *Entry) Hooks() *Entry {
|
||||
return e
|
||||
}
|
||||
|
||||
// Byte appends a byte value as key-value pair to the log entry
|
||||
func (e *Entry) Byte(key string, value byte) *Entry {
|
||||
e.log.Format.AppendByteField(e.buf, key, value)
|
||||
// Byte appends a byte value to the log entry
|
||||
func (e *Entry) Byte(value byte) *Entry {
|
||||
e.log.Format.AppendByte(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Bytes appends a byte slice value as key-value pair to the log entry
|
||||
func (e *Entry) Bytes(key string, value []byte) *Entry {
|
||||
e.log.Format.AppendBytesField(e.buf, key, value)
|
||||
// ByteField appends a byte value as key-value pair to the log entry
|
||||
func (e *Entry) ByteField(key string, value byte) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendByte(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Str appends a string value as key-value pair to the log entry
|
||||
func (e *Entry) Str(key string, value string) *Entry {
|
||||
e.log.Format.AppendStringField(e.buf, key, value)
|
||||
// Bytes appends a byte slice value as to the log entry
|
||||
func (e *Entry) Bytes(value []byte) *Entry {
|
||||
e.log.Format.AppendBytes(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Strs appends a string slice value as key-value pair to the log entry
|
||||
func (e *Entry) Strs(key string, value []string) *Entry {
|
||||
e.log.Format.AppendStringsField(e.buf, key, value)
|
||||
// BytesField appends a byte slice value as key-value pair to the log entry
|
||||
func (e *Entry) BytesField(key string, value []byte) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendBytes(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Int appends an int value as key-value pair to the log entry
|
||||
func (e *Entry) Int(key string, value int) *Entry {
|
||||
e.log.Format.AppendIntField(e.buf, key, value)
|
||||
// Str appends a string value to the log entry
|
||||
func (e *Entry) Str(value string) *Entry {
|
||||
e.log.Format.AppendString(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Ints appends an int slice value as key-value pair to the log entry
|
||||
func (e *Entry) Ints(key string, value []int) *Entry {
|
||||
e.log.Format.AppendIntsField(e.buf, key, value)
|
||||
// StrField appends a string value as key-value pair to the log entry
|
||||
func (e *Entry) StrField(key string, value string) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendString(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Uint appends a uint value as key-value pair to the log entry
|
||||
func (e *Entry) Uint(key string, value uint) *Entry {
|
||||
e.log.Format.AppendUintField(e.buf, key, value)
|
||||
// Strs appends a string slice value to the log entry
|
||||
func (e *Entry) Strs(value []string) *Entry {
|
||||
e.log.Format.AppendStrings(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Uints appends a uint slice value as key-value pair to the log entry
|
||||
func (e *Entry) Uints(key string, value []uint) *Entry {
|
||||
e.log.Format.AppendUintsField(e.buf, key, value)
|
||||
// StrsField appends a string slice value as key-value pair to the log entry
|
||||
func (e *Entry) StrsField(key string, value []string) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendStrings(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Float appends a float value as key-value pair to the log entry
|
||||
func (e *Entry) Float(key string, value float64) *Entry {
|
||||
e.log.Format.AppendFloatField(e.buf, key, value)
|
||||
// Int appends an int value to the log entry
|
||||
func (e *Entry) Int(value int) *Entry {
|
||||
e.log.Format.AppendInt(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Floats appends a float slice value as key-value pair to the log entry
|
||||
func (e *Entry) Floats(key string, value []float64) *Entry {
|
||||
e.log.Format.AppendFloatsField(e.buf, key, value)
|
||||
// IntField appends an int value as key-value pair to the log entry
|
||||
func (e *Entry) IntField(key string, value int) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendInt(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Bool appends a bool value as key-value pair to the log entry
|
||||
func (e *Entry) Bool(key string, value bool) *Entry {
|
||||
e.log.Format.AppendBoolField(e.buf, key, value)
|
||||
// Ints appends an int slice value to the log entry
|
||||
func (e *Entry) Ints(value []int) *Entry {
|
||||
e.log.Format.AppendInts(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Bools appends a bool slice value as key-value pair to the log entry
|
||||
func (e *Entry) Bools(key string, value []bool) *Entry {
|
||||
e.log.Format.AppendBoolsField(e.buf, key, value)
|
||||
// IntsField appends an int slice value as key-value pair to the log entry
|
||||
func (e *Entry) IntsField(key string, value []int) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendInts(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Time appends a time.Time value as key-value pair to the log entry
|
||||
func (e *Entry) Time(key string, value time.Time) *Entry {
|
||||
e.log.Format.AppendTimeField(e.buf, key, value)
|
||||
// Uint appends a uint value to the log entry
|
||||
func (e *Entry) Uint(value uint) *Entry {
|
||||
e.log.Format.AppendUint(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Times appends a time.Time slice value as key-value pair to the log entry
|
||||
func (e *Entry) Times(key string, value []time.Time) *Entry {
|
||||
e.log.Format.AppendTimesField(e.buf, key, value)
|
||||
// UintField appends a uint value as key-value pair to the log entry
|
||||
func (e *Entry) UintField(key string, value uint) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendUint(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Duration appends a time.Duration value as key-value pair to the log entry
|
||||
func (e *Entry) Duration(key string, value time.Duration) *Entry {
|
||||
e.log.Format.AppendDurationField(e.buf, key, value)
|
||||
// Uints appends a uint slice value to the log entry
|
||||
func (e *Entry) Uints(value []uint) *Entry {
|
||||
e.log.Format.AppendUints(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Durations appends a time.Duration slice value as key-value pair to the log entry
|
||||
func (e *Entry) Durations(key string, value []time.Duration) *Entry {
|
||||
e.log.Format.AppendDurationsField(e.buf, key, value)
|
||||
// UintsField appends a uint slice value as key-value pair to the log entry
|
||||
func (e *Entry) UintsField(key string, value []uint) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendUints(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Float appends a float value to the log entry
|
||||
func (e *Entry) Float(value float64) *Entry {
|
||||
e.log.Format.AppendFloat(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// FloatField appends a float value as key-value pair to the log entry
|
||||
func (e *Entry) FloatField(key string, value float64) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendFloat(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Floats appends a float slice value to the log entry
|
||||
func (e *Entry) Floats(value []float64) *Entry {
|
||||
e.log.Format.AppendFloats(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// FloatsField appends a float slice value as key-value pair to the log entry
|
||||
func (e *Entry) FloatsField(key string, value []float64) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendFloats(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Bool appends a bool value to the log entry
|
||||
func (e *Entry) Bool(value bool) *Entry {
|
||||
e.log.Format.AppendBool(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// BoolField appends a bool value as key-value pair to the log entry
|
||||
func (e *Entry) BoolField(key string, value bool) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendBool(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Bools appends a bool slice value to the log entry
|
||||
func (e *Entry) Bools(value []bool) *Entry {
|
||||
e.log.Format.AppendBools(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// BoolsField appends a bool slice value as key-value pair to the log entry
|
||||
func (e *Entry) BoolsField(key string, value []bool) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendBools(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Time appends a time.Time value to the log entry
|
||||
func (e *Entry) Time(value time.Time) *Entry {
|
||||
e.log.Format.AppendTime(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// TimeField appends a time.Time value as key-value pair to the log entry
|
||||
func (e *Entry) TimeField(key string, value time.Time) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendTime(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Times appends a time.Time slice value to the log entry
|
||||
func (e *Entry) Times(value []time.Time) *Entry {
|
||||
e.log.Format.AppendTimes(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// TimesField appends a time.Time slice value as key-value pair to the log entry
|
||||
func (e *Entry) TimesField(key string, value []time.Time) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendTimes(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// DurationField appends a time.Duration value to the log entry
|
||||
func (e *Entry) Duration(value time.Duration) *Entry {
|
||||
e.log.Format.AppendDuration(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// DurationField appends a time.Duration value as key-value pair to the log entry
|
||||
func (e *Entry) DurationField(key string, value time.Duration) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendDuration(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Durations appends a time.Duration slice value to the log entry
|
||||
func (e *Entry) Durations(value []time.Duration) *Entry {
|
||||
e.log.Format.AppendDurations(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// DurationsField appends a time.Duration slice value as key-value pair to the log entry
|
||||
func (e *Entry) DurationsField(key string, value []time.Duration) *Entry {
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendDurations(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Field appends an interface value as key-value pair to the log entry
|
||||
func (e *Entry) Field(key string, value interface{}) *Entry {
|
||||
e.log.Format.AppendField(e.buf, key, value)
|
||||
e.log.Format.AppendKey(e.buf, key)
|
||||
e.log.Format.AppendValue(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Fields appends a map of key-value pairs to the log entry
|
||||
func (e *Entry) Fields(fields map[string]interface{}) *Entry {
|
||||
e.log.Format.AppendFields(e.buf, fields)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Value appends the given value to the log entry formatted as a value, without a key.
|
||||
func (e *Entry) Value(value interface{}) *Entry {
|
||||
e.log.Format.AppendValue(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
for key, value := range fields {
|
||||
e.Field(key, value)
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// Values appends the given values to the log entry formatted as values, without a key.
|
||||
func (e *Entry) Values(values ...interface{}) *Entry {
|
||||
e.log.Format.AppendValues(e.buf, values)
|
||||
for _, value := range values {
|
||||
e.log.Format.AppendValue(e.buf, value)
|
||||
e.buf.WriteByte(' ')
|
||||
}
|
||||
return e
|
||||
}
|
||||
|
||||
// Append will append the given args formatted using fmt.Sprint(a...) to the Entry.
|
||||
func (e *Entry) Append(a ...interface{}) *Entry {
|
||||
fmt.Fprint(e.buf, a...)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
||||
// Args appends the given args formatted using the log formatter (usually faster than printf) without any key-value / value formatting.
|
||||
func (e *Entry) Args(a ...interface{}) *Entry {
|
||||
e.log.Format.AppendArgs(e.buf, a)
|
||||
// Appendf will append the given format string and args using fmt.Sprintf(s, a...) to the Entry.
|
||||
func (e *Entry) Appendf(s string, a ...interface{}) *Entry {
|
||||
fmt.Fprintf(e.buf, s, a...)
|
||||
e.buf.WriteByte(' ')
|
||||
return e
|
||||
}
|
||||
|
91
vendor/codeberg.org/gruf/go-logger/format.go
generated
vendored
91
vendor/codeberg.org/gruf/go-logger/format.go
generated
vendored
@@ -9,76 +9,75 @@ import (
|
||||
// Check our types impl LogFormat
|
||||
var _ LogFormat = &TextFormat{}
|
||||
|
||||
// Formattable defines a type capable of writing a string formatted form
|
||||
// of itself to a supplied byte buffer, and returning the resulting byte
|
||||
// buffer. Implementing this will greatly speed up formatting of custom
|
||||
// types passed to LogFormat (assuming they implement checking for this).
|
||||
type Formattable interface {
|
||||
AppendFormat([]byte) []byte
|
||||
}
|
||||
|
||||
// LogFormat defines a method of formatting log entries
|
||||
type LogFormat interface {
|
||||
// AppendLevel appends given log level to the log buffer
|
||||
// AppendKey appends given key to the log buffer
|
||||
AppendKey(buf *bytes.Buffer, key string)
|
||||
|
||||
// AppendLevel appends given log level as key-value pair to the log buffer
|
||||
AppendLevel(buf *bytes.Buffer, lvl LEVEL)
|
||||
|
||||
// AppendTimestamp appends given time format string to the log buffer
|
||||
// AppendTimestamp appends given timestamp string as key-value pair to the log buffer
|
||||
AppendTimestamp(buf *bytes.Buffer, fmtNow string)
|
||||
|
||||
// AppendField appends given key-value pair to the log buffer
|
||||
AppendField(buf *bytes.Buffer, key string, value interface{})
|
||||
|
||||
// AppendFields appends given key-values pairs to the log buffer
|
||||
AppendFields(buf *bytes.Buffer, fields map[string]interface{})
|
||||
|
||||
// AppendValue appends given interface formatted as value to the log buffer
|
||||
AppendValue(buf *bytes.Buffer, value interface{})
|
||||
|
||||
// AppendValues appends given interfaces formatted as values to the log buffer
|
||||
AppendValues(buf *bytes.Buffer, slice []interface{})
|
||||
// AppendByte appends given byte value to the log buffer
|
||||
AppendByte(buf *bytes.Buffer, value byte)
|
||||
|
||||
// AppendArgs appends given interfaces raw to the log buffer
|
||||
AppendArgs(buf *bytes.Buffer, args []interface{})
|
||||
// AppendBytes appends given byte slice value to the log buffer
|
||||
AppendBytes(buf *bytes.Buffer, value []byte)
|
||||
|
||||
// AppendByteField appends given byte value as key-value pair to the log buffer
|
||||
AppendByteField(buf *bytes.Buffer, key string, value byte)
|
||||
// AppendString appends given string value to the log buffer
|
||||
AppendString(buf *bytes.Buffer, value string)
|
||||
|
||||
// AppendBytesField appends given byte slice value as key-value pair to the log buffer
|
||||
AppendBytesField(buf *bytes.Buffer, key string, value []byte)
|
||||
// AppendStrings appends given string slice value to the log buffer
|
||||
AppendStrings(buf *bytes.Buffer, value []string)
|
||||
|
||||
// AppendStringField appends given string value as key-value pair to the log buffer
|
||||
AppendStringField(buf *bytes.Buffer, key string, value string)
|
||||
// AppendBool appends given bool value to the log buffer
|
||||
AppendBool(buf *bytes.Buffer, value bool)
|
||||
|
||||
// AppendStringsField appends given string slice value as key-value pair to the log buffer
|
||||
AppendStringsField(buf *bytes.Buffer, key string, value []string)
|
||||
// AppendBools appends given bool slice value to the log buffer
|
||||
AppendBools(buf *bytes.Buffer, value []bool)
|
||||
|
||||
// AppendBoolField appends given bool value as key-value pair to the log buffer
|
||||
AppendBoolField(buf *bytes.Buffer, key string, value bool)
|
||||
// AppendInt appends given int value to the log buffer
|
||||
AppendInt(buf *bytes.Buffer, value int)
|
||||
|
||||
// AppendBoolsField appends given bool slice value as key-value pair to the log buffer
|
||||
AppendBoolsField(buf *bytes.Buffer, key string, value []bool)
|
||||
// AppendInts appends given int slice value to the log buffer
|
||||
AppendInts(buf *bytes.Buffer, value []int)
|
||||
|
||||
// AppendIntField appends given int value as key-value pair to the log buffer
|
||||
AppendIntField(buf *bytes.Buffer, key string, value int)
|
||||
// AppendUint appends given uint value to the log buffer
|
||||
AppendUint(buf *bytes.Buffer, value uint)
|
||||
|
||||
// AppendIntsField appends given int slice value as key-value pair to the log buffer
|
||||
AppendIntsField(buf *bytes.Buffer, key string, value []int)
|
||||
// AppendUints appends given uint slice value to the log buffer
|
||||
AppendUints(buf *bytes.Buffer, value []uint)
|
||||
|
||||
// AppendUintField appends given uint value as key-value pair to the log buffer
|
||||
AppendUintField(buf *bytes.Buffer, key string, value uint)
|
||||
// AppendFloat appends given float value to the log buffer
|
||||
AppendFloat(buf *bytes.Buffer, value float64)
|
||||
|
||||
// AppendUintsField appends given uint slice value as key-value pair to the log buffer
|
||||
AppendUintsField(buf *bytes.Buffer, key string, value []uint)
|
||||
// AppendFloats appends given float slice value to the log buffer
|
||||
AppendFloats(buf *bytes.Buffer, value []float64)
|
||||
|
||||
// AppendFloatField appends given float value as key-value pair to the log buffer
|
||||
AppendFloatField(buf *bytes.Buffer, key string, value float64)
|
||||
// AppendTime appends given time value to the log buffer
|
||||
AppendTime(buf *bytes.Buffer, value time.Time)
|
||||
|
||||
// AppendFloatsField appends given float slice value as key-value pair to the log buffer
|
||||
AppendFloatsField(buf *bytes.Buffer, key string, value []float64)
|
||||
// AppendTimes appends given time slice value to the log buffer
|
||||
AppendTimes(buf *bytes.Buffer, value []time.Time)
|
||||
|
||||
// AppendTimeField appends given time value as key-value pair to the log buffer
|
||||
AppendTimeField(buf *bytes.Buffer, key string, value time.Time)
|
||||
// AppendDuration appends given duration value to the log buffer
|
||||
AppendDuration(buf *bytes.Buffer, value time.Duration)
|
||||
|
||||
// AppendTimesField appends given time slice value as key-value pair to the log buffer
|
||||
AppendTimesField(buf *bytes.Buffer, key string, value []time.Time)
|
||||
|
||||
// AppendDurationField appends given duration value as key-value pair to the log buffer
|
||||
AppendDurationField(buf *bytes.Buffer, key string, value time.Duration)
|
||||
|
||||
// AppendDurationsField appends given duration slice value as key-value pair to the log buffer
|
||||
AppendDurationsField(buf *bytes.Buffer, key string, value []time.Duration)
|
||||
// AppendDurations appends given duration slice value to the log buffer
|
||||
AppendDurations(buf *bytes.Buffer, value []time.Duration)
|
||||
|
||||
// AppendMsg appends given msg as key-value pair to the log buffer using fmt.Sprint(...) formatting
|
||||
AppendMsg(buf *bytes.Buffer, a ...interface{})
|
||||
|
413
vendor/codeberg.org/gruf/go-logger/format_text.go
generated
vendored
413
vendor/codeberg.org/gruf/go-logger/format_text.go
generated
vendored
@@ -12,49 +12,63 @@ import (
|
||||
|
||||
// DefaultTextFormat is the default TextFormat instance
|
||||
var DefaultTextFormat = TextFormat{
|
||||
Strict: false,
|
||||
MaxDepth: 5,
|
||||
Levels: DefaultLevels(),
|
||||
TimeFormat: time.RFC1123,
|
||||
Strict: false,
|
||||
Verbose: false,
|
||||
MaxDepth: 10,
|
||||
Levels: DefaultLevels(),
|
||||
}
|
||||
|
||||
// TextFormat is the default LogFormat implementation, with very similar formatting to logfmt
|
||||
// TextFormat is the default LogFormat implementation, with very similar formatting to the
|
||||
// standard "fmt" package's '%#v' operator. The main difference being that pointers are
|
||||
// dereferenced as far as possible in order to reach a printable value. It is also *mildly* faster.
|
||||
type TextFormat struct {
|
||||
// Strict defines whether to use strict key-value pair formatting, i.e. should the level
|
||||
// timestamp and msg be formatted as key-value pairs (with forced quoting for msg)
|
||||
Strict bool
|
||||
|
||||
// Verbose defines whether to increase output verbosity, i.e. include types with nil values
|
||||
// and force values implementing .String() / .AppendFormat() to be printed as a struct etc.
|
||||
Verbose bool
|
||||
|
||||
// MaxDepth specifies the max depth of fields the formatter will iterate
|
||||
MaxDepth uint8
|
||||
|
||||
// Levels defines the map of log LEVELs to level strings
|
||||
Levels Levels
|
||||
|
||||
// TimeFormat specifies the time formatting to use
|
||||
TimeFormat string
|
||||
}
|
||||
|
||||
// fmt returns a new format instance based on receiver TextFormat and given buffer
|
||||
func (f TextFormat) fmt(buf *bytes.Buffer) format {
|
||||
var flags uint8
|
||||
if f.Verbose {
|
||||
flags |= vboseBit
|
||||
}
|
||||
return format{
|
||||
isKey: false,
|
||||
depth: 0,
|
||||
txt: f,
|
||||
flags: flags,
|
||||
depth: uint16(f.MaxDepth) << 8,
|
||||
buf: buf,
|
||||
}
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendKey(buf *bytes.Buffer, key string) {
|
||||
if len(key) > 0 {
|
||||
// only append if key is non-zero length
|
||||
appendString(f.fmt(buf).SetIsKey(true), key)
|
||||
buf.WriteByte('=')
|
||||
}
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendLevel(buf *bytes.Buffer, lvl LEVEL) {
|
||||
if f.Strict {
|
||||
// Strict format, append level key
|
||||
buf.WriteString(`level=`)
|
||||
buf.WriteString(f.Levels.LevelString(lvl))
|
||||
buf.WriteString(f.Levels.Get(lvl))
|
||||
return
|
||||
}
|
||||
|
||||
// Write level string
|
||||
buf.WriteByte('[')
|
||||
buf.WriteString(f.Levels.LevelString(lvl))
|
||||
buf.WriteString(f.Levels.Get(lvl))
|
||||
buf.WriteByte(']')
|
||||
}
|
||||
|
||||
@@ -70,140 +84,71 @@ func (f TextFormat) AppendTimestamp(buf *bytes.Buffer, now string) {
|
||||
buf.WriteString(now)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendField(buf *bytes.Buffer, key string, value interface{}) {
|
||||
appendKey(buf, key)
|
||||
appendIfaceOrRValue(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendFields(buf *bytes.Buffer, fields map[string]interface{}) {
|
||||
fmt := f.fmt(buf)
|
||||
|
||||
// Append individual fields
|
||||
for key, value := range fields {
|
||||
appendKey(buf, key)
|
||||
appendIfaceOrRValue(fmt, value)
|
||||
buf.WriteByte(' ')
|
||||
}
|
||||
|
||||
// Drop last space
|
||||
if len(fields) > 0 {
|
||||
buf.Truncate(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendValue(buf *bytes.Buffer, value interface{}) {
|
||||
appendIfaceOrRValue(f.fmt(buf).IsKey(true), value)
|
||||
appendIfaceOrRValue(f.fmt(buf).SetIsKey(false), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendValues(buf *bytes.Buffer, values []interface{}) {
|
||||
// Prepare formatter
|
||||
fmt := f.fmt(buf).IsKey(true)
|
||||
|
||||
// Append each of the values
|
||||
for _, value := range values {
|
||||
appendIfaceOrRValue(fmt, value)
|
||||
buf.WriteByte(' ')
|
||||
}
|
||||
|
||||
// Drop last space
|
||||
if len(values) > 0 {
|
||||
buf.Truncate(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendArgs(buf *bytes.Buffer, args []interface{}) {
|
||||
// Prepare formatter
|
||||
fmt := f.fmt(buf).IsKey(true).IsRaw(true)
|
||||
|
||||
// Append each of the values
|
||||
for _, arg := range args {
|
||||
appendIfaceOrRValue(fmt, arg)
|
||||
buf.WriteByte(' ')
|
||||
}
|
||||
|
||||
// Drop last space
|
||||
if len(args) > 0 {
|
||||
buf.Truncate(1)
|
||||
}
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendByteField(buf *bytes.Buffer, key string, value byte) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendByte(buf *bytes.Buffer, value byte) {
|
||||
appendByte(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendBytesField(buf *bytes.Buffer, key string, value []byte) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendBytes(buf *bytes.Buffer, value []byte) {
|
||||
appendBytes(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendStringField(buf *bytes.Buffer, key string, value string) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendString(buf *bytes.Buffer, value string) {
|
||||
appendString(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendStringsField(buf *bytes.Buffer, key string, value []string) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendStrings(buf *bytes.Buffer, value []string) {
|
||||
appendStringSlice(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendBoolField(buf *bytes.Buffer, key string, value bool) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendBool(buf *bytes.Buffer, value bool) {
|
||||
appendBool(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendBoolsField(buf *bytes.Buffer, key string, value []bool) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendBools(buf *bytes.Buffer, value []bool) {
|
||||
appendBoolSlice(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendIntField(buf *bytes.Buffer, key string, value int) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendInt(buf *bytes.Buffer, value int) {
|
||||
appendInt(f.fmt(buf), int64(value))
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendIntsField(buf *bytes.Buffer, key string, value []int) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendInts(buf *bytes.Buffer, value []int) {
|
||||
appendIntSlice(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendUintField(buf *bytes.Buffer, key string, value uint) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendUint(buf *bytes.Buffer, value uint) {
|
||||
appendUint(f.fmt(buf), uint64(value))
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendUintsField(buf *bytes.Buffer, key string, value []uint) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendUints(buf *bytes.Buffer, value []uint) {
|
||||
appendUintSlice(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendFloatField(buf *bytes.Buffer, key string, value float64) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendFloat(buf *bytes.Buffer, value float64) {
|
||||
appendFloat(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendFloatsField(buf *bytes.Buffer, key string, value []float64) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendFloats(buf *bytes.Buffer, value []float64) {
|
||||
appendFloatSlice(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendTimeField(buf *bytes.Buffer, key string, value time.Time) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendTime(buf *bytes.Buffer, value time.Time) {
|
||||
appendTime(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendTimesField(buf *bytes.Buffer, key string, value []time.Time) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendTimes(buf *bytes.Buffer, value []time.Time) {
|
||||
appendTimeSlice(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendDurationField(buf *bytes.Buffer, key string, value time.Duration) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendDuration(buf *bytes.Buffer, value time.Duration) {
|
||||
appendDuration(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
func (f TextFormat) AppendDurationsField(buf *bytes.Buffer, key string, value []time.Duration) {
|
||||
appendKey(buf, key)
|
||||
func (f TextFormat) AppendDurations(buf *bytes.Buffer, value []time.Duration) {
|
||||
appendDurationSlice(f.fmt(buf), value)
|
||||
}
|
||||
|
||||
@@ -233,46 +178,89 @@ func (f TextFormat) AppendMsgf(buf *bytes.Buffer, s string, a ...interface{}) {
|
||||
|
||||
// format is the object passed among the append___ formatting functions
|
||||
type format struct {
|
||||
raw bool
|
||||
isKey bool
|
||||
depth uint8
|
||||
txt TextFormat
|
||||
buf *bytes.Buffer
|
||||
flags uint8 // 'isKey' and 'verbose' flags
|
||||
depth uint16 // encoded as 0b(maxDepth)(curDepth)
|
||||
buf *bytes.Buffer // out buffer
|
||||
}
|
||||
|
||||
// IsKey returns format instance with key set to value
|
||||
func (f format) IsKey(is bool) format {
|
||||
const (
|
||||
// flag bit constants
|
||||
isKeyBit = uint8(1) << 0
|
||||
vboseBit = uint8(1) << 1
|
||||
)
|
||||
|
||||
// AtMaxDepth returns whether format is currently at max depth.
|
||||
func (f format) AtMaxDepth() bool {
|
||||
return uint8(f.depth) >= uint8(f.depth>>8)
|
||||
}
|
||||
|
||||
// IsKey returns whether the isKey flag is set.
|
||||
func (f format) IsKey() bool {
|
||||
return (f.flags & isKeyBit) != 0
|
||||
}
|
||||
|
||||
// Verbose returns whether the verbose flag is set.
|
||||
func (f format) Verbose() bool {
|
||||
return (f.flags & vboseBit) != 0
|
||||
}
|
||||
|
||||
// SetIsKey returns format instance with the isKey bit set to value.
|
||||
func (f format) SetIsKey(is bool) format {
|
||||
flags := f.flags
|
||||
if is {
|
||||
flags |= isKeyBit
|
||||
} else {
|
||||
flags &= ^isKeyBit
|
||||
}
|
||||
return format{
|
||||
raw: f.raw,
|
||||
isKey: is,
|
||||
flags: flags,
|
||||
depth: f.depth,
|
||||
txt: f.txt,
|
||||
buf: f.buf,
|
||||
}
|
||||
}
|
||||
|
||||
// IsRaw returns format instance with raw set to value
|
||||
func (f format) IsRaw(is bool) format {
|
||||
return format{
|
||||
raw: is,
|
||||
isKey: f.isKey,
|
||||
depth: f.depth,
|
||||
txt: f.txt,
|
||||
buf: f.buf,
|
||||
}
|
||||
}
|
||||
|
||||
// IncrDepth returns format instance with depth incremented
|
||||
// IncrDepth returns format instance with depth incremented.
|
||||
func (f format) IncrDepth() format {
|
||||
return format{
|
||||
raw: f.raw,
|
||||
isKey: f.isKey,
|
||||
depth: f.depth + 1,
|
||||
txt: f.txt,
|
||||
flags: f.flags,
|
||||
depth: (f.depth & 0b1111111100000000) | uint16(uint8(f.depth)+1),
|
||||
buf: f.buf,
|
||||
}
|
||||
}
|
||||
|
||||
// appendNilType writes nil to buf, type included if verbose.
|
||||
func appendNilType(fmt format, t string) {
|
||||
if fmt.Verbose() {
|
||||
fmt.buf.WriteByte('(')
|
||||
fmt.buf.WriteString(t)
|
||||
fmt.buf.WriteString(`)(nil)`)
|
||||
} else {
|
||||
fmt.buf.WriteString(`nil`)
|
||||
}
|
||||
}
|
||||
|
||||
// appendNilFace writes nil to buf, type included if verbose.
|
||||
func appendNilIface(fmt format, i interface{}) {
|
||||
if fmt.Verbose() {
|
||||
fmt.buf.WriteByte('(')
|
||||
fmt.buf.WriteString(reflect.TypeOf(i).String())
|
||||
fmt.buf.WriteString(`)(nil)`)
|
||||
} else {
|
||||
fmt.buf.WriteString(`nil`)
|
||||
}
|
||||
}
|
||||
|
||||
// appendNilRValue writes nil to buf, type included if verbose.
|
||||
func appendNilRValue(fmt format, v reflect.Value) {
|
||||
if fmt.Verbose() {
|
||||
fmt.buf.WriteByte('(')
|
||||
fmt.buf.WriteString(v.Type().String())
|
||||
fmt.buf.WriteString(`)(nil)`)
|
||||
} else {
|
||||
fmt.buf.WriteString(`nil`)
|
||||
}
|
||||
}
|
||||
|
||||
// appendByte writes a single byte to buf
|
||||
func appendByte(fmt format, b byte) {
|
||||
fmt.buf.WriteByte(b)
|
||||
@@ -280,7 +268,7 @@ func appendByte(fmt format, b byte) {
|
||||
|
||||
// appendBytes writes a quoted byte slice to buf
|
||||
func appendBytes(fmt format, b []byte) {
|
||||
if !fmt.isKey && b == nil {
|
||||
if !fmt.IsKey() && b == nil {
|
||||
// Values CAN be nil formatted
|
||||
appendNilType(fmt, `[]byte`)
|
||||
} else {
|
||||
@@ -290,19 +278,16 @@ func appendBytes(fmt format, b []byte) {
|
||||
|
||||
// appendString writes an escaped, double-quoted string to buf
|
||||
func appendString(fmt format, s string) {
|
||||
if !fmt.raw {
|
||||
// Only handle quoting if NOT raw
|
||||
if !strconv.CanBackquote(s) || !fmt.isKey {
|
||||
// All non-keys and multiline keys get quoted + escaped
|
||||
fmt.buf.B = strconv.AppendQuote(fmt.buf.B, s)
|
||||
return
|
||||
} else if containsSpaceOrTab(s) {
|
||||
// Key containing spaces/tabs, quote this
|
||||
fmt.buf.WriteByte('"')
|
||||
fmt.buf.WriteString(s)
|
||||
fmt.buf.WriteByte('"')
|
||||
return
|
||||
}
|
||||
if !fmt.IsKey() || !strconv.CanBackquote(s) {
|
||||
// All non-keys and multiline keys get quoted + escaped
|
||||
fmt.buf.B = strconv.AppendQuote(fmt.buf.B, s)
|
||||
return
|
||||
} else if containsSpaceOrTab(s) {
|
||||
// Key containing spaces/tabs, quote this
|
||||
fmt.buf.WriteByte('"')
|
||||
fmt.buf.WriteString(s)
|
||||
fmt.buf.WriteByte('"')
|
||||
return
|
||||
}
|
||||
|
||||
// Safe to leave unquoted
|
||||
@@ -319,12 +304,9 @@ func appendStringSlice(fmt format, s []string) {
|
||||
|
||||
fmt.buf.WriteByte('[')
|
||||
|
||||
// Prepare formatter
|
||||
fmt = fmt.IsKey(false)
|
||||
|
||||
// Write elements
|
||||
for _, s := range s {
|
||||
appendString(fmt, s)
|
||||
appendString(fmt.SetIsKey(false), s)
|
||||
fmt.buf.WriteByte(',')
|
||||
}
|
||||
|
||||
@@ -454,7 +436,7 @@ func appendFloatSlice(fmt format, f []float64) {
|
||||
|
||||
// appendTime writes a formatted, quoted time string to buf
|
||||
func appendTime(fmt format, t time.Time) {
|
||||
appendString(fmt.IsKey(true), t.Format(fmt.txt.TimeFormat))
|
||||
appendString(fmt.SetIsKey(true), t.Format(time.RFC1123))
|
||||
}
|
||||
|
||||
// appendTimeSlice writes a slice of formatted time strings to buf
|
||||
@@ -467,12 +449,9 @@ func appendTimeSlice(fmt format, t []time.Time) {
|
||||
|
||||
fmt.buf.WriteByte('[')
|
||||
|
||||
// Prepare formatter
|
||||
fmt = fmt.IsKey(true)
|
||||
|
||||
// Write elements
|
||||
for _, t := range t {
|
||||
appendString(fmt, t.Format(fmt.txt.TimeFormat))
|
||||
appendString(fmt.SetIsKey(true), t.Format(time.RFC1123))
|
||||
fmt.buf.WriteByte(',')
|
||||
}
|
||||
|
||||
@@ -486,7 +465,7 @@ func appendTimeSlice(fmt format, t []time.Time) {
|
||||
|
||||
// appendDuration writes a formatted, quoted duration string to buf
|
||||
func appendDuration(fmt format, d time.Duration) {
|
||||
appendString(fmt.IsKey(true), d.String())
|
||||
appendString(fmt.SetIsKey(true), d.String())
|
||||
}
|
||||
|
||||
// appendDurationSlice writes a slice of formatted, quoted duration strings to buf
|
||||
@@ -499,12 +478,9 @@ func appendDurationSlice(fmt format, d []time.Duration) {
|
||||
|
||||
fmt.buf.WriteByte('[')
|
||||
|
||||
// Prepare formatter
|
||||
fmt = fmt.IsKey(true)
|
||||
|
||||
// Write elements
|
||||
for _, d := range d {
|
||||
appendString(fmt, d.String())
|
||||
appendString(fmt.SetIsKey(true), d.String())
|
||||
fmt.buf.WriteByte(',')
|
||||
}
|
||||
|
||||
@@ -560,22 +536,10 @@ func notNil(i interface{}) bool {
|
||||
return (e.valueOf != nil)
|
||||
}
|
||||
|
||||
// appendNilType will append a formatted nil of type 't'
|
||||
func appendNilType(fmt format, t string) {
|
||||
fmt.buf.WriteByte('(')
|
||||
fmt.buf.WriteString(t)
|
||||
fmt.buf.WriteString(`)(<nil>)`)
|
||||
}
|
||||
|
||||
// appendNilValue will append a formatted nil of type fetched from value 'v'
|
||||
func appendNilRValue(fmt format, v reflect.Value) {
|
||||
appendNilType(fmt, v.Type().String())
|
||||
}
|
||||
|
||||
// appendIfaceOrReflectValue will attempt to append as interface, falling back to reflection
|
||||
func appendIfaceOrRValue(fmt format, i interface{}) {
|
||||
// appendIfaceOrRValueNext performs appendIfaceOrRValue checking + incr depth
|
||||
func appendIfaceOrRValueNext(fmt format, i interface{}) {
|
||||
// Check we haven't hit max
|
||||
if fmt.depth >= fmt.txt.MaxDepth {
|
||||
if fmt.AtMaxDepth() {
|
||||
fmt.buf.WriteString("...")
|
||||
return
|
||||
}
|
||||
@@ -583,16 +547,21 @@ func appendIfaceOrRValue(fmt format, i interface{}) {
|
||||
// Incr the depth
|
||||
fmt = fmt.IncrDepth()
|
||||
|
||||
// Attempt to append interface, fallback to reflect
|
||||
// Make actual call
|
||||
appendIfaceOrRValue(fmt, i)
|
||||
}
|
||||
|
||||
// appendIfaceOrReflectValue will attempt to append as interface, falling back to reflection
|
||||
func appendIfaceOrRValue(fmt format, i interface{}) {
|
||||
if !appendIface(fmt, i) {
|
||||
appendRValue(fmt, reflect.ValueOf(i))
|
||||
}
|
||||
}
|
||||
|
||||
// appendReflectValueOrIface will attempt to interface the reflect.Value, falling back to using this directly
|
||||
func appendRValueOrIface(fmt format, v reflect.Value) {
|
||||
// appendValueOrIfaceNext performs appendRValueOrIface checking + incr depth
|
||||
func appendRValueOrIfaceNext(fmt format, v reflect.Value) {
|
||||
// Check we haven't hit max
|
||||
if fmt.depth >= fmt.txt.MaxDepth {
|
||||
if fmt.AtMaxDepth() {
|
||||
fmt.buf.WriteString("...")
|
||||
return
|
||||
}
|
||||
@@ -600,7 +569,12 @@ func appendRValueOrIface(fmt format, v reflect.Value) {
|
||||
// Incr the depth
|
||||
fmt = fmt.IncrDepth()
|
||||
|
||||
// Attempt to interface reflect value, fallback to handling value itself
|
||||
// Make actual call
|
||||
appendRValueOrIface(fmt, v)
|
||||
}
|
||||
|
||||
// appendRValueOrIface will attempt to interface the reflect.Value, falling back to using this directly
|
||||
func appendRValueOrIface(fmt format, v reflect.Value) {
|
||||
if !v.CanInterface() || !appendIface(fmt, v.Interface()) {
|
||||
appendRValue(fmt, v)
|
||||
}
|
||||
@@ -610,7 +584,7 @@ func appendRValueOrIface(fmt format, v reflect.Value) {
|
||||
func appendIface(fmt format, i interface{}) bool {
|
||||
switch i := i.(type) {
|
||||
case nil:
|
||||
fmt.buf.WriteString(`<nil>`)
|
||||
fmt.buf.WriteString(`nil`)
|
||||
case byte:
|
||||
appendByte(fmt, i)
|
||||
case []byte:
|
||||
@@ -671,13 +645,35 @@ func appendIface(fmt format, i interface{}) bool {
|
||||
if notNil(i) /* use safer nil check */ {
|
||||
appendString(fmt, i.Error())
|
||||
} else {
|
||||
appendNilType(fmt, reflect.TypeOf(i).String())
|
||||
appendNilIface(fmt, i)
|
||||
}
|
||||
case Formattable:
|
||||
switch {
|
||||
// catch nil case first
|
||||
case !notNil(i):
|
||||
appendNilIface(fmt, i)
|
||||
|
||||
// not permitted
|
||||
case fmt.Verbose():
|
||||
return false
|
||||
|
||||
// use func
|
||||
default:
|
||||
fmt.buf.B = i.AppendFormat(fmt.buf.B)
|
||||
}
|
||||
case stdfmt.Stringer:
|
||||
if notNil(i) /* use safer nil check */ {
|
||||
switch {
|
||||
// catch nil case first
|
||||
case !notNil(i):
|
||||
appendNilIface(fmt, i)
|
||||
|
||||
// not permitted
|
||||
case fmt.Verbose():
|
||||
return false
|
||||
|
||||
// use func
|
||||
default:
|
||||
appendString(fmt, i.String())
|
||||
} else {
|
||||
appendNilType(fmt, reflect.TypeOf(i).String())
|
||||
}
|
||||
default:
|
||||
return false // could not handle
|
||||
@@ -720,7 +716,7 @@ func appendRValue(fmt format, v reflect.Value) {
|
||||
fmt.buf.WriteString("0x")
|
||||
fmt.buf.B = strconv.AppendUint(fmt.buf.B, uint64(u), 16)
|
||||
} else {
|
||||
fmt.buf.WriteString(`<nil>`)
|
||||
fmt.buf.WriteString(`nil`)
|
||||
}
|
||||
fmt.buf.WriteByte(')')
|
||||
case reflect.Uintptr:
|
||||
@@ -730,7 +726,7 @@ func appendRValue(fmt format, v reflect.Value) {
|
||||
fmt.buf.WriteString("0x")
|
||||
fmt.buf.B = strconv.AppendUint(fmt.buf.B, u, 16)
|
||||
} else {
|
||||
fmt.buf.WriteString(`<nil>`)
|
||||
fmt.buf.WriteString(`nil`)
|
||||
}
|
||||
fmt.buf.WriteByte(')')
|
||||
case reflect.String:
|
||||
@@ -758,15 +754,11 @@ func appendIfaceMap(fmt format, v map[string]interface{}) {
|
||||
|
||||
fmt.buf.WriteByte('{')
|
||||
|
||||
// Prepare formatters
|
||||
fmtKey := fmt.IsKey(true)
|
||||
fmtVal := fmt.IsKey(false)
|
||||
|
||||
// Write map pairs!
|
||||
for key, value := range v {
|
||||
appendString(fmtKey, key)
|
||||
appendString(fmt.SetIsKey(true), key)
|
||||
fmt.buf.WriteByte('=')
|
||||
appendIfaceOrRValue(fmtVal, value)
|
||||
appendIfaceOrRValueNext(fmt.SetIsKey(false), value)
|
||||
fmt.buf.WriteByte(' ')
|
||||
}
|
||||
|
||||
@@ -785,12 +777,9 @@ func appendArrayType(fmt format, v reflect.Value) {
|
||||
|
||||
fmt.buf.WriteByte('[')
|
||||
|
||||
// Prepare formatter
|
||||
fmt = fmt.IsKey(false)
|
||||
|
||||
// Write values
|
||||
for i := 0; i < n; i++ {
|
||||
appendRValueOrIface(fmt, v.Index(i))
|
||||
appendRValueOrIfaceNext(fmt.SetIsKey(false), v.Index(i))
|
||||
fmt.buf.WriteByte(',')
|
||||
}
|
||||
|
||||
@@ -825,15 +814,11 @@ func appendMapType(fmt format, v reflect.Value) {
|
||||
|
||||
fmt.buf.WriteByte('{')
|
||||
|
||||
// Prepare formatters
|
||||
fmtKey := fmt.IsKey(true)
|
||||
fmtVal := fmt.IsKey(false)
|
||||
|
||||
// Iterate pairs
|
||||
for r.Next() {
|
||||
appendRValueOrIface(fmtKey, r.Key())
|
||||
appendRValueOrIfaceNext(fmt.SetIsKey(true), r.Key())
|
||||
fmt.buf.WriteByte('=')
|
||||
appendRValueOrIface(fmtVal, r.Value())
|
||||
appendRValueOrIfaceNext(fmt.SetIsKey(false), r.Value())
|
||||
fmt.buf.WriteByte(' ')
|
||||
}
|
||||
|
||||
@@ -852,24 +837,25 @@ func appendStructType(fmt format, v reflect.Value) {
|
||||
n := v.NumField()
|
||||
w := 0
|
||||
|
||||
fmt.buf.WriteByte('{')
|
||||
// If verbose, append the type
|
||||
|
||||
// Prepare formatters
|
||||
fmtKey := fmt.IsKey(true)
|
||||
fmtVal := fmt.IsKey(false)
|
||||
fmt.buf.WriteByte('{')
|
||||
|
||||
// Iterate fields
|
||||
for i := 0; i < n; i++ {
|
||||
vfield := v.Field(i)
|
||||
name := t.Field(i).Name
|
||||
|
||||
// Append field name
|
||||
appendString(fmt.SetIsKey(true), name)
|
||||
fmt.buf.WriteByte('=')
|
||||
|
||||
if !vfield.CanInterface() {
|
||||
// This is an unexported field
|
||||
appendRValue(fmtVal, vfield)
|
||||
appendRValue(fmt.SetIsKey(false), vfield)
|
||||
} else {
|
||||
// This is an exported field!
|
||||
appendString(fmtKey, t.Field(i).Name)
|
||||
fmt.buf.WriteByte('=')
|
||||
appendRValueOrIface(fmtVal, vfield)
|
||||
appendRValueOrIfaceNext(fmt.SetIsKey(false), vfield)
|
||||
}
|
||||
|
||||
// Iter written count
|
||||
@@ -885,25 +871,6 @@ func appendStructType(fmt format, v reflect.Value) {
|
||||
fmt.buf.WriteByte('}')
|
||||
}
|
||||
|
||||
// appendKey should only be used in the case of directly setting key-value pairs,
|
||||
// not in the case of appendMapType, appendStructType
|
||||
func appendKey(buf *bytes.Buffer, key string) {
|
||||
if len(key) > 0 {
|
||||
if containsSpaceOrTab(key) {
|
||||
// Key containing spaces/tabs, quote this
|
||||
buf.WriteByte('"')
|
||||
buf.WriteString(key)
|
||||
buf.WriteByte('"')
|
||||
} else {
|
||||
// Key is safe to leave unquoted
|
||||
buf.WriteString(key)
|
||||
}
|
||||
|
||||
// Write final '='
|
||||
buf.WriteByte('=')
|
||||
}
|
||||
}
|
||||
|
||||
// containsSpaceOrTab checks if "s" contains space or tabs
|
||||
func containsSpaceOrTab(s string) bool {
|
||||
for _, r := range s {
|
||||
|
15
vendor/codeberg.org/gruf/go-logger/level.go
generated
vendored
15
vendor/codeberg.org/gruf/go-logger/level.go
generated
vendored
@@ -5,7 +5,7 @@ type LEVEL uint8
|
||||
|
||||
// Available levels of logging.
|
||||
const (
|
||||
unset LEVEL = 255
|
||||
unset LEVEL = ^LEVEL(0)
|
||||
DEBUG LEVEL = 5
|
||||
INFO LEVEL = 10
|
||||
WARN LEVEL = 15
|
||||
@@ -16,7 +16,7 @@ const (
|
||||
var unknownLevel = "unknown"
|
||||
|
||||
// Levels defines a mapping of log LEVELs to formatted level strings
|
||||
type Levels map[LEVEL]string
|
||||
type Levels [^LEVEL(0)]string
|
||||
|
||||
// DefaultLevels returns the default set of log levels
|
||||
func DefaultLevels() Levels {
|
||||
@@ -29,11 +29,10 @@ func DefaultLevels() Levels {
|
||||
}
|
||||
}
|
||||
|
||||
// LevelString fetches the appropriate level string for the provided level, or "unknown"
|
||||
func (l Levels) LevelString(lvl LEVEL) string {
|
||||
str, ok := l[lvl]
|
||||
if !ok {
|
||||
return unknownLevel
|
||||
// Get fetches the level string for the provided value, or "unknown"
|
||||
func (l Levels) Get(lvl LEVEL) string {
|
||||
if str := l[int(lvl)]; str != "" {
|
||||
return str
|
||||
}
|
||||
return str
|
||||
return unknownLevel
|
||||
}
|
||||
|
70
vendor/codeberg.org/gruf/go-logger/logger.go
generated
vendored
70
vendor/codeberg.org/gruf/go-logger/logger.go
generated
vendored
@@ -2,6 +2,7 @@ package logger
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
@@ -55,7 +56,6 @@ func NewWith(lvl LEVEL, timestamp bool, fmt LogFormat, bufsize int64, out io.Wri
|
||||
Format: fmt,
|
||||
BufSize: bufsize,
|
||||
Output: out,
|
||||
pool: sync.Pool{},
|
||||
}
|
||||
|
||||
// Ensure clock running
|
||||
@@ -75,79 +75,113 @@ func NewWith(lvl LEVEL, timestamp bool, fmt LogFormat, bufsize int64, out io.Wri
|
||||
|
||||
// Entry returns a new Entry from the Logger's pool with background context
|
||||
func (l *Logger) Entry() *Entry {
|
||||
return l.pool.Get().(*Entry).WithContext(context.Background())
|
||||
entry, _ := l.pool.Get().(*Entry)
|
||||
entry.ctx = context.Background()
|
||||
return entry
|
||||
}
|
||||
|
||||
// Debug prints the provided arguments with the debug prefix
|
||||
func (l *Logger) Debug(a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(DEBUG).Hooks().Msg(a...)
|
||||
l.Log(DEBUG, a...)
|
||||
}
|
||||
|
||||
// Debugf prints the provided format string and arguments with the debug prefix
|
||||
func (l *Logger) Debugf(s string, a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(DEBUG).Hooks().Msgf(s, a...)
|
||||
l.Logf(DEBUG, s, a...)
|
||||
}
|
||||
|
||||
// Info prints the provided arguments with the info prefix
|
||||
func (l *Logger) Info(a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(INFO).Hooks().Msg(a...)
|
||||
l.Log(INFO, a...)
|
||||
}
|
||||
|
||||
// Infof prints the provided format string and arguments with the info prefix
|
||||
func (l *Logger) Infof(s string, a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(INFO).Hooks().Msgf(s, a...)
|
||||
l.Logf(INFO, s, a...)
|
||||
}
|
||||
|
||||
// Warn prints the provided arguments with the warn prefix
|
||||
func (l *Logger) Warn(a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(WARN).Hooks().Msg(a...)
|
||||
l.Log(WARN, a...)
|
||||
}
|
||||
|
||||
// Warnf prints the provided format string and arguments with the warn prefix
|
||||
func (l *Logger) Warnf(s string, a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(WARN).Hooks().Msgf(s, a...)
|
||||
l.Logf(WARN, s, a...)
|
||||
}
|
||||
|
||||
// Error prints the provided arguments with the error prefix
|
||||
func (l *Logger) Error(a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(ERROR).Hooks().Msg(a...)
|
||||
l.Log(ERROR, a...)
|
||||
}
|
||||
|
||||
// Errorf prints the provided format string and arguments with the error prefix
|
||||
func (l *Logger) Errorf(s string, a ...interface{}) {
|
||||
l.Entry().TimestampIf().Level(ERROR).Hooks().Msgf(s, a...)
|
||||
l.Logf(ERROR, s, a...)
|
||||
}
|
||||
|
||||
// Fatal prints provided arguments with the fatal prefix before exiting the program
|
||||
// with os.Exit(1)
|
||||
func (l *Logger) Fatal(a ...interface{}) {
|
||||
defer os.Exit(1)
|
||||
l.Entry().TimestampIf().Level(FATAL).Hooks().Msg(a...)
|
||||
l.Log(FATAL, a...)
|
||||
}
|
||||
|
||||
// Fatalf prints provided the provided format string and arguments with the fatal prefix
|
||||
// before exiting the program with os.Exit(1)
|
||||
func (l *Logger) Fatalf(s string, a ...interface{}) {
|
||||
defer os.Exit(1)
|
||||
l.Entry().TimestampIf().Level(FATAL).Hooks().Msgf(s, a...)
|
||||
l.Logf(FATAL, s, a...)
|
||||
}
|
||||
|
||||
// Log prints the provided arguments with the supplied log level
|
||||
// Log prints the provided arguments at the supplied log level
|
||||
func (l *Logger) Log(lvl LEVEL, a ...interface{}) {
|
||||
l.Entry().TimestampIf().Hooks().Msg(a...)
|
||||
if lvl >= l.Level {
|
||||
l.Entry().TimestampIf().Level(lvl).Hooks().Msg(a...)
|
||||
}
|
||||
}
|
||||
|
||||
// Logf prints the provided format string and arguments with the supplied log level
|
||||
// Logf prints the provided format string and arguments at the supplied log level
|
||||
func (l *Logger) Logf(lvl LEVEL, s string, a ...interface{}) {
|
||||
l.Entry().TimestampIf().Hooks().Msgf(s, a...)
|
||||
if lvl >= l.Level {
|
||||
l.Entry().TimestampIf().Level(lvl).Hooks().Msgf(s, a...)
|
||||
}
|
||||
}
|
||||
|
||||
// LogFields prints the provided fields formatted as key-value pairs at the supplied log level
|
||||
func (l *Logger) LogFields(lvl LEVEL, fields map[string]interface{}) {
|
||||
if lvl >= l.Level {
|
||||
l.Entry().TimestampIf().Level(lvl).Fields(fields).Hooks().Send()
|
||||
}
|
||||
}
|
||||
|
||||
// LogValues prints the provided values formatted as-so at the supplied log level
|
||||
func (l *Logger) LogValues(lvl LEVEL, a ...interface{}) {
|
||||
if lvl >= l.Level {
|
||||
l.Entry().TimestampIf().Level(lvl).Values(a...).Hooks().Send()
|
||||
}
|
||||
}
|
||||
|
||||
// Print simply prints provided arguments
|
||||
func (l *Logger) Print(a ...interface{}) {
|
||||
l.Entry().Hooks().Msg(a...)
|
||||
e := l.Entry().TimestampIf()
|
||||
fmt.Fprint(e.buf, a...)
|
||||
e.Send()
|
||||
}
|
||||
|
||||
// Printf simply prints provided the provided format string and arguments
|
||||
func (l *Logger) Printf(s string, a ...interface{}) {
|
||||
l.Entry().Hooks().Msgf(s, a...)
|
||||
e := l.Entry().TimestampIf()
|
||||
fmt.Fprintf(e.buf, s, a...)
|
||||
e.Send()
|
||||
}
|
||||
|
||||
// PrintFields prints the provided fields formatted as key-value pairs
|
||||
func (l *Logger) PrintFields(fields map[string]interface{}) {
|
||||
l.Entry().TimestampIf().Fields(fields).Send()
|
||||
}
|
||||
|
||||
// PrintValues prints the provided values formatted as-so
|
||||
func (l *Logger) PrintValues(a ...interface{}) {
|
||||
l.Entry().TimestampIf().Values(a...).Send()
|
||||
}
|
||||
|
Reference in New Issue
Block a user