log

package
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 1, 2022 License: MIT Imports: 8 Imported by: 12

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Out io.Writer = os.Stderr
)

Functions

func Configure

func Configure(config Config) error

func KvParse

func KvParse(line string) map[string]string

func KvParseAll

func KvParseAll(l string) []map[string]string

Types

type Config

type Config struct {
	Level    string   `json:"level"`
	PoolSize uint16   `json:"pool_size"`
	Format   string   `json:"format"`
	KV       KvConfig `json:"kv"`
}

type Factory

type Factory func(p *Pool) Logger

func KvFactory

func KvFactory(maxSize uint32) Factory

type Field

type Field struct {
	// contains filtered or unexported fields
}

func NewField

func NewField() *Field

func (*Field) Finalize

func (f *Field) Finalize() Field

just return Field so that it can be used in chaining

func (*Field) Int

func (f *Field) Int(key string, value int) *Field

func (*Field) KV

func (f *Field) KV() []byte

func (*Field) String

func (f *Field) String(key string, value string) *Field

type KvConfig

type KvConfig struct {
	MaxSize uint32 `json:"max_size"`
}

type KvLogger

type KvLogger struct {
	// contains filtered or unexported fields
}

func NewKvLogger

func NewKvLogger(maxSize uint32, pool *Pool) *KvLogger

func (*KvLogger) Bytes

func (l *KvLogger) Bytes() []byte

Get the bytes from the logger. This is only valid before Log is called (after log is called, you'll get an empty slice). Only really useful for testing.

func (*KvLogger) Err

func (l *KvLogger) Err(err error) Logger

Add a field (key=value) where value is an error

func (*KvLogger) Error

func (l *KvLogger) Error(ctx string) Logger

Log an error-level message. Every message must have a [hopefully] unique context

func (*KvLogger) Fatal

func (l *KvLogger) Fatal(ctx string) Logger

Log an fatal-level message. Every message must have a [hopefully] unique context

func (*KvLogger) Field

func (l *KvLogger) Field(field Field) Logger

func (*KvLogger) Fixed

func (l *KvLogger) Fixed()

Logger will _always_ include this data. Meant to be used with the Field builder. Even once released to the pool and re-checked out, this data will still be in the logger. For checkout-specific data, see MultiUse().

func (*KvLogger) Info

func (l *KvLogger) Info(ctx string) Logger

Log an info-level message. Every message must have a [hopefully] unique context

func (*KvLogger) Int

func (l *KvLogger) Int(key string, value int) Logger

Add a field (key=value) where value is an int

func (*KvLogger) Int64

func (l *KvLogger) Int64(key string, value int64) Logger

Add a field (key=value) where value is an int

func (*KvLogger) Log

func (l *KvLogger) Log()

Write the log to our globally configured writer

func (*KvLogger) LogTo

func (l *KvLogger) LogTo(out io.Writer)

func (*KvLogger) MultiUse

func (l *KvLogger) MultiUse() Logger

Similar to Fixed, but exists only while checked out

func (*KvLogger) Release

func (l *KvLogger) Release()

func (*KvLogger) Reset

func (l *KvLogger) Reset()

func (*KvLogger) String

func (l *KvLogger) String(key string, value string) Logger

Add a field (key=value) where value is a string

func (*KvLogger) Warn

func (l *KvLogger) Warn(ctx string) Logger

Log an warn-level message. Every message must have a [hopefully] unique context

type Level

type Level uint8
const (
	INFO Level = iota
	WARN
	ERROR
	FATAL
	NONE
)

type Logger

type Logger interface {
	// Actually log the data to the configured output
	// If the logger was not configured for MultiUse, this
	// must release the logger back to the pool (if any).
	Log()

	// Log to the specific writer
	LogTo(io.Writer)

	// Resets the logger, without releasing it back to the pool.
	// Reset must not erased any fixed data
	Reset()

	// Releases the logger back to the pool, if one is configured.
	Release()

	// Gets the log data
	Bytes() []byte

	// Set the level and context for a new log entry
	Info(ctx string) Logger
	Warn(ctx string) Logger
	Error(ctx string) Logger
	Fatal(ctx string) Logger

	// Log an error
	Err(err error) Logger

	// Any data already in the logger will be including in _every_
	// message ever generated from this logger. This data survives both
	// a reset and a release.
	// As an example of where this could be used is a project owned loggers
	// in which case these loggers could have the project_id always included
	// in any log entry.
	Fixed()

	// Any data already in the logger will be including in _every_
	// message generated until the logger is released. This data survives a
	// a reset but not a release.
	// This is meant to be used for a request-owned logger where every entry
	// logged with the logger has request_id.
	MultiUse() Logger

	// Add an int value to the current entry
	Int(key string, value int) Logger

	// Add an int64 value to the current entry
	Int64(key string, value int64) Logger

	// Add an string value to the current entry
	String(key string, value string) Logger

	// Log a field
	Field(field Field) Logger
}

func Checkout

func Checkout() Logger

func Error

func Error(ctx string) Logger

func Fatal

func Fatal(ctx string) Logger

func Info

func Info(ctx string) Logger

func Warn

func Warn(ctx string) Logger

type Noop

type Noop struct {
}

func (Noop) Bytes

func (_ Noop) Bytes() []byte

func (Noop) Err

func (n Noop) Err(err error) Logger

func (Noop) Error

func (n Noop) Error(ctx string) Logger

func (Noop) Fatal

func (n Noop) Fatal(ctx string) Logger

func (Noop) Field

func (n Noop) Field(field Field) Logger

func (Noop) Fixed

func (n Noop) Fixed()

func (Noop) Info

func (n Noop) Info(ctx string) Logger

func (Noop) Int

func (n Noop) Int(key string, value int) Logger

func (Noop) Int64

func (n Noop) Int64(key string, value int64) Logger

func (Noop) Log

func (_ Noop) Log()

func (Noop) LogTo

func (_ Noop) LogTo(io.Writer)

func (Noop) MultiUse

func (n Noop) MultiUse() Logger

func (Noop) Release

func (_ Noop) Release()

func (Noop) Reset

func (_ Noop) Reset()

func (Noop) String

func (n Noop) String(key string, value string) Logger

func (Noop) Warn

func (n Noop) Warn(ctx string) Logger

type Pool

type Pool struct {
	// contains filtered or unexported fields
}

func NewPool

func NewPool(count uint16, level Level, factory Factory, field *Field) *Pool

func (*Pool) Checkout

func (p *Pool) Checkout() Logger

func (*Pool) Depleted

func (p *Pool) Depleted() uint64

func (*Pool) Error

func (p *Pool) Error(ctx string) Logger

func (*Pool) Fatal

func (p *Pool) Fatal(ctx string) Logger

func (*Pool) Info

func (p *Pool) Info(ctx string) Logger

func (*Pool) Len

func (p *Pool) Len() int

func (*Pool) Warn

func (p *Pool) Warn(ctx string) Logger

type StructuredError

type StructuredError struct {
	Code int            `json:"code"`
	Err  error          `json:"err"`
	Data map[string]any `json:"data"`
}

An error that's designed to be logged in a more structured manner

func Err

func Err(code int, err error) *StructuredError

func Errf

func Errf(code int, format string, args ...any) *StructuredError

func (StructuredError) Error

func (e StructuredError) Error() string

func (*StructuredError) Int

func (e *StructuredError) Int(key string, value int) *StructuredError

func (*StructuredError) String

func (e *StructuredError) String(key string, value string) *StructuredError

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL