logmid

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 5 Imported by: 0

README

logmid

logmid is a http middleware that log using a logger.Logger.

By default no fields (like latencies, status, ...) are added to the logger as it's up to the developer to know which fields he / she may want. Instead, a callback can be given to the construtor to add any fields to the logger (or do any other kind of action).

Inside a http.Handler any fields can be added using AddFieldInContext and / or AddErrorInContext which respectively call logger.WithField and logger.WithError.

Custom options can be applied to the middleware (for example the verbosity of the log based on whatever please you, the message wrote, ...)

Example

func setupRouter(router) {
    router.Use(
        httpinfo.Record(), // middleware to record latencies, status, ...

        logmid.New(
            logmid.WithDefaultFields(), // adds default fields like latencies, status, ...
            logmid.WithLogLevelFunc(customLevelFunc), // default is Info, but custom level can be applied
            logmid.WithCallback(customCallback),
        ),
    )
}

func customLevelFunc(r *http.Request) logger.Level {
    var lvl logger.Level

    switch status := httpinfo.Status(); status {
    case status >= 400 && status < 500:
        lvl = logger.LevelWarn
    case status >= 500:
        lvl = logger.LevelError
    default:
        lvl = logger.LevelDebug
    }
    
    return lvl
}

func customCallback(r *http.Request) {
    logmid.AddFieldInContext(r.Context(), "hello", "world")
}

Documentation

Overview

Package logmid is a net/http middleware that uses httpinfo and logger.Logger to log HTTP requests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddErrorInContext

func AddErrorInContext(ctx context.Context, err error)

AddErrorInContext add an error field to the context. If there is already an error in the context, the original error will be wrap with the reason (err.Error()).

func AddFieldInContext

func AddFieldInContext(ctx context.Context, key string, value interface{})

AddFieldInContext add a log field to the context.

func New

func New(log logger.Logger, opts ...Option) func(http.Handler) http.Handler

New returns a middleware that log requests.

Types

type LogAtLevelFunc

type LogAtLevelFunc func(r *http.Request) logger.Level

LogAtLevelFunc defines the signature of the function that gives the level of the log function.

type OnRequestFunc

type OnRequestFunc func(r *http.Request)

OnRequestFunc defines the signature of the function called on each requests.

type Option

type Option func(opts *Options)

Option defines a way to apply an option to the options.

func WithCallback

func WithCallback(fcts ...OnRequestFunc) Option

WithCallback adds a function called each time a request is logged.

func WithDefaultFields

func WithDefaultFields() Option

WithDefaultFields adds some fields to the request's log.

func WithLogLevelFunc

func WithLogLevelFunc(fct LogAtLevelFunc) Option

WithLogLevelFunc adds a function to set logger's level based on the request.

func WithMessage

func WithMessage(message string) Option

WithMessage sets a custom message on each http request logs.

type Options

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

Options stores the middleware configuration options.

Jump to

Keyboard shortcuts

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