Categorygithub.com/smallfish-root/gin-http-logger
modulepackage
1.1.0
Repository: https://github.com/smallfish-root/gin-http-logger.git
Documentation: pkg.go.dev

# README

Gin-Gonic HTTP log forwarder

A Gin-Gonic middleware forwarding access logs over HTTP (in JSON). It can be used for instance to forward all your requests logs to a Fluentd HTTP listener.

Features

  • Non blocking: heavy calls made in a goroutine separated from the GIN handler one.
  • Possible to log the request & response bodies all the time, when the handler returns an error code (4xx, 5xx) or never
  • Memory efficient: uses the standard io library abstractions to limit what's loaded in memory, body logs are truncated to 10000 bytes by default, in case of connection failure with the HTTP endpoint, no more than 1000 logs will be kept in memory. These values can be tweaked to your liking.
  • Lightweight but complete

Usage

Like any other Gin-Gonic middleware:

import (
  // ...
  httpLogger "github.com/elafarge/gin-http-logger"
  "github.com/gin-gonic/gin"
  // ...
)

// ...

  r := gin.Default()

	httpLoggerConf := httpLogger.FluentdLoggerConfig{
		Host:           "localhost",
		Port:           13713,
		Env:            "etienne-kubernetes",
		Tag:            "gin.requests",
		BodyLogPolicy:  httpLogger.LOG_BODIES_ON_ERROR,
		MaxBodyLogSize: 50,
		DropSize:       5,
		RetryInterval:  5,
	}

	r.Use(httpLogger.New(httpLoggerConf))

Compatible with

  • FluentD (tested)

Author

# Packages

No description provided by the author

# Functions

No description provided by the author
No description provided by the author
Constructs and returns such a tweaked gin.ResponseWriter.
No description provided by the author
No description provided by the author

# Constants

CONFIG ////.
CONFIG ////.
CONFIG ////.

# Variables

No description provided by the author

# Structs

No description provided by the author
Log format for the whole request.
Format of a Request body and it's metadata.
Definition of such a Queue that forwards logs in JSON over HTTP.
An extension of gin.ResponseWriter that logs the first bytes of the response body in a bytes buffer.
A wrapper around io.ReadCloser that logs the first bytes of a Request's body (in our case) into a bytes buffer.
Log structure passed through the log forwarding channel.
Let's mock the LogForwardingQueue to be able to read what's logged Due to its minmalistic synchronization primitives, you'll have to re-instantiate a queue for each request you want to test.
Request log format.
Response log format.

# Interfaces

Generic Interface for a queue.