package
1.2.0
Repository: https://github.com/krostar/logger.git
Documentation: pkg.go.dev

# 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")
}

# Functions

AddErrorInContext add an error field to the context.
AddFieldInContext add a log field to the context.
New returns a middleware that log requests.
WithCallback adds a function called each time a request is logged.
WithDefaultFields adds some fields to the request's log.
WithLogLevelFunc adds a function to set logger's level based on the request.
WithMessage sets a custom message on each http request logs.

# Structs

Options stores the middleware configuration options.

# Type aliases

No description provided by the author
No description provided by the author
Option defines a way to apply an option to the options.