zerologgin

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2024 License: MIT Imports: 7 Imported by: 2

README

zerolog-gin

Go Reference Go Report Card Release License

zerolog-gin is a middleware for the Gin web framework that integrates the zerolog logger, providing a fast and efficient logging solution for your web applications. Designed to be lightweight and performant, zerolog-gin captures and structures logs optimally, making debugging and performance analysis easier. With its compatibility with the advanced features of zerolog, it allows for customizing logs according to the specific needs of your application while maintaining a minimal memory footprint.

Installation

To install the package, use the following command:

go get github.com/go-mods/zerolog-gin

Usage

Here is an example of how to use zerolog-gin with a Gin application:

package main

import (
    "bytes"
    "fmt"
    "github.com/gin-gonic/gin"
    zerologgin "github.com/go-mods/zerolog-gin"
    "github.com/rs/zerolog"
    "io"
    "net/http"
    "os"
)

func main() {
    // logger to use with gin
    logger := zerolog.New(os.Stdout).With().Timestamp().Logger()

    // Create an instance of gin router
    r := gin.New()
    r.SetTrustedProxies([]string{"::1"})

    // Add zerolog-gin as a middleware
    r.Use(zerologgin.LoggerWithOptions(&zerologgin.Options{Name: "server", Logger: &logger}))

    r.GET("/", func(c *gin.Context) {
        c.String(http.StatusOK, "hello, zerolog-gin example")
    })

    r.GET("/ping", func(c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{"message": "pong"})
    })

    r.POST("/echo", func(c *gin.Context) {
        body, err := io.ReadAll(c.Request.Body)
        c.Request.Body = io.NopCloser(bytes.NewReader(body))
        if err != nil {
            c.JSON(http.StatusInternalServerError, gin.H{"error": "undefined"})
        } else {
            c.String(http.StatusOK, string(body))
        }
    })

    if err := r.Run(":8080"); err != nil {
        fmt.Println(err.Error())
    }
}

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	NameFieldName      = "name"
	HostnameFieldName  = "hostname"
	ClientIPFieldName  = "client_ip"
	UserAgentFieldName = "user_agent"
	TimestampFieldName = zerolog.TimestampFieldName
	DurationFieldName  = "elapsed"
	MethodFieldName    = "method"
	PathFieldName      = "path"
	PayloadFieldName   = "payload"
	RefererFieldName   = "referer"

	DataLengthFieldName = "data_length"
	BodyFieldName       = "body"
)

Functions

func Logger

func Logger() gin.HandlerFunc

Logger is a gin middleware which use zerolog

func LoggerWithOptions

func LoggerWithOptions(opt *Options) gin.HandlerFunc

LoggerWithOptions is a gin middleware which use zerolog

Types

type Options

type Options struct {
	//
	Name string

	// Custom logger
	Logger *zerolog.Logger

	// FieldsOrder defines the order of fields in output.
	FieldsOrder []string

	// FieldsExclude defines contextual fields to not display in output.
	FieldsExclude []string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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