Skip to content

Go logger middleware which can be included via constructor

License

Notifications You must be signed in to change notification settings

LookinLabs/go-logger-middleware

Repository files navigation

go-logger-middleware

Go Logger Middleware is a lightweight, fast and simple HTTP middleware that logs incoming HTTP requests and outgoing HTTP responses.

It uses only standard Go libraries and is compatible with any Go web framework that supports HTTP middleware.

Features

  • User Agent
  • Latency
  • Request ID
  • Request Method
  • Request Body
  • Request Path
  • Client IP
  • Status Code
  • Body Size
  • Response Body
  • Response Host

Usage

The examples usage can be found under the examples directory.

Gin

package examples

import (
	"log"
	"net/http"

	"go-logger-middleware"

	"github.com/gin-gonic/gin"
)

func main() {
	// Initialize the logger middleware
	sensitiveFields := []string{"password", "token"}
	loggerMiddleware := logger.NewLoggerMiddleware(sensitiveFields)

	// Create a Gin router
	r := gin.Default()

	// Use the middleware
	r.Use(gin.WrapH(loggerMiddleware.Middleware(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		// Your handler logic here
		w.Write([]byte("Hello, World!"))
	}))))

	// Define a simple endpoint
	r.GET("/hello", func(c *gin.Context) {
		c.String(http.StatusOK, "Hello, World!")
	})

	// Start the server
	if err := r.Run(":8080"); err != nil {
		log.Fatalf("Failed to run server: %v", err)
	}
}

Chi

package examples

import (
	"log"
	"net/http"

	"go-logger-middleware"

	"github.com/go-chi/chi/v5"
	"github.com/go-chi/chi/v5/middleware"
)

func main() {
	// Initialize the logger middleware
	sensitiveFields := []string{"password", "token"}
	loggerMiddleware := logger.NewLoggerMiddleware(sensitiveFields)

	// Create a Chi router
	r := chi.NewRouter()

	// Use the built-in Chi middleware
	r.Use(middleware.RequestID)
	r.Use(middleware.RealIP)
	r.Use(middleware.Logger)
	r.Use(middleware.Recoverer)

	// Use the custom logger middleware
	r.Use(loggerMiddleware.Middleware)

	// Define a simple endpoint
	r.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, World!"))
	})

	// Start the server
	if err := http.ListenAndServe(":8080", r); err != nil {
		log.Fatalf("Failed to run server: %v", err)
	}
}

Default Logger

package main

import (
	"log"
	"net/http"

	"github.com/lookinlabs/go-logger-middleware"
)

func main() {
	// Initialize the logger middleware
	sensitiveFields := []string{"password", "token"}
	loggerMiddleware := logger.NewLoggerMiddleware(sensitiveFields)

	// Create a new HTTP mux (router)
	mux := http.NewServeMux()

	// Define a simple endpoint
	mux.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello, World!"))
	})

	// Wrap the mux with the logger middleware
	handler := loggerMiddleware.Middleware(mux)

	// Start the server
	if err := http.ListenAndServe(":8080", handler); err != nil {
		log.Fatalf("Failed to run server: %v", err)
	}
}

Logs example at CloudWatch

CloudWatch Logs

License

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

About

Go logger middleware which can be included via constructor

Resources

License

Stars

Watchers

Forks

Packages

No packages published