ginzap

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2020 License: MIT Imports: 9 Imported by: 0

README

zap

Build Status Go Report Card GoDoc

Alternative logging through zap. Thanks for Pull Request from @yezooz

Usage

Start using it

Download and install it:

$ go get github.com/bijenkins/zap

Import it in your code:

import "github.com/bijenkins/zap"

Example

See the example.

package main

import (
	"fmt"
	"time"

	// "github.com/davecgh/go-spew/spew"
	ginzap "github.com/bijenkins/zap"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type User struct {
	Username string `json:"username"   binding:"required"`
	Password string `json:"password"   binding:"required"`
}

func main() {
	r := gin.New()

	//logger, _ := zap.NewProduction()
	config := zap.NewProductionConfig()

	config.OutputPaths = []string{
		"/tmp/zaptest.log",
	}
	logger, _ := config.Build()

	defer logger.Sync()
	//spew.Dump(config)

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

	// Logs all panic to error log
	//   - stack means whether output the stack info.
	r.Use(ginzap.RecoveryWithZap(logger, true))

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

	// Example ping request.
	r.POST("/ping", func(c *gin.Context) {

		user := User{}
		err := c.ShouldBindJSON(&user)
		if err != nil {
			//logger.Info(err.Error())
			c.JSON(400, err.Error())
			//panic(err.Error())
			return
		}

		// c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
		c.JSON(200, gin.H{"data": user})
	})

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

Documentation

Overview

Package ginzap provides log handling using zap package. Code structure based on ginrus package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ginzap

func Ginzap(logger *zap.Logger, timeFormat string, utc bool) gin.HandlerFunc

Ginzap returns a gin.HandlerFunc (middleware) that logs requests using uber-go/zap.

Requests with errors are logged using zap.Error(). Requests without errors are logged using zap.Info().

It receives:

  1. A time package format string (e.g. time.RFC3339).
  2. A boolean stating whether to use UTC time zone or local.

func RecoveryWithZap

func RecoveryWithZap(logger *zap.Logger, stack bool) gin.HandlerFunc

RecoveryWithZap returns a gin.HandlerFunc (middleware) that recovers from any panics and logs requests using uber-go/zap. All errors are logged using zap.Error(). stack means whether output the stack info. The stack info is easy to find where the error occurs but the stack info is too large.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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