middleware

package
v0.5.11 Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: MIT Imports: 5 Imported by: 2

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Caller

func Caller(skip int) logger.MiddlewareInterface

Caller will add 2 fields in the handled entry context eg:

_file:/..../file_of_the_handle_caller.go
_line:72

func Context

func Context(defaultContext *logger.Context) logger.MiddlewareInterface

Context will define and merge defaultContext with each Entry Context if a default context key is redefined when calling Log/Debug/Info/... func, it will override the default ctx value

Example
package main

import (
	"os"

	"github.com/gol4ng/logger"
	"github.com/gol4ng/logger/formatter"
	"github.com/gol4ng/logger/handler"
	"github.com/gol4ng/logger/middleware"
)

func main() {
	streamHandler := handler.Stream(os.Stdout, formatter.NewJSONEncoder())
	contextHandler := middleware.Context(logger.Ctx("my_value_1", "value 1"))

	myLogger := logger.NewLogger(handler.Group(contextHandler(streamHandler), streamHandler))
	myLogger.Debug("will be printed", logger.Any("my_value_1", "overwritten value 1"))
	myLogger.Debug("only context handler values will be printed")

}
Output:

{"Message":"will be printed","Level":7,"Context":{"my_value_1":"overwritten value 1"}}
{"Message":"will be printed","Level":7,"Context":{"my_value_1":"overwritten value 1"}}
{"Message":"only context handler values will be printed","Level":7,"Context":{"my_value_1":"value 1"}}
{"Message":"only context handler values will be printed","Level":7,"Context":null}

func Error

func Error(passThrough bool) logger.MiddlewareInterface

Error provide a middleware that will catch underlying handler errors and print it to stdout if passThrough is set to true, the error will be printed and then returned in order to be handled elsewhere (in the overlying handler for example)

func Filter

func Filter(filterFn func(logger.Entry) bool) logger.MiddlewareInterface

Filter will exclude some Entry for the underlying handler with the given filter function eg: allows you to only treat the logs that have a specific level

func MaxLevelFilter

func MaxLevelFilter(level logger.Level) logger.MiddlewareInterface

MaxLevelFilter will exclude logs that have a major level than a given level

func MinLevelFilter

func MinLevelFilter(level logger.Level) logger.MiddlewareInterface

MinLevelFilter will exclude logs that have a minor level than a given level

Example
package main

import (
	"os"

	"github.com/gol4ng/logger"
	"github.com/gol4ng/logger/formatter"
	"github.com/gol4ng/logger/handler"
	"github.com/gol4ng/logger/middleware"
)

func main() {
	streamHandler := handler.Stream(os.Stdout, formatter.NewDefaultFormatter())

	minLvlFilterHandler := middleware.MinLevelFilter(logger.WarningLevel)(streamHandler)
	_ = minLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.DebugLevel})
	_ = minLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.InfoLevel})
	_ = minLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.NoticeLevel})
	_ = minLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.WarningLevel})
	_ = minLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.ErrorLevel})
	_ = minLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.CriticalLevel})
	_ = minLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.AlertLevel})
	_ = minLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.EmergencyLevel})

}
Output:

<warning> will be printed
<error> will be printed
<critical> will be printed
<alert> will be printed
<emergency> will be printed

func Placeholder

func Placeholder() logger.MiddlewareInterface

Placeholder will replace message placeholder with context field

func RangeLevelFilter

func RangeLevelFilter(level1 logger.Level, level2 logger.Level) logger.MiddlewareInterface

RangeLevelFilter will exclude logs that have a level that are not between two given levels

Example
package main

import (
	"os"

	"github.com/gol4ng/logger"
	"github.com/gol4ng/logger/formatter"
	"github.com/gol4ng/logger/handler"
	"github.com/gol4ng/logger/middleware"
)

func main() {
	streamHandler := handler.Stream(os.Stdout, formatter.NewDefaultFormatter())

	rangeLvlFilterHandler := middleware.RangeLevelFilter(logger.InfoLevel, logger.WarningLevel)(streamHandler)

	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.DebugLevel})
	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.InfoLevel})
	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.NoticeLevel})
	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be printed", Level: logger.WarningLevel})
	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.ErrorLevel})
	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.CriticalLevel})
	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.AlertLevel})
	_ = rangeLvlFilterHandler(logger.Entry{Message: "will be excluded", Level: logger.EmergencyLevel})

}
Output:

<info> will be printed
<notice> will be printed
<warning> will be printed

func Recover

func Recover() logger.MiddlewareInterface

Recover provide a middleware func that will handle panics that occur in the underlying handler

func Timestamp

func Timestamp() logger.MiddlewareInterface

Timestamp middleware will add the timestamp to the log context

Types

type PanicError

type PanicError struct {
	Data interface{}
	// contains filtered or unexported fields
}

PanicError is an error that contains panic data

func NewPanicError

func NewPanicError(message string, data interface{}) *PanicError

NewPanicError builds a specific error from panic data

func (*PanicError) Error

func (p *PanicError) Error() string

Error implements the error interface for PanicError

Jump to

Keyboard shortcuts

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