update dependencies

This commit is contained in:
tsmethurst
2022-01-16 18:52:30 +01:00
parent 18047de666
commit 6f5ccf4355
55 changed files with 5674 additions and 1905 deletions

View File

@@ -1,9 +0,0 @@
MIT License
Copyright (c) 2021 gruf
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

View File

@@ -1,13 +0,0 @@
Fast levelled logging package with customizable formatting.
Supports logging in 2 modes:
- no locks, fastest possible logging, no guarantees for io.Writer thread safety
- mutex locks during writes, still far faster than standard library logger
Running without locks isn't likely to cause you any issues*, but if it does, you can wrap your `io.Writer` using `AddSafety()` when instantiating your new Logger. Even when running the benchmarks, this library has no printing issues without locks, so in most cases you'll be fine, but the safety is there if you need it.
*most logging libraries advertising high speeds are likely not performing mutex locks, which is why with this library you have the option to opt-in/out of them.
Note there are 2 uses of the unsafe package:
- safer interface nil value checks, uses similar logic to reflect package to check if the value in the internal fat pointer is nil
- casting a byte slice to string to allow sharing of similar byte and string methods, performs same logic as `strings.Builder{}.String()`

View File

@@ -1,21 +0,0 @@
package logger
import (
"sync"
"time"
"codeberg.org/gruf/go-nowish"
)
var (
clock = nowish.Clock{}
clockOnce = sync.Once{}
)
// startClock starts the global nowish clock.
func startClock() {
clockOnce.Do(func() {
clock.Start(time.Millisecond * 100)
clock.SetFormat("2006-01-02 15:04:05")
})
}

View File

@@ -1,107 +0,0 @@
package logger
import (
"os"
"sync"
)
var (
instance *Logger
instanceOnce = sync.Once{}
)
// Default returns the default Logger instance.
func Default() *Logger {
instanceOnce.Do(func() { instance = New(os.Stdout) })
return instance
}
// Debug prints the provided arguments with the debug prefix to the global Logger instance.
func Debug(a ...interface{}) {
Default().Debug(a...)
}
// Debugf prints the provided format string and arguments with the debug prefix to the global Logger instance.
func Debugf(s string, a ...interface{}) {
Default().Debugf(s, a...)
}
// Info prints the provided arguments with the info prefix to the global Logger instance.
func Info(a ...interface{}) {
Default().Info(a...)
}
// Infof prints the provided format string and arguments with the info prefix to the global Logger instance.
func Infof(s string, a ...interface{}) {
Default().Infof(s, a...)
}
// Warn prints the provided arguments with the warn prefix to the global Logger instance.
func Warn(a ...interface{}) {
Default().Warn(a...)
}
// Warnf prints the provided format string and arguments with the warn prefix to the global Logger instance.
func Warnf(s string, a ...interface{}) {
Default().Warnf(s, a...)
}
// Error prints the provided arguments with the error prefix to the global Logger instance.
func Error(a ...interface{}) {
Default().Error(a...)
}
// Errorf prints the provided format string and arguments with the error prefix to the global Logger instance.
func Errorf(s string, a ...interface{}) {
Default().Errorf(s, a...)
}
// Fatal prints the provided arguments with the fatal prefix to the global Logger instance before exiting the program with os.Exit(1).
func Fatal(a ...interface{}) {
Default().Fatal(a...)
}
// Fatalf prints the provided format string and arguments with the fatal prefix to the global Logger instance before exiting the program with os.Exit(1).
func Fatalf(s string, a ...interface{}) {
Default().Fatalf(s, a...)
}
// Log prints the provided arguments with the supplied log level to the global Logger instance.
func Log(lvl LEVEL, a ...interface{}) {
Default().Log(lvl, a...)
}
// Logf prints the provided format string and arguments with the supplied log level to the global Logger instance.
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...)
}
// Printf simply prints provided the provided format string and arguments to the global Logger instance.
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...)
}

View File

@@ -1,385 +0,0 @@
package logger
import (
"context"
"fmt"
"time"
"codeberg.org/gruf/go-bytes"
)
// Entry defines an entry in the log, it is NOT safe for concurrent use
type Entry struct {
ctx context.Context
lvl LEVEL
buf *bytes.Buffer
log *Logger
}
// Context returns the current set Entry context.Context
func (e *Entry) Context() context.Context {
return e.ctx
}
// WithContext updates Entry context value to the supplied
func (e *Entry) WithContext(ctx context.Context) *Entry {
e.ctx = ctx
return e
}
// Level appends the supplied level to the log entry, and sets the entry level.
// Please note this CAN be called and append log levels multiple times
func (e *Entry) Level(lvl LEVEL) *Entry {
e.log.Format.AppendLevel(e.buf, lvl)
e.buf.WriteByte(' ')
e.lvl = lvl
return e
}
// Timestamp appends the current timestamp to the log entry. Please note this
// CAN be called and append the timestamp multiple times
func (e *Entry) Timestamp() *Entry {
e.log.Format.AppendTimestamp(e.buf, clock.NowFormat())
e.buf.WriteByte(' ')
return e
}
// TimestampIf performs Entry.Timestamp() only IF timestamping is enabled for the Logger.
// Please note this CAN be called multiple times
func (e *Entry) TimestampIf() *Entry {
if e.log.Timestamp {
e.Timestamp()
}
return e
}
// Hooks applies currently set Hooks to the Entry. Please note this CAN be
// called and perform the Hooks multiple times
func (e *Entry) Hooks() *Entry {
for _, hook := range e.log.Hooks {
hook.Do(e)
}
return e
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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
}
// 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.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 {
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 {
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
}
// 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
}
// Msg appends the fmt.Sprint() formatted final message to the log and calls .Send()
func (e *Entry) Msg(a ...interface{}) {
e.log.Format.AppendMsg(e.buf, a...)
e.Send()
}
// Msgf appends the fmt.Sprintf() formatted final message to the log and calls .Send()
func (e *Entry) Msgf(s string, a ...interface{}) {
e.log.Format.AppendMsgf(e.buf, s, a...)
e.Send()
}
// Send triggers write of the log entry, skipping if the entry's log LEVEL
// is below the currently set Logger level, and releases the Entry back to
// the Logger's Entry pool. So it is NOT safe to continue using this Entry
// object after calling .Send(), .Msg() or .Msgf()
func (e *Entry) Send() {
// If nothing to do, return
if e.lvl < e.log.Level || e.buf.Len() < 1 {
e.reset()
return
}
// Ensure a final new line
if e.buf.B[e.buf.Len()-1] != '\n' {
e.buf.WriteByte('\n')
}
// Write, reset and release
e.log.Output.Write(e.buf.B)
e.reset()
}
func (e *Entry) reset() {
// Reset all
e.ctx = nil
e.buf.Reset()
e.lvl = unset
// Release to pool
e.log.pool.Put(e)
}

View File

@@ -1,87 +0,0 @@
package logger
import (
"time"
"codeberg.org/gruf/go-bytes"
)
// 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 {
// 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 timestamp string as key-value pair to the log buffer
AppendTimestamp(buf *bytes.Buffer, fmtNow string)
// AppendValue appends given interface formatted as value to the log buffer
AppendValue(buf *bytes.Buffer, value interface{})
// AppendByte appends given byte value to the log buffer
AppendByte(buf *bytes.Buffer, value byte)
// AppendBytes appends given byte slice value to the log buffer
AppendBytes(buf *bytes.Buffer, value []byte)
// AppendString appends given string value to the log buffer
AppendString(buf *bytes.Buffer, value string)
// AppendStrings appends given string slice value to the log buffer
AppendStrings(buf *bytes.Buffer, value []string)
// AppendBool appends given bool value to the log buffer
AppendBool(buf *bytes.Buffer, value bool)
// AppendBools appends given bool slice value to the log buffer
AppendBools(buf *bytes.Buffer, value []bool)
// AppendInt appends given int value to the log buffer
AppendInt(buf *bytes.Buffer, value int)
// AppendInts appends given int slice value to the log buffer
AppendInts(buf *bytes.Buffer, value []int)
// AppendUint appends given uint value to the log buffer
AppendUint(buf *bytes.Buffer, value uint)
// AppendUints appends given uint slice value to the log buffer
AppendUints(buf *bytes.Buffer, value []uint)
// AppendFloat appends given float value to the log buffer
AppendFloat(buf *bytes.Buffer, value float64)
// AppendFloats appends given float slice value to the log buffer
AppendFloats(buf *bytes.Buffer, value []float64)
// AppendTime appends given time value to the log buffer
AppendTime(buf *bytes.Buffer, value time.Time)
// AppendTimes appends given time slice value to the log buffer
AppendTimes(buf *bytes.Buffer, value []time.Time)
// AppendDuration appends given duration value to the log buffer
AppendDuration(buf *bytes.Buffer, 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{})
// AppendMsgf appends given msg format string as key-value pair to the log buffer using fmt.Sprintf(...) formatting
AppendMsgf(buf *bytes.Buffer, s string, a ...interface{})
}

View File

@@ -1,914 +0,0 @@
package logger
import (
stdfmt "fmt"
"reflect"
"strconv"
"time"
"unsafe"
"codeberg.org/gruf/go-bytes"
)
// DefaultTextFormat is the default TextFormat instance
var DefaultTextFormat = TextFormat{
Strict: false,
Verbose: false,
MaxDepth: 10,
Levels: DefaultLevels(),
}
// 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
}
// 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{
flags: flags,
curd: 0,
maxd: f.MaxDepth,
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.Get(lvl))
return
}
// Write level string
buf.WriteByte('[')
buf.WriteString(f.Levels.Get(lvl))
buf.WriteByte(']')
}
func (f TextFormat) AppendTimestamp(buf *bytes.Buffer, now string) {
if f.Strict {
// Strict format, use key and quote
buf.WriteString(`time=`)
appendString(f.fmt(buf), now)
return
}
// Write time as-is
buf.WriteString(now)
}
func (f TextFormat) AppendValue(buf *bytes.Buffer, value interface{}) {
appendIfaceOrRValue(f.fmt(buf).SetIsKey(false), value)
}
func (f TextFormat) AppendByte(buf *bytes.Buffer, value byte) {
appendByte(f.fmt(buf), value)
}
func (f TextFormat) AppendBytes(buf *bytes.Buffer, value []byte) {
appendBytes(f.fmt(buf), value)
}
func (f TextFormat) AppendString(buf *bytes.Buffer, value string) {
appendString(f.fmt(buf), value)
}
func (f TextFormat) AppendStrings(buf *bytes.Buffer, value []string) {
appendStringSlice(f.fmt(buf), value)
}
func (f TextFormat) AppendBool(buf *bytes.Buffer, value bool) {
appendBool(f.fmt(buf), value)
}
func (f TextFormat) AppendBools(buf *bytes.Buffer, value []bool) {
appendBoolSlice(f.fmt(buf), value)
}
func (f TextFormat) AppendInt(buf *bytes.Buffer, value int) {
appendInt(f.fmt(buf), int64(value))
}
func (f TextFormat) AppendInts(buf *bytes.Buffer, value []int) {
appendIntSlice(f.fmt(buf), value)
}
func (f TextFormat) AppendUint(buf *bytes.Buffer, value uint) {
appendUint(f.fmt(buf), uint64(value))
}
func (f TextFormat) AppendUints(buf *bytes.Buffer, value []uint) {
appendUintSlice(f.fmt(buf), value)
}
func (f TextFormat) AppendFloat(buf *bytes.Buffer, value float64) {
appendFloat(f.fmt(buf), value)
}
func (f TextFormat) AppendFloats(buf *bytes.Buffer, value []float64) {
appendFloatSlice(f.fmt(buf), value)
}
func (f TextFormat) AppendTime(buf *bytes.Buffer, value time.Time) {
appendTime(f.fmt(buf), value)
}
func (f TextFormat) AppendTimes(buf *bytes.Buffer, value []time.Time) {
appendTimeSlice(f.fmt(buf), value)
}
func (f TextFormat) AppendDuration(buf *bytes.Buffer, value time.Duration) {
appendDuration(f.fmt(buf), value)
}
func (f TextFormat) AppendDurations(buf *bytes.Buffer, value []time.Duration) {
appendDurationSlice(f.fmt(buf), value)
}
func (f TextFormat) AppendMsg(buf *bytes.Buffer, a ...interface{}) {
if f.Strict {
// Strict format, use key and quote
buf.WriteString(`msg=`)
buf.B = strconv.AppendQuote(buf.B, stdfmt.Sprint(a...))
return
}
// Write message as-is
stdfmt.Fprint(buf, a...)
}
func (f TextFormat) AppendMsgf(buf *bytes.Buffer, s string, a ...interface{}) {
if f.Strict {
// Strict format, use key and quote
buf.WriteString(`msg=`)
buf.B = strconv.AppendQuote(buf.B, stdfmt.Sprintf(s, a...))
return
}
// Write message as-is
stdfmt.Fprintf(buf, s, a...)
}
// format is the object passed among the append___ formatting functions
type format struct {
flags uint8 // 'isKey' and 'verbose' flags
drefs uint8 // current value deref count
curd uint8 // current depth
maxd uint8 // maximum depth
buf *bytes.Buffer // out buffer
}
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 f.curd >= f.maxd
}
// Derefs returns no. times current value has been dereferenced.
func (f format) Derefs() uint8 {
return f.drefs
}
// 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{
flags: flags,
drefs: f.drefs,
curd: f.curd,
maxd: f.maxd,
buf: f.buf,
}
}
// IncrDepth returns format instance with depth incremented.
func (f format) IncrDepth() format {
return format{
flags: f.flags,
drefs: f.drefs,
curd: f.curd + 1,
maxd: f.maxd,
buf: f.buf,
}
}
// IncrDerefs returns format instance with dereference count incremented.
func (f format) IncrDerefs() format {
return format{
flags: f.flags,
drefs: f.drefs + 1,
curd: f.curd,
maxd: f.maxd,
buf: f.buf,
}
}
// appendType appends a type using supplied type str.
func appendType(fmt format, t string) {
for i := uint8(0); i < fmt.Derefs(); i++ {
fmt.buf.WriteByte('*')
}
fmt.buf.WriteString(t)
}
// appendNilType writes nil to buf, type included if verbose.
func appendNilType(fmt format, t string) {
if fmt.Verbose() {
fmt.buf.WriteByte('(')
appendType(fmt, 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('(')
appendType(fmt, 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('(')
appendType(fmt, 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)
}
// appendBytes writes a quoted byte slice to buf
func appendBytes(fmt format, b []byte) {
if !fmt.IsKey() && b == nil {
// Values CAN be nil formatted
appendNilType(fmt, `[]byte`)
} else {
// unsafe cast as string to prevent reallocation
appendString(fmt, *(*string)(unsafe.Pointer(&b)))
}
}
// appendString writes an escaped, double-quoted string to buf
func appendString(fmt format, s string) {
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
fmt.buf.WriteString(s)
}
// appendStringSlice writes a slice of strings to buf
func appendStringSlice(fmt format, s []string) {
// Check for nil slice
if s == nil {
appendNilType(fmt, `[]string`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, s := range s {
appendString(fmt.SetIsKey(false), s)
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(s) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendBool writes a formatted bool to buf
func appendBool(fmt format, b bool) {
fmt.buf.B = strconv.AppendBool(fmt.buf.B, b)
}
// appendBool writes a slice of formatted bools to buf
func appendBoolSlice(fmt format, b []bool) {
// Check for nil slice
if b == nil {
appendNilType(fmt, `[]bool`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, b := range b {
appendBool(fmt, b)
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(b) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendInt writes a formatted int to buf
func appendInt(fmt format, i int64) {
fmt.buf.B = strconv.AppendInt(fmt.buf.B, i, 10)
}
// appendIntSlice writes a slice of formatted int to buf
func appendIntSlice(fmt format, i []int) {
// Check for nil slice
if i == nil {
appendNilType(fmt, `[]int`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, i := range i {
appendInt(fmt, int64(i))
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(i) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendUint writes a formatted uint to buf
func appendUint(fmt format, u uint64) {
fmt.buf.B = strconv.AppendUint(fmt.buf.B, u, 10)
}
// appendUintSlice writes a slice of formatted uint to buf
func appendUintSlice(fmt format, u []uint) {
// Check for nil slice
if u == nil {
appendNilType(fmt, `[]uint`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, u := range u {
appendUint(fmt, uint64(u))
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(u) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendFloat writes a formatted float to buf
func appendFloat(fmt format, f float64) {
fmt.buf.B = strconv.AppendFloat(fmt.buf.B, f, 'G', -1, 64)
}
// appendFloatSlice writes a slice formatted floats to buf
func appendFloatSlice(fmt format, f []float64) {
// Check for nil slice
if f == nil {
appendNilType(fmt, `[]float64`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, f := range f {
appendFloat(fmt, f)
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(f) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendTime writes a formatted, quoted time string to buf
func appendTime(fmt format, t time.Time) {
appendString(fmt.SetIsKey(true), t.Format(time.RFC1123))
}
// appendTimeSlice writes a slice of formatted time strings to buf
func appendTimeSlice(fmt format, t []time.Time) {
// Check for nil slice
if t == nil {
appendNilType(fmt, `[]time.Time`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, t := range t {
appendString(fmt.SetIsKey(true), t.Format(time.RFC1123))
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(t) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendDuration writes a formatted, quoted duration string to buf
func appendDuration(fmt format, d time.Duration) {
appendString(fmt.SetIsKey(true), d.String())
}
// appendDurationSlice writes a slice of formatted, quoted duration strings to buf
func appendDurationSlice(fmt format, d []time.Duration) {
// Check for nil slice
if d == nil {
appendNilType(fmt, `[]time.Duration`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, d := range d {
appendString(fmt.SetIsKey(true), d.String())
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(d) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendComplex writes a formatted complex128 to buf
func appendComplex(fmt format, c complex128) {
appendFloat(fmt, real(c))
fmt.buf.WriteByte('+')
appendFloat(fmt, imag(c))
fmt.buf.WriteByte('i')
}
// appendComplexSlice writes a slice of formatted complex128s to buf
func appendComplexSlice(fmt format, c []complex128) {
// Check for nil slice
if c == nil {
appendNilType(fmt, `[]complex128`)
return
}
fmt.buf.WriteByte('[')
// Write elements
for _, c := range c {
appendComplex(fmt, c)
fmt.buf.WriteByte(',')
}
// Drop last comma
if len(c) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// notNil will safely check if 'v' is nil without dealing with weird Go interface nil bullshit.
func notNil(i interface{}) bool {
// cast to get fat pointer
e := *(*struct {
typeOf unsafe.Pointer // ignored
valueOf unsafe.Pointer
})(unsafe.Pointer(&i))
// check if value part is nil
return (e.valueOf != nil)
}
// appendIfaceOrRValueNext performs appendIfaceOrRValue checking + incr depth
func appendIfaceOrRValueNext(fmt format, i interface{}) {
// Check we haven't hit max
if fmt.AtMaxDepth() {
fmt.buf.WriteString("...")
return
}
// Incr the depth
fmt = fmt.IncrDepth()
// 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))
}
}
// appendValueOrIfaceNext performs appendRValueOrIface checking + incr depth
func appendRValueOrIfaceNext(fmt format, v reflect.Value) {
// Check we haven't hit max
if fmt.AtMaxDepth() {
fmt.buf.WriteString("...")
return
}
// Incr the depth
fmt = fmt.IncrDepth()
// 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)
}
}
// appendIface parses and writes a formatted interface value to buf
func appendIface(fmt format, i interface{}) bool {
switch i := i.(type) {
case nil:
fmt.buf.WriteString(`nil`)
case byte:
appendByte(fmt, i)
case []byte:
appendBytes(fmt, i)
case string:
appendString(fmt, i)
case []string:
appendStringSlice(fmt, i)
case int:
appendInt(fmt, int64(i))
case int8:
appendInt(fmt, int64(i))
case int16:
appendInt(fmt, int64(i))
case int32:
appendInt(fmt, int64(i))
case int64:
appendInt(fmt, i)
case []int:
appendIntSlice(fmt, i)
case uint:
appendUint(fmt, uint64(i))
case uint16:
appendUint(fmt, uint64(i))
case uint32:
appendUint(fmt, uint64(i))
case uint64:
appendUint(fmt, i)
case []uint:
appendUintSlice(fmt, i)
case float32:
appendFloat(fmt, float64(i))
case float64:
appendFloat(fmt, i)
case []float64:
appendFloatSlice(fmt, i)
case bool:
appendBool(fmt, i)
case []bool:
appendBoolSlice(fmt, i)
case time.Time:
appendTime(fmt, i)
case []time.Time:
appendTimeSlice(fmt, i)
case time.Duration:
appendDuration(fmt, i)
case []time.Duration:
appendDurationSlice(fmt, i)
case complex64:
appendComplex(fmt, complex128(i))
case complex128:
appendComplex(fmt, i)
case []complex128:
appendComplexSlice(fmt, i)
case map[string]interface{}:
appendIfaceMap(fmt, i)
case error:
if notNil(i) /* use safer nil check */ {
appendString(fmt, i.Error())
} else {
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:
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())
}
default:
return false // could not handle
}
return true
}
// appendReflectValue will safely append a reflected value
func appendRValue(fmt format, v reflect.Value) {
switch v.Kind() {
case reflect.Float32, reflect.Float64:
appendFloat(fmt, v.Float())
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
appendInt(fmt, v.Int())
case reflect.Uint8:
appendByte(fmt, uint8(v.Uint()))
case reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
appendUint(fmt, v.Uint())
case reflect.Bool:
appendBool(fmt, v.Bool())
case reflect.Array:
appendArrayType(fmt, v)
case reflect.Slice:
appendSliceType(fmt, v)
case reflect.Map:
appendMapType(fmt, v)
case reflect.Struct:
appendStructType(fmt, v)
case reflect.Ptr:
if v.IsNil() {
appendNilRValue(fmt, v)
} else {
appendRValue(fmt.IncrDerefs(), v.Elem())
}
case reflect.UnsafePointer:
fmt.buf.WriteString("(unsafe.Pointer)")
fmt.buf.WriteByte('(')
if u := v.Pointer(); u != 0 {
fmt.buf.WriteString("0x")
fmt.buf.B = strconv.AppendUint(fmt.buf.B, uint64(u), 16)
} else {
fmt.buf.WriteString(`nil`)
}
fmt.buf.WriteByte(')')
case reflect.Uintptr:
fmt.buf.WriteString("(uintptr)")
fmt.buf.WriteByte('(')
if u := v.Uint(); u != 0 {
fmt.buf.WriteString("0x")
fmt.buf.B = strconv.AppendUint(fmt.buf.B, u, 16)
} else {
fmt.buf.WriteString(`nil`)
}
fmt.buf.WriteByte(')')
case reflect.String:
appendString(fmt, v.String())
case reflect.Complex64, reflect.Complex128:
appendComplex(fmt, v.Complex())
case reflect.Func, reflect.Chan, reflect.Interface:
if v.IsNil() {
appendNilRValue(fmt, v)
} else {
fmt.buf.WriteString(v.String())
}
default:
fmt.buf.WriteString(v.String())
}
}
// appendIfaceMap writes a map of key-value pairs (as a set of fields) to buf
func appendIfaceMap(fmt format, v map[string]interface{}) {
// Catch nil map
if v == nil {
appendNilType(fmt, `map[string]interface{}`)
return
}
fmt.buf.WriteByte('{')
// Write map pairs!
for key, value := range v {
appendString(fmt.SetIsKey(true), key)
fmt.buf.WriteByte('=')
appendIfaceOrRValueNext(fmt.SetIsKey(false), value)
fmt.buf.WriteByte(' ')
}
// Drop last space
if len(v) > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte('}')
}
// appendArrayType writes an array of unknown type (parsed by reflection) to buf, unlike appendSliceType does NOT catch nil slice
func appendArrayType(fmt format, v reflect.Value) {
// get no. elements
n := v.Len()
fmt.buf.WriteByte('[')
// Write values
for i := 0; i < n; i++ {
appendRValueOrIfaceNext(fmt.SetIsKey(false), v.Index(i))
fmt.buf.WriteByte(',')
}
// Drop last comma
if n > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte(']')
}
// appendSliceType writes a slice of unknown type (parsed by reflection) to buf
func appendSliceType(fmt format, v reflect.Value) {
if v.IsNil() {
appendNilRValue(fmt, v)
} else {
appendArrayType(fmt, v)
}
}
// appendMapType writes a map of unknown types (parsed by reflection) to buf
func appendMapType(fmt format, v reflect.Value) {
// Catch nil map
if v.IsNil() {
appendNilRValue(fmt, v)
return
}
// Get a map iterator
r := v.MapRange()
n := v.Len()
fmt.buf.WriteByte('{')
// Iterate pairs
for r.Next() {
appendRValueOrIfaceNext(fmt.SetIsKey(true), r.Key())
fmt.buf.WriteByte('=')
appendRValueOrIfaceNext(fmt.SetIsKey(false), r.Value())
fmt.buf.WriteByte(' ')
}
// Drop last space
if n > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte('}')
}
// appendStructType writes a struct (as a set of key-value fields) to buf
func appendStructType(fmt format, v reflect.Value) {
// Get value type & no. fields
t := v.Type()
n := v.NumField()
w := 0
// If verbose, append the type
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(fmt.SetIsKey(false), vfield)
} else {
// This is an exported field!
appendRValueOrIfaceNext(fmt.SetIsKey(false), vfield)
}
// Iter written count
fmt.buf.WriteByte(' ')
w++
}
// Drop last space
if w > 0 {
fmt.buf.Truncate(1)
}
fmt.buf.WriteByte('}')
}
// containsSpaceOrTab checks if "s" contains space or tabs
func containsSpaceOrTab(s string) bool {
for _, r := range s {
if r == ' ' || r == '\t' {
return true
}
}
return false
}

View File

@@ -1,13 +0,0 @@
package logger
// Hook defines a log Entry modifier
type Hook interface {
Do(*Entry)
}
// HookFunc is a simple adapter to allow functions to satisfy the Hook interface
type HookFunc func(*Entry)
func (hook HookFunc) Do(entry *Entry) {
hook(entry)
}

View File

@@ -1,38 +0,0 @@
package logger
// LEVEL defines a level of logging
type LEVEL uint8
// Available levels of logging.
const (
unset LEVEL = ^LEVEL(0)
DEBUG LEVEL = 5
INFO LEVEL = 10
WARN LEVEL = 15
ERROR LEVEL = 20
FATAL LEVEL = 25
)
var unknownLevel = "unknown"
// Levels defines a mapping of log LEVELs to formatted level strings
type Levels [^LEVEL(0)]string
// DefaultLevels returns the default set of log levels
func DefaultLevels() Levels {
return Levels{
DEBUG: "DEBUG",
INFO: "INFO",
WARN: "WARN",
ERROR: "ERROR",
FATAL: "FATAL",
}
}
// 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 unknownLevel
}

View File

@@ -1,187 +0,0 @@
package logger
import (
"context"
"fmt"
"io"
"os"
"sync"
"sync/atomic"
"codeberg.org/gruf/go-bytes"
)
type Logger struct {
// Hooks defines a list of hooks which are called before an entry
// is written. This should NOT be modified while the Logger is in use
Hooks []Hook
// Level is the current log LEVEL, entries at level below the
// currently set level will not be output. This should NOT
// be modified while the Logger is in use
Level LEVEL
// Timestamp defines whether to automatically append timestamps
// to entries written via Logger convience methods and specifically
// Entry.TimestampIf(). This should NOT be modified while Logger in use
Timestamp bool
// Format is the log entry LogFormat to use. This should NOT
// be modified while the Logger is in use
Format LogFormat
// BufferSize is the Entry buffer size to use when allocating
// new Entry objects. This should be modified atomically
BufSize int64
// Output is the log's output writer. This should NOT be
// modified while the Logger is in use
Output io.Writer
// entry pool
pool sync.Pool
}
// New returns a new Logger instance with defaults
func New(out io.Writer) *Logger {
return NewWith(0 /* all */, true, DefaultTextFormat, 512, out)
}
// NewWith returns a new Logger instance with supplied configuration
func NewWith(lvl LEVEL, timestamp bool, fmt LogFormat, bufsize int64, out io.Writer) *Logger {
// Create new logger object
log := &Logger{
Level: lvl,
Timestamp: timestamp,
Format: fmt,
BufSize: bufsize,
Output: out,
}
// Ensure clock running
startClock()
// Set-up logger Entry pool
log.pool.New = func() interface{} {
return &Entry{
lvl: unset,
buf: &bytes.Buffer{B: make([]byte, 0, atomic.LoadInt64(&log.BufSize))},
log: log,
}
}
return log
}
// Entry returns a new Entry from the Logger's pool with background context
func (l *Logger) Entry() *Entry {
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.Log(DEBUG, a...)
}
// Debugf prints the provided format string and arguments with the debug prefix
func (l *Logger) Debugf(s string, a ...interface{}) {
l.Logf(DEBUG, s, a...)
}
// Info prints the provided arguments with the info prefix
func (l *Logger) Info(a ...interface{}) {
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.Logf(INFO, s, a...)
}
// Warn prints the provided arguments with the warn prefix
func (l *Logger) Warn(a ...interface{}) {
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.Logf(WARN, s, a...)
}
// Error prints the provided arguments with the error prefix
func (l *Logger) Error(a ...interface{}) {
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.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.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.Logf(FATAL, s, a...)
}
// Log prints the provided arguments at the supplied log level
func (l *Logger) Log(lvl LEVEL, a ...interface{}) {
if lvl >= l.Level {
l.Entry().TimestampIf().Level(lvl).Hooks().Msg(a...)
}
}
// Logf prints the provided format string and arguments at the supplied log level
func (l *Logger) Logf(lvl LEVEL, s string, a ...interface{}) {
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{}) {
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{}) {
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()
}

View File

@@ -1,29 +0,0 @@
package logger
import (
"io"
"io/ioutil"
"sync"
)
// AddSafety wraps an io.Writer to provide mutex locking protection
func AddSafety(w io.Writer) io.Writer {
if w == nil {
w = ioutil.Discard
} else if sw, ok := w.(*safeWriter); ok {
return sw
}
return &safeWriter{wr: w}
}
// safeWriter wraps an io.Writer to provide mutex locking on write
type safeWriter struct {
wr io.Writer
mu sync.Mutex
}
func (w *safeWriter) Write(b []byte) (int, error) {
w.mu.Lock()
defer w.mu.Unlock()
return w.wr.Write(b)
}