ginzero

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2024 License: MIT Imports: 15 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

View Source
var Xid = "Xid"

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
	Genxid    func() string
}

type OptionFunc

type OptionFunc func(*Config)

func Genxid added in v0.0.4

func Genxid(genxid func() string) OptionFunc

func SkipPaths added in v0.0.4

func SkipPaths(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