slog

package
v0.1.7 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: AGPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package log provides simple log facilities. Loggers may have two levels (log and error) and a stack of prefixes.

Example
logger := SimpleLeveled{
	Printer:  log.New(os.Stdout, "", 0),
	ErrStack: []interface{}{"Error"},
}
logger.Push("Test")
logger.Log("Let's try something")
logger.Error("It has failed")
Output:

Test Let's try something
Error Test It has failed

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CtxError

func CtxError(ctx context.Context, args ...interface{})

CtxError calls Error on the logger stored in the context. If the stored logger does not have interface Leveled, Log is called instead with "Error" as first argument.

func CtxErrorf

func CtxErrorf(ctx context.Context, format string, args ...interface{})

CtxErrorf calls Errorf on the logger stored in the context. If the stored logger does not have interface Leveled, Logf is called instead with the format prefixed with "Error ".

func CtxLog

func CtxLog(ctx context.Context, args ...interface{})

CtxLog calls Log on the Logger stored in the context.

func CtxLogf

func CtxLogf(ctx context.Context, format string, args ...interface{})

CtxLogf calls Logf on the Logger stored in the context.

func CtxPush

func CtxPush(ctx context.Context, args ...interface{})

CtxPush calls Push on the logger stored in the context. If the stored logger does not have interface Stacked, the method panics.

func CtxSaveLogger

func CtxSaveLogger(ctx context.Context, logger Logger) context.Context

CtxSaveLogger creates a context containing the given Logger.

func Log

func Log(target func(...interface{}), stack []interface{}, args ...interface{})

Log is a low level function to implement Logger. It uses the given function to display the stack and the arguments.

func Logf

func Logf(target func(...interface{}), stack []interface{}, format string, args ...interface{})

Log is a low level function to implement Logger. It uses the given function to display the stack and the formatted arguments.

Types

type AsStacked

type AsStacked struct {
	StackedLeveled
}

AsStacked is a simple wrapper from StackedLeveled to Stacked.

func (AsStacked) With

func (self AsStacked) With(args ...interface{}) Stacked

type Leveled

type Leveled interface {
	Logger
	Error(args ...interface{})
	Errorf(format string, args ...interface{})
}

Leveled represents loggers with two levels: log and error.

type Logger

type Logger interface {
	Log(args ...interface{})
	Logf(format string, args ...interface{})
}

Logger is the base type for loggers.

func CtxLoadLogger

func CtxLoadLogger(ctx context.Context) Logger

CtxLoadLogger retrieves a Logger from a context. The Logger must have been stored in the context by CtxSaveLogger.

type Printer

type Printer interface {
	Println(a ...interface{})
}

Printer represents backends for Logger.

type SimpleLeveled

type SimpleLeveled struct {
	Printer  Printer
	LogStack []interface{}
	ErrStack []interface{}
}

SimpleLeveled is an implementation of StackedLeveled.

There is no constructor and the zero value is not usable; you must provide a Printer.

The two levels are distinguished by their stack only. In spite of that, both Push and With append the same values to both stacks. The distinction between the two stacks should be done when constructing a new SimpleLeveled.

func (SimpleLeveled) Error

func (self SimpleLeveled) Error(args ...interface{})

func (SimpleLeveled) Errorf

func (self SimpleLeveled) Errorf(format string, args ...interface{})

func (SimpleLeveled) Log

func (self SimpleLeveled) Log(args ...interface{})

func (SimpleLeveled) Logf

func (self SimpleLeveled) Logf(format string, args ...interface{})

func (*SimpleLeveled) Push

func (self *SimpleLeveled) Push(args ...interface{})

Push appends values to the stacks of prefixes. The values are appended to both levels.

func (*SimpleLeveled) With

func (self *SimpleLeveled) With(args ...interface{}) StackedLeveled

With creates a new StackedLeveled logger with the given values appended to the stack of the current object. The values are appended for both levels.

type SimpleLogger

type SimpleLogger struct {
	Printer Printer
	Stack   []interface{}
}

SimpleLogger is a simple stupid implementation of Stacked. There is no constructor and the zero value is not usable; you must provide a Printer.

func (SimpleLogger) Log

func (self SimpleLogger) Log(args ...interface{})

func (SimpleLogger) Logf

func (self SimpleLogger) Logf(format string, args ...interface{})

func (*SimpleLogger) Push

func (self *SimpleLogger) Push(args ...interface{})

func (SimpleLogger) With

func (self SimpleLogger) With(args ...interface{}) Stacked

type Stacked

type Stacked interface {
	Logger

	// Push appends values to the stack of prefixes.
	Push(args ...interface{})

	// With creates a new Stacked logger with the given values appended to the stack of the current
	// object.
	With(args ...interface{}) Stacked
}

Stacked represents loggers with a stack of prefixes. All values on the stack are prepended to all messages.

func CtxLoadStacked

func CtxLoadStacked(ctx context.Context) Stacked

CtxLoadStacked retrieves a Stacked stored as the logger in the context. The logger must have been stored in the context by CtxSaveLogger. If the stored logger implements StackedLeveled it is converted to Stacked. If the stored logger does not implement neither Stacked nor StackedLeveled, nil is returned.

type StackedLeveled

type StackedLeveled interface {
	Leveled

	// Push appends values to the stack of prefixes.
	Push(args ...interface{})

	// With creates a new StackedLeveled logger with the given values appended to the stack of the
	// current object.
	With(args ...interface{}) StackedLeveled
}

StackedLeveled represents two levels loggers with a stack of prefixes. All values on the stack are prepended to all messages.

type WithStack

type WithStack struct {
	Target interface {
		Log(args ...interface{})
		Error(args ...interface{})
	}
	Stack []interface{}
}

WithStack constructs a StackedLeveled from an object with Log and Error method. It can be used for instance to convert a Leveled value into a StackedLeveled one.

There is no constructor and the zero value is not usable; you must provide a Target.

func (WithStack) Error

func (self WithStack) Error(args ...interface{})

func (WithStack) Errorf

func (self WithStack) Errorf(format string, args ...interface{})

func (WithStack) Log

func (self WithStack) Log(args ...interface{})

func (WithStack) Logf

func (self WithStack) Logf(format string, args ...interface{})

func (*WithStack) Push

func (self *WithStack) Push(args ...interface{})

func (WithStack) With

func (self WithStack) With(args ...interface{}) StackedLeveled

Jump to

Keyboard shortcuts

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