ginzap

package module
v0.0.0-...-1d9e517 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2018 License: MIT Imports: 3 Imported by: 2

README

Gin-Zap

Gin middleware for Logging with Zap, the structured, leveled logging framework from Uber

CircleCI Coverage Status Go Report Card GoDoc

Usage

Example
package main

import (
    "flag"
    "time"

    "github.com/uber-go/zap"
    "github.com/akath19/gin-zap"
    "github.com/gin-gonic/gin"
)

func main() {
    //Gin Router
    router := gin.New()
    //Zap logger
    zap := zap.NewProduction()
    //Add middleware to Gin, requires sync duration & zap pointer
    router.Use(ginzap.Logger(3 * time.Second, zap))
    //Other gin configs
    router.Use(gin.Recovery())

    //Logger will work outside Gin as well
    zap.Warn("Warning")
    zap.Error("Error")
    zap.Info("Info")

    //Start Gin
    router.Run(":8080")
}

Gin-Zap will produces lines in the following way:

[2018-08-06T14:27:43.001-0500] WARN [GIN] {"statusCode": 404, "latency": "1.232µs", "clientIP": "::1", "method": "GET", "path": "/png", "error": ""}

2018-08-06T14:27:43.262-0500 WARN [GIN] {"statusCode": 404, "latency": "60.356µs", "clientIP": "::1", "method": "GET", "path": "/favicon.ico", "error": ""}

2018-08-06T14:27:43.267-0500 WARN [GIN] {"statusCode": 404, "latency": "1.029µs", "clientIP": "::1", "method": "GET", "path": "/favicon.ico", "error": ""}

2018-08-06T14:27:45.397-0500 INFO [GIN] {"statusCode": 200, "latency": "98.698µs", "clientIP": "::1", "method": "GET", "path": "/ping", "error": ""} `

Contributing

All PRs are welcome!

  • Any PRs looking to improve test coverage will be greatly appreciated

License

See LICENSE file.

Documentation

Overview

Package ginzap provides a logging middleware to get https://github.com/uber-go/zap as logging library for https://github.com/gin-gonic/gin. It can be used as replacement for the internal logging middleware http://godoc.org/github.com/gin-gonic/gin#Logger.

This package is heavily based on https://github.com/szuecs/gin-glog

Example:

   package main
   import (
       "flag
       "time"
       "github.com/golang/glog"
       "github.com/akath19/gin-zap"
       "github.com/gin-gonic/gin"
   )
   func main() {
       flag.Parse()
       router := gin.New()
	      logger := zap.NewProduction()
       router.Use(ginzap.Logger(3 * time.Second, logger))
       //..
       router.Use(gin.Recovery())
		  logger.Info("Gin bootstrapped with Zap")
       router.Run(":8080")
   }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ErrorLogger

func ErrorLogger() gin.HandlerFunc

ErrorLogger returns a gin handler func for errors

func ErrorLoggerT

func ErrorLoggerT(t gin.ErrorType) gin.HandlerFunc

ErrorLoggerT returns a gin handler middleware with the given type gin.ErrorType.

func Logger

func Logger(duration time.Duration, logger *zap.Logger) gin.HandlerFunc

Logger returns a gin handler func for all logging

Types

This section is empty.

Jump to

Keyboard shortcuts

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