logger

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2024 License: MIT Imports: 10 Imported by: 0

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(pairs []KeyValuePair) ([]byte, error)

Marshal is a custom JSON marshalling function that preserves the order of keys.

func Unmarshal

func Unmarshal(data []byte, v interface{}) error

Unmarshal is a custom JSON unmarshalling function.

Types

type KeyValuePair

type KeyValuePair struct {
	Key   string
	Value interface{}
}

KeyValuePair represents a key-value pair.

func MapToKeyValuePairs

func MapToKeyValuePairs(m map[string]interface{}) []KeyValuePair

MapToKeyValuePairs converts a map to a slice of KeyValuePair

type Middleware

type Middleware struct {
	// contains filtered or unexported fields
}

LoggerMiddleware is a struct that holds the configuration for the middleware.

func NewLoggerMiddleware

func NewLoggerMiddleware(sensitiveFields []string, logger *log.Logger) *Middleware

NewMiddleware creates a new Middleware with the given sensitive fields and logger.

func (*Middleware) Middleware

func (lm *Middleware) Middleware(next http.Handler) http.Handler

Middleware is the actual middleware function.

Jump to

Keyboard shortcuts

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