ginstructuredlogger

package module
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2023 License: Apache-2.0 Imports: 5 Imported by: 3

README

Gin Structured Logger

Structured logger middleware for Gin

Why?

Shortcuts.

I like using structured logging for my projects and Zerolog works nicely. It's not difficult to set up, but it's a bit repetitive, so this is a reusable library.

If RequestID is enabled in the application, it adds a requestID to the log output.

Finally, it sets a request-specific instance of the logger that is stored in the Gin context. There is a Get method which can be used to simplify retrieval of the logger from the context.

Example

See the Simple example for a runnable example

package main

import (
	"github.com/gin-contrib/requestid"
	"github.com/gin-gonic/gin"

	logger "github.com/mrsimonemms/gin-structured-logger"
)

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

  // RequestID is optional
	r.Use(requestid.New(), logger.New())

	r.GET("/", func(c *gin.Context) {
		log := logger.Get(c)

		log.Info().Msg("Hello world endpoint called")

		c.String(200, "hello world")
	})

	r.Run(":3000")
}

Thanks

This is based upon this Learning Go article

Documentation

Index

Constants

View Source
const (
	CtxKey = "logger"
)

Variables

This section is empty.

Functions

func Get

func Get(c *gin.Context) *zerolog.Logger

func New

func New() gin.HandlerFunc

Types

This section is empty.

Directories

Path Synopsis
examples
simple module

Jump to

Keyboard shortcuts

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