log

package
v0.0.1-beta Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2023 License: GPL-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Caller

type Caller struct {
	File string `json:"file"`
	Line int    `json:"line"`
}

type DebugFn

type DebugFn func(msg string, opt ...Option)

type Driver

type Driver interface {
	Send(l *Entry)

	// PreLog returns a function that is called before each logging call
	// is made. This is only useful if the driver needs to find out the
	// caller and has a way of skipping some of the caller with a helper
	// function like `testing.T`.Helper(). Since the logger itself will get
	// information about the caller into log entries this is usually
	// unnecessary, so the function may return nil.
	//
	// An example of a driver that needs this helper is `logtotest`, which
	// sends log messages to a `testing.T` object, allowing the logged
	// data to be shown as part of the test, and automatically failing tests
	// that produce an error log. This loggers return `t.Helper`, and as
	// a result, the test log messages will point to the code that called
	// the logger, not to the log library itself.
	PreLog() func()
}

type Entry

type Entry struct {
	Timestamp time.Time
	Tags      map[string]any
	Message   string
	Caller    caller
	Severity  Severity
}

type Logger

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

func New

func New(d Driver, opt ...Option) *Logger

func (*Logger) D

func (l *Logger) D(lf func(lg DebugFn))

D sends a debug log message. This function is designed to allow close-to-nil overhead for debug log messages when the debug log is disabled.

Usage:

logger.D(func(lg log.DebugFn){
  msg := computeSomethingExpensive()
  lg(msg)
})

Since the callback function will not be executed if debug level is not enabled, its cost can be avoided in production, while still keeping the ability of debugging issues when they happen.

func (*Logger) E

func (l *Logger) E(msg string, opt ...Option)

func (*Logger) I

func (l *Logger) I(msg string, opt ...Option)

func (*Logger) SubLogger

func (l *Logger) SubLogger(opts ...Option) *Logger

func (*Logger) W

func (l *Logger) W(msg string, opt ...Option)

type Option

type Option func(*options)

func WithDebugEnabledFn

func WithDebugEnabledFn(enabledFn func() bool) Option

WithDebugEnabledFn allows providing a function that will be invoked to determine if debug log messages should be logged. Might be called concurrently, and must be fast.

func WithDuration

func WithDuration(key string, val time.Duration) Option

func WithError

func WithError(err error) Option

func WithInt

func WithInt(key string, val int) Option

func WithPrefix

func WithPrefix(prefix string) Option

WithPrefix can be used on a logger to configure it to include a prefix on all messages.

If the prefix is "Foo" and a message "Bar" is send to a log, then the resulting log message will be "Foo: Bar".

func WithString

func WithString(key, val string) Option

type Severity

type Severity int
const (
	Debug Severity = iota
	Info
	Warn
	Error
)

func (Severity) String

func (s Severity) String() string

Directories

Path Synopsis
Package niltarget is a log driver that does nothing, dropping all messages.
Package niltarget is a log driver that does nothing, dropping all messages.
Package testtarget is a log driver that sends log messages to a go test.
Package testtarget is a log driver that sends log messages to a go test.
Package texttarget is a log driver that encodes log messages as text and writes log messages to a provided writer.
Package texttarget is a log driver that encodes log messages as text and writes log messages to a provided writer.

Jump to

Keyboard shortcuts

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