ginzero

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 12, 2023 License: MIT Imports: 11 Imported by: 0

README

ginzero

Usage

Start using it

Download and install it:

go get github.com/lixvyang/ginzero

Import it in your code:

import "github.com/lixvyang/ginzero"

Example

See the example.

package main

import (
	"os"

	"github.com/gin-gonic/gin"
	"github.com/lixvyang/ginzero"
	"github.com/rs/zerolog"
)

func NewLogger() zerolog.Logger {
	logger := zerolog.New(os.Stdout).
		With().
		Caller().
		Timestamp().
		Logger()
	return logger
}

func main() {
	logger := NewLogger()
	r := gin.New()
	r.Use(ginzero.Ginzero(&logger), ginzero.RecoveryWithZero(&logger, true))

	r.GET("/hello", func(c *gin.Context) {
		c.String(200, "hello")
	})

	r.GET("/panic", func(c *gin.Context) {
		panic("panic msg.")
	})

	r.Run(":8002")
}

Skip logging

When you want to skip logging for specific path, please use GinzapWithConfig

r.Use(ginzero.Ginzero(&logger, ginzero.WithSkipPaths([]string{"/hello"})))

Documentation

Overview

Package ginzero provides log handing useing zerolog package. Code structure based on ginrus package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CustomRecoveryWithZero

func CustomRecoveryWithZero(logger ZeroLogger, stack bool, recovery gin.RecoveryFunc) gin.HandlerFunc

func Ginzero

func Ginzero(logger ZeroLogger, optFuncs ...OptionFunc) gin.HandlerFunc

func GinzeroWithConfig

func GinzeroWithConfig(logger ZeroLogger, conf *Config) gin.HandlerFunc

func RecoveryWithZero

func RecoveryWithZero(logger ZeroLogger, stack bool) gin.HandlerFunc

Types

type Config

type Config struct {
	SkipPaths []string
}

type OptionFunc

type OptionFunc func(*Config)

func WithSkipPaths

func WithSkipPaths(paths []string) OptionFunc

type ZeroLogger

type ZeroLogger interface {
	Info() *zerolog.Event
	Error() *zerolog.Event
}

ZapLogger is the minimal logger interface compatible with zerolog.Logger

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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