Skip to content
/ ginlogr Public

Logging middleware for gin-gonic/gin via go-logr/logr

License

Notifications You must be signed in to change notification settings

alron/ginlogr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ginlogr

Alternative gin-gonic middleware logging through logr.

The Majority of the code used here was lifted from ginzap.

Usage

Start using it

Download and install it:

$ go get github.com/alron/ginlogr
$

Import it in your code:

import "github.com/alron/ginlogr"

Example

package main

import (
    "fmt"
    "time"

    "github.com/gin-gonic/gin"
    "github.com/go-logr/zapr"
    "go.uber.org/zap"
    "github.com/alron/ginlogr"
)

func main() {
    r := gin.New()
    // We use zap and zapr here, but you can really use any of the loggers
    // supported by logr
    zl, _ := zap.NewProduction()
    logger := zapr.NewLogger(zl)

    // Add a ginlogr middleware, which:
    //   - Logs all requests, like a combined access and error log.
    //   - Logs to stdout.
    //   - RFC3339 with UTC time format.
    r.Use(ginlogr.Ginlogr(logger, time.RFC3339, true))

    // Logs all panic to error log
    //   - RFC3389 with UTC time format.
    //   - stack means whether output the stack info.
    r.Use(ginlogr.RecoveryWithLogr(logger, time.RFC3339, true, true))

    // Example ping request.
    r.GET("/ping", func(c *gin.Context) {
        c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
    })

    // Example when panic happen.
    r.GET("/panic", func(c *gin.Context) {
        panic("An unexpected error happen!")
    })

    // Listen and Server in 0.0.0.0:8080
    r.Run(":8080")
}

About

Logging middleware for gin-gonic/gin via go-logr/logr

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages