# 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
- Étienne Lafarge [email protected]
# Functions
New returns an gin.HandlerFunc that will log our HTTP requests.
NewHTTPLogForwardingQueue builds a log forwarding queue that sends entries to an HTTP service, formatted as JSON.
NewLeechedGinResponseWriter builds an returns a LeechedGinResponseWriter.
NewLeechedReadCloser creates a readCloser which reads and stores at most maxSize bytes from a ReadCloser and returns a clone of that same reader, data included.
NewLogrusLogForwardingQueue returns a such a forwarding queue.
NewMockedLogForwardingQueue creates a new MockedLogForwardingQueue.
# Constants
LogAllBodies can be used to log all request bodies (use with care).
LogBodiesOnError indicates to log bodies only for 4xx & 5xx errors.
LogNoBody stipulates that we should never log bodies.
# Variables
NoBodyHTTPMethods is the list of methods for which we don't log bodies cause they don't have any.
# Structs
AccessLog describes the complete log entry format.
AccessLoggerConfig describe the config of our access logger.
HTTPContent describes the format of a Request body and it's metadata.
HTTPLogForwardingQueue forwards logs to an HTTP backend.
LeechedGinResponseWriter is an extension of gin.ResponseWriter that logs the first bytes of the response body in a bytes buffer.
LeechedReadCloser is 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.
LogrusLogForwardingQueue simply forwards logs to Logrus, with the specified log level.
MockedLogForwardingQueue is a mock class for our log forwarding queue.
RequestLogEntry describes the incoming requests log format.
ResponseLogEntry describes the server response log format.
# Interfaces
LogForwardingQueue is a generic interface that forwards logs to a given output stream (stdout, http...).