logger

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2022 License: MIT Imports: 5 Imported by: 5

README

logger

A light-weight wrapper of standard log.

Static logger

logger.G().Info("message")

writes like 2022/09/20 10:00:00 I | message to stderr.

Logger instance

l := logger.NewDefault(logger.Lerror)
l.Info("message")

Customized logger

package main

import (
	"fmt"
	"log"
	"os"

	"github.com/berquerant/logger"
)

func main() {
	log.SetFlags(0)
	log.SetOutput(os.Stdout)
	l := &logger.Logger{
		Proxy: logger.NewProxy(
			logger.MustNewMapperFunc(func(ev logger.Event) logger.Event {
				switch ev.Level() {
				case logger.Linfo, logger.Lwarn, logger.Lerror:
					// select info, warn, error
					return ev
				default:
					// ignore other levels
					fmt.Printf("Ignore: %v", ev)
					return nil
				}
			}).Next(func(ev logger.Event) {
				if ev.Level() == logger.Lerror { // consume only error logs
					fmt.Printf("Got an error: %v\n", ev)
				}
			}).Next(logger.StandardLogConsumer),
		),
	}
	l.Info("info msg")
	l.Error("error msg")
	l.Trace("trace msg")
}
// Output:
// info msg
// Got an error: error msg
// error msg
// Ignore: trace msg

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidMapperFunc = errors.New("InvalidMapperFunc")
	ErrNilMapperFunc     = errors.New("NilMapperFunc")
	ErrNilEvent          = errors.New("NilEvent")
)

Functions

This section is empty.

Types

type Event

type Event interface {
	Level() Level
	Format() string
	Args() []any
}

func LogLevelToPrefixMapper

func LogLevelToPrefixMapper(ev Event) (Event, error)

LogLevelToPrefixMapper adds a prefix depending on the event level.

func NewEvent

func NewEvent(
	level Level,
	format string,
	args []any,
) Event

func StandardLogConsumer

func StandardLogConsumer(ev Event) (Event, error)

StandardLogConsumer writes an event by `log.Printf`.

type GlobalLogger

type GlobalLogger interface {
	Info(format string, v ...any)
	Warn(format string, v ...any)
	Error(format string, v ...any)
	Debug(format string, v ...any)
	Trace(format string, v ...any)
	SetLevel(level Level)
	Level() Level
}

GlobalLogger is a static logger instance. This filters logs by level, adds a prefix depending on event level and writes logs by `log.Printf`.

func G

func G() GlobalLogger

G returns the `GlobalLogger`.

Example
package main

import (
	"log"
	"os"

	"github.com/berquerant/logger"
)

func main() {
	log.SetFlags(0)
	log.SetOutput(os.Stdout)
	logger.G().Info("information")
	logger.G().Error("error")
	logger.G().Debug("debug")
	logger.G().SetLevel(logger.Ldebug)
	logger.G().Info("change level")
	logger.G().Debug("last line")
}
Output:

I | information
E | error
I | change level
D | last line

type Level

type Level int

Level is the threshold for logging.

const (
	Lsilent Level = 0
	Lerror  Level = 10
	Lwarn   Level = 20
	Linfo   Level = 30
	Ldebug  Level = 40
	Ltrace  Level = 50
)

type Logger

type Logger struct {
	Proxy
}

func NewDefault

func NewDefault(level Level) *Logger

NewDefault returns a new logger with `LogLevelFilter`, `LogLevelToPrefixMapper` and `StandardLogConsumer`.

Example
package main

import (
	"log"
	"os"

	"github.com/berquerant/logger"
)

func main() {
	log.SetFlags(0)
	log.SetOutput(os.Stdout)
	l := logger.NewDefault(logger.Lwarn)
	l.Info("information")
	l.Error("error")
	l.Debug("debug")
	l.Warn("warn")
	l.Debug("last line")
}
Output:

E | error
W | warn

func (*Logger) Debug

func (l *Logger) Debug(format string, v ...any)

func (*Logger) Error

func (l *Logger) Error(format string, v ...any)

func (*Logger) Info

func (l *Logger) Info(format string, v ...any)

func (*Logger) Trace

func (l *Logger) Trace(format string, v ...any)

func (*Logger) Warn

func (l *Logger) Warn(format string, v ...any)

type MapperFunc added in v0.4.0

type MapperFunc func(Event) (Event, error)

MapperFunc converts and/or filters the log event.

func LogLevelFilter

func LogLevelFilter(level Level) MapperFunc

LogLevelFilter ignores an event with the lower level.

func MustNewMapperFunc added in v0.4.0

func MustNewMapperFunc(f any) MapperFunc

func NewMapperFunc added in v0.4.0

func NewMapperFunc(f any) (MapperFunc, error)

func (MapperFunc) Call added in v0.4.0

func (m MapperFunc) Call(event Event) (Event, error)

Call the function. Return ErrNilMapperFunc if it's nil. Return ErrNilEvent if event is nil.

func (MapperFunc) Next added in v0.4.0

func (m MapperFunc) Next(f any) MapperFunc

Next appends a MapperFunc. The returned function calls this, and f with the returned event of this, if no errors.

Available signatures of f:

func(Event)
func(Event) Event
func(Event) error
func(Event) (Event, error)

Otherwise f is evaluated as nil MapperFunc.

func (MapperFunc) Via added in v0.4.0

func (m MapperFunc) Via(f any) MapperFunc

Via appends a MapperFunc. The returned function calls this, and f with the returned event of this, if no errors, but ignores the values from f.

Available signatures of f:

func(Event)
func(Event) Event
func(Event) error
func(Event) (Event, error)

Otherwise f is evaluated as nil MapperFunc.

type Proxy

type Proxy interface {
	Put(event Event)
	SetErrConsumer(func(error))
}

func NewProxy

func NewProxy(mapper MapperFunc) Proxy

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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