requestid

package module
v0.0.0-...-618fbd2 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2019 License: Apache-2.0 Imports: 6 Imported by: 13

README

Gin's RequestID Middleware

Gin's is a middleware that injects a 'RequestID' into the context and header of each request with enhanced request logging.

Contents

Installation

To install Gin package, you need to install Go and set your Go workspace first.

The first need Go installed (version 1.11+ is required), then you can use the below Go command to install Gin & the RequestID Middleware.

$ go get -v -u github.com/gin-gonic/gin
$ go get -v -u github.com/sumit-tembe/gin-requestid

Quick start

RequestID Middleware with all default configs.
# assume the following code is in example.go file
$ cat example.go
package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	requestid "github.com/sumit-tembe/gin-requestid"
)

func main() {
	// without any middlewares
	router := gin.New()

	// Middlewares
	{
		//recovery middleware
		router.Use(gin.Recovery())
		//middleware which injects a 'RequestID' into the context and header of each request.
		router.Use(requestid.RequestID(nil))
		//middleware which enhance Gin request logger to include 'RequestID'
		router.Use(gin.LoggerWithConfig(requestid.GetLoggerConfig(nil, nil, nil)))
	}

	router.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Golang Otaku!")
	})

	router.Run(":8080")
}

# run example.go and visit 0.0.0.0:8080/ on browser
$ go run example.go
Output Logs:
[GIN-debug] 2019-12-16T18:50:49+05:30 [bzQg6wTpL4cdZ9bM] - "GET /"
[GIN-debug] 2019-12-16T18:50:49+05:30 [bzQg6wTpL4cdZ9bM] - [::1] "GET / HTTP/1.1 200 22.415µs" Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36
RequestID Middleware with user specified UUID generator function.
package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
	requestid "github.com/sumit-tembe/gin-requestid"
	"utils"
)

func main() {
	// without any middlewares
	router := gin.New()

	// Middlewares
	{
		//recovery middleware
		router.Use(gin.Recovery())
		//middleware which injects a 'RequestID' into the context and header of each request.
		router.Use(requestid.RequestID(utils.GenerateULID))
		//middleware which enhance Gin request logger to include 'RequestID'
		router.Use(gin.LoggerWithConfig(requestid.GetLoggerConfig(nil, nil, nil)))
	}

	router.GET("/", func(c *gin.Context) {
		c.String(http.StatusOK, "Golang Otaku!")
	})

	router.Run(":8080")
}
# run example.go and visit 0.0.0.0:8080/ on browser
$ go run example.go
Output Logs:
[GIN-debug] 2019-12-16T19:02:59+05:30 [01DW7EJYEKRXGJRGWERQ2QHWTZ] - "GET /"
[GIN-debug] 2019-12-16T19:02:59+05:30 [01DW7EJYEKRXGJRGWERQ2QHWTZ] - [::1] "GET / HTTP/1.1 200 22.415µs" Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36

Tip: It's a good practice to pass context to all functions so that we can log with RequestID.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDefaultLogFormatterWithRequestID

func GetDefaultLogFormatterWithRequestID() gin.LogFormatter

GetDefaultLogFormatterWithRequestID returns gin.LogFormatter with 'RequestID'

func GetLoggerConfig

func GetLoggerConfig(formatter gin.LogFormatter, output io.Writer, skipPaths []string) gin.LoggerConfig

GetLoggerConfig return gin.LoggerConfig which will write the logs to specified io.Writer with given gin.LogFormatter. By default gin.DefaultWriter = os.Stdout reference: https://github.com/gin-gonic/gin#custom-log-format

func GetRequestIDFromContext

func GetRequestIDFromContext(c *gin.Context) string

GetRequestIDFromContext returns 'RequestID' from the given context if present.

func GetRequestIDFromHeaders

func GetRequestIDFromHeaders(c *gin.Context) string

GetRequestIDFromHeaders returns 'RequestID' from the headers if present.

func RequestID

func RequestID(gen generator) gin.HandlerFunc

RequestID is a middleware that injects a 'RequestID' into the context and header of each request.

Types

This section is empty.

Jump to

Keyboard shortcuts

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