Socket
Socket
Sign inDemoInstall

go.xsfx.dev/logginghandler

Package Overview
Dependencies
8
Alerts
File Explorer

Install Socket

Detect and block malicious and high-risk dependencies

Install

    go.xsfx.dev/logginghandler

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


Version published

Readme

Source

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

FAQs

Last updated on 24 Feb 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc