logginghandler

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2023 License: MIT Imports: 8 Imported by: 3

README

logginghandler

Build Status Go Reference Go Report Card

Just a simple zerolog based request logging http middleware. It also sets a X-Request-ID in the request and response headers.

Powered by github.com/rs/zerolog/hlog and github.com/justinas/alice.

Install

go get -v go.xsfx.dev/logginghandler

Usage

logger := log.With().Logger()

handler := logginghandler.Handler(logger)(
    http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
        log := logginghander.Logger(r)
        log.Info().Msg("hello world")

        w.WriteHeader(http.StatusOK)

        return
    })
)

http.Handle("/", handler)
log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())

or with alice

logger := log.With().Logger()
chain := alice.New(logginghandler.Handler(logger)).Then(
    http.HandlerFunc(func(w http.ResponseWriter, r *http.Request){
        log := logginghander.Logger(r)
        log.Info().Msg("hello world")

        w.WriteHeader(http.StatusOK)

        return
    })
)

http.Handle("/", chain)

log.Fatal().Err(http.ListenAndServe(":5000", nil)).Msg("goodbye")

In other handlers you can access the UUID:

func anotherHandler(w http.ResponseWriter, r *http.Request) {
    log := logginghandler.FromRequest(r)

    uuid, ok := logginghandler.GetUUID(r)
    if !ok {
        log.Error().Err(err).Msg("could not find uuid")
        w.WriteHeader(http.StatusInternalServerError)

        return
    }

    fmt.Fprintf(w, "your uuid is: %s", uuid)

    return
}

The already prepared logger is also available:

l := logginghandler.FromRequest(r)
l.Info().Msg("foo bar")

Documentation

Overview

Package logginghandler is a simple, zerolog based, request logging http middleware. It also sets `X-Request-ID` in the request and response headers.

Example
package main

import (
	"net/http"

	"github.com/rs/zerolog/log"
	"go.xsfx.dev/logginghandler"
)

func main() {
	logger := log.With().Logger()

	handler := logginghandler.Handler(
		logger,
	)(
		http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			logger := logginghandler.FromRequest(r)

			logger.Info().Msg("this is a request")

			w.WriteHeader(http.StatusOK)
		}),
	)

	http.Handle("/", handler)
	log.Fatal().Msg(http.ListenAndServe(":5000", nil).Error())
}
Output:

Index

Examples

Constants

View Source
const (
	UUIDKey    = "uuid"
	UUIDHeader = "X-Request-ID"
)

Variables

This section is empty.

Functions

func FromCtx added in v0.4.0

func FromCtx(ctx context.Context) *zerolog.Logger

FromCtx returns a logger set from ctx. If no one could be found, it will return the global one.

func FromRequest added in v0.4.0

func FromRequest(r *http.Request) *zerolog.Logger

FromRequest returns a logger set from request. If no one could be found, it will return the global one.

func GetUUID

func GetUUID(r *http.Request) (string, bool)

GetUUID gets the requests UUID from a request.

func Handler

func Handler(log zerolog.Logger) func(http.Handler) http.Handler

func RequestIDHandler added in v0.7.0

func RequestIDHandler() func(http.Handler) http.Handler

RequestIDHandler looks in the header for an existing request id. Else it will create one.

Types

This section is empty.

Jump to

Keyboard shortcuts

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