logger

package
v0.0.19 Latest Latest
Warning

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

Go to latest
Published: Dec 26, 2024 License: GPL-3.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const MissingKey = "EXTRA_VALUE_AT_END"
View Source
const TimeFormat = "2006-01-02T15:04:05.000Z0700"

TimeFormat to use for logging. This is a version of RFC3339 that contains contains millisecond precision

Variables

View Source
var (
	//DefaultOutput is used as the default log output.
	DefaultOutput io.Writer = os.Stderr

	// DefaultLevel is used as the default log level.
	DefaultLevel = Info
)

Functions

This section is empty.

Types

type Binary

type Binary int

A simple shortcut to format numbers in binary when displayed with the normal text output. For example: L.Info("bits", Binary(17))

type CapturedStacktrace

type CapturedStacktrace string

CapturedStacktrace represents a stacktrace captured by a previous call to log.Stacktrace. If passed to a logging function, the stacktrace will be appended.

func Stacktrace

func Stacktrace() CapturedStacktrace

Stacktrace captures a stacktrace of the current goroutine and returns it to be passed to a logging function.

type ColorOption

type ColorOption uint8

ColorOption defines color option

const (
	// ColorOff is the default coloration, and does not
	// inject color codes into the io.Writer.
	ColorOff ColorOption = iota
	// AutoColor checks if the io.Writer is a tty,
	// and if so enables coloring.
	AutoColor
	// ForceColor will enable coloring, regardless of whether
	// the io.Writer is a tty or not.
	ForceColor
)

type Flushable

type Flushable interface {
	Flush() error
}

Flushable represents a method for flushing an output buffer. It can be used if Resetting the log to use a new output, in order to flush the writes to the existing output beforehand.

type Format

type Format []interface{}

Format is a simple convience type for when formatting is required. When processing a value of this type, the logger automatically treats the first argument as a Printf formatting string and passes the rest as the values to be formatted. For example: L.Info(Fmt{"%d beans/day", beans}).

func Fmt

func Fmt(str string, args ...interface{}) Format

Fmt returns a Format type. This is a convience function for creating a Format type.

type Hex

type Hex int

A simple shortcut to format numbers in hex when displayed with the normal text output. For example: L.Info("header value", Hex(17))

type Level

type Level int32

Level represents a log level.

const (
	// NoLevel is a special level used to indicate that no level has been
	// set and allow for a default to be used.
	NoLevel Level = 0

	// Trace is the most verbose level. Intended to be used for the tracing
	// of actions in code, such as function enters/exits, etc.
	Trace Level = 1

	// Debug information for programmer lowlevel analysis.
	Debug Level = 2

	// Info information about steady state operations.
	Info Level = 3

	// Warn information about rare but handled events.
	Warn Level = 4

	// Error information about unrecoverable events.
	Error Level = 5
)

func LevelFromString

func LevelFromString(levelStr string) Level

LevelFromString returns a Level type for the named log level, or "NoLevel" if the level string is invalid. This facilitates setting the log level via config or environment variable by name in a predictable way.

func (Level) String

func (l Level) String() string

type LevelWriter

type LevelWriter interface {
	LevelWrite(level Level, p []byte) (n int, err error)
}

LevelWriter is the interface that wraps the LevelWrite method.

type LeveledWriter

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

LeveledWriter writes all log messages to the standard writer, except for log levels that are defined in the overrides map.

func NewLeveledWriter

func NewLeveledWriter(standard io.Writer, overrides map[Level]io.Writer) *LeveledWriter

NewLeveledWriter returns an initialized LeveledWriter.

standard will be used as the default writer for all log levels, except for log levels that are defined in the overrides map.

func (*LeveledWriter) LevelWrite

func (lw *LeveledWriter) LevelWrite(level Level, p []byte) (int, error)

LevelWrite implements LevelWriter.

func (*LeveledWriter) Write

func (lw *LeveledWriter) Write(p []byte) (int, error)

Write implements io.Writer.

type Locker

type Locker interface {
	// Lock is called when the output is going to be changed or written to
	Lock()

	// Unlock is called when the operation that called Lock() completes
	Unlock()
}

Locker is used for locking output. If not set when creating a logger, a sync.Mutex will be used internally.

type Logger

type Logger interface {
	// Args are alternating key, val pairs
	// keys must be strings
	// vals can be any type, but display is implementation specific
	// Emit a message and key/value pairs at a provided log level
	Log(level Level, msg string, args ...interface{})

	// Emit a message and key/value pairs at the TRACE level
	Trace(msg string, args ...interface{})

	// Emit a message and key/value pairs at the DEBUG level
	Debug(msg string, args ...interface{})

	// Emit a message and key/value pairs at the INFO level
	Info(msg string, args ...interface{})

	// Emit a message and key/value pairs at the WARN level
	Warn(msg string, args ...interface{})

	// Emit a message and key/value pairs at the ERROR level
	Error(msg string, args ...interface{})

	// Emit a message and key/value pairs at the ERROR level & panic
	Panic(msg string, args ...interface{})

	// If err not null Emit a message and panic
	ErrorPanic(err error, args ...interface{})

	// Indicate if TRACE logs would be emitted. This and the other Is* guards
	// are used to elide expensive logging code based on the current level.
	IsTrace() bool

	// Indicate if DEBUG logs would be emitted. This and the other Is* guards
	IsDebug() bool

	// Indicate if INFO logs would be emitted. This and the other Is* guards
	IsInfo() bool

	// Indicate if WARN logs would be emitted. This and the other Is* guards
	IsWarn() bool

	// Indicate if ERROR logs would be emitted. This and the other Is* guards
	IsError() bool

	// ImpliedArgs returns With key/value pairs
	ImpliedArgs() []interface{}

	// Creates a sublogger that will always have the given key/value pairs
	With(args ...interface{}) Logger

	// Returns the Name of the logger
	Name() string

	// Create a logger that will prepend the name string on the front of all messages.
	// If the logger already has a name, the new value will be appended to the current
	// name. That way, a major subsystem can use this to decorate all it's own logs
	// without losing context.
	Named(name string) Logger

	// Create a logger that will prepend the name string on the front of all messages.
	// This sets the name of the logger to the value directly, unlike Named which honor
	// the current name as well.
	ResetNamed(name string) Logger

	// Updates the level. This should affect all sub-loggers as well. If an
	// implementation cannot update the level on the fly, it should no-op.
	SetLevel(level Level)
}

Logger describes the interface that must be implemeted by all loggers.

func New

func New(opts *LoggerOptions) Logger

New returns a configured logger.

type LoggerOptions

type LoggerOptions struct {
	// Name of the subsystem to prefix logs with
	Name string

	// The threshold for the logger. Anything less severe is supressed
	Level Level

	// Where to write the logs to. Defaults to os.Stderr if nil
	Output []io.Writer

	// An optional Locker in case Output is shared. This can be a sync.Mutex or
	// a NoopLocker if the caller wants control over output, e.g. for batching
	// log lines.
	Mutex Locker

	// Control if the output should be in JSON.
	JSONFormat bool

	// Include file and line information in each log line
	IncludeLocation bool

	// The time format to use instead of the default
	TimeFormat string

	// Control whether or not to display the time at all. This is required
	// because setting TimeFormat to empty assumes the default format.
	DisableTime bool

	// Color the output. On Windows, colored logs are only avaiable for io.Writers that
	// are concretely instances of *os.File.
	Color []ColorOption

	// A function which is called with the log information and if it returns true the value
	// should not be logged.
	// This is useful when interacting with a system that you wish to suppress the log
	// message for (because it's too noisy, etc)
	Exclude func(level Level, msg string, args ...interface{}) bool
}

LoggerOptions can be used to configure a new logger.

type NoopLocker

type NoopLocker struct{}

NoopLocker implements locker but does nothing. This is useful if the client wants tight control over locking, in order to provide grouping of log entries or other functionality.

func (NoopLocker) Lock

func (n NoopLocker) Lock()

Lock does nothing

func (NoopLocker) Unlock

func (n NoopLocker) Unlock()

Unlock does nothing

type Octal

type Octal int

A simple shortcut to format numbers in octal when displayed with the normal text output. For example: L.Info("perms", Octal(17))

type OutputResettable

type OutputResettable interface {
	// ResetOutput swaps the current output writer with the one given in the
	// opts. Color options given in opts will be used for the new output.
	ResetOutput(opts *LoggerOptions) error

	// ResetOutputWithFlush swaps the current output writer with the one given
	// in the opts, first calling Flush on the given Flushable. Color options
	// given in opts will be used for the new output.
	ResetOutputWithFlush(opts *LoggerOptions, flushable Flushable) error
}

OutputResettable provides ways to swap the output in use at runtime

Jump to

Keyboard shortcuts

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