log

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: May 21, 2023 License: MIT Imports: 11 Imported by: 1

README

GoDoc

Shared utils and helpers.

Documentation

Index

Constants

View Source
const DebugCallerKey = "loc"
View Source
const ErrorKey = "error"
View Source
const LevelKey = "level"
View Source
const MsgKey = "msg"
View Source
const PrefixKey = "prefix"

Variables

This section is empty.

Functions

func Add

func Add(args ...Arg)

func Debug

func Debug(args ...Arg)

func Error

func Error(err error, args ...Arg) bool

func Info

func Info(args ...Arg)

func Log

func Log(level Level, args ...Arg)

func Reset

func Reset()

func SetLevel

func SetLevel(level Level)

func Warn

func Warn(args ...Arg)

Types

type Arg

type Arg interface {
	// contains filtered or unexported methods
}

Arg is something that can be printed in a log message or used to control its output.

func AddSkip

func AddSkip(skip int) Arg

AddSkip increments the skip value of a call to a log function. The log functions only call skip with the key DebugCallerKey when the Level LevelDebug is valid.

func Bool

func Bool(key string, value bool) Arg

Bool is a key=value pair with a given boolean value.

func Caller

func Caller(key string, skip int) Arg

Caller creates a caller Arg with the given skip. skip == 0 represents the caller of Caller.

func Duration

func Duration(key string, v time.Duration) Arg

Duration is a key=value pair with a value as the unquoted output of time.Duration.String.

func Err

func Err(err error) Arg

Err creates an Arg with the key ErrorKey. If err == nil Skip is returned.

func Float

func Float[N constraints.Float](key string, value N) Arg

Float is a key=value pair with a given float value.

func IfDebug

func IfDebug(producer _Producer) Arg

IfDebug is for a func that produces an Arg when LevelDebug is valid. producer is only called if LevelDebug is valid.

func Int

func Int[N constraints.Integer](key string, value N) Arg

Int is a key=value pair with a given integer value.

func KeyValue

func KeyValue(key, value string) Arg

KeyValue is a raw key=value string pair.

func Msg

func Msg(format string, i ...any) Arg

Msg is a trimmed and quoted message with the key MsgKey. Only the most recent given Msg will be used.

func Prefix

func Prefix(prefix string) Arg

func Producer

func Producer(fn func() Arg) Arg

Producer creates an Arg at log time.

func Skip

func Skip() Arg

Skip is an Arg to not print anything.

func String

func String(key, value string) Arg

String creates a key=value pair with value being a string quoted with strconv.Quote.

func Time

func Time(key string, v time.Time) Arg

Time is a key=value pair with a value as the unquoted output of time.Time.Format with the format of time.RFC3339.

func WithLevel

func WithLevel(level Level) Arg

WithLevel creates a new shared level for this logger and child loggers until WithLevel is used again with Logger.With.

func WithWriter

func WithWriter(w io.Writer) Arg

WithWriter sets the writer of Logger. The writer is shared with all children until Logger.With is called with [WithNewWriter]. Each write to the writer is locked until the message completes, errors are ignored. Default value is os.Stdout. If w is nil, os.Stdout will be used. Change the value of an existing logger with [WithNewWriter].

type Level

type Level uint8

Level is the level to print log messages at.

const (
	// LevelAll prints at all levels.
	LevelAll Level = iota
	// LevelDebug should be only used in dev.
	// Adds file and line number of log call to output.
	LevelDebug
	// LevelInfo verbose logging.
	LevelInfo
	// LevelWarn only warning and errors are printed.
	LevelWarn
	// LevelError only errors are printed.
	LevelError
	// LevelNone nothing is printed.
	LevelNone
)

func GetLevel

func GetLevel() Level

func (Level) String

func (l Level) String() string

type Logger

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

Logger can be used to print a structured log to an output. All logging is done to a single output.

func New

func New(opt ...Arg) *Logger

New creates a new logger with the following defaults:

func With

func With(args ...Arg) *Logger

func (*Logger) Debug

func (l *Logger) Debug(args ...Arg)

Debug logs a message at LevelDebug.

func (*Logger) Error

func (l *Logger) Error(err error, args ...Arg) bool

Error logs a message at LevelError.

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

func (*Logger) Info

func (l *Logger) Info(args ...Arg)

Info logs a message at LevelInfo.

func (*Logger) Log

func (l *Logger) Log(level Level, args ...Arg)

Log does the actual logging at the given level.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

func (*Logger) Warn

func (l *Logger) Warn(args ...Arg)

Warn logs a message at LevelWarn.

func (*Logger) With

func (l *Logger) With(args ...Arg) *Logger

With creates a sublogger with the given args embedded into each message of the child. This can be used to embed the url of a request for example.

type Option

type Option func(*Logger)

Option represent a top level option for the Logger. Options can only be used with New.

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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