logparams

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2023 License: MIT Imports: 8 Imported by: 2

README

logparams

go.dev Reference GitHub Workflow Status (with event)

This is a Go middleware log output that prints parameters if present in the HTTP request. Currently supports PostForm, query params, and JSON body.

The output can be a string or printed directly to the logger. Recommend using with middleware, see example below.

Install

go get -u github.com/aaronvb/logparams

Usage

Using logger:

var logger log.Logger{}
lp := logparams.LogParams{Request: r}
lp.ToLogger(&logger)

Output to a string:

lp := logparams.LogParams{Request: r}
lp.ToString()

Result:

Parameters: {"foo" => "bar", "hello" => "world"}

Returning data in struct:

lp := logparams.LogParams{Request: r}
lp.ToFields()
type ParamFields struct {
	Form      map[string]string
	Query     map[string]string
	Json      map[string]interface{}
	JsonArray []map[string]interface{}
}

Middleware Example (using gorilla/mux)

package main

import (
	"fmt"
	"log"
	"net/http"
	"os"

	"github.com/aaronvb/logparams"

	"github.com/gorilla/mux"
)

type application struct {
	errorLog *log.Logger
	infoLog  *log.Logger
}

func main() {
	infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
	errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime|log.Lshortfile)

	app := &application{
		errorLog: errorLog,
		infoLog:  infoLog,
	}

	srv := &http.Server{
		Addr:     ":8080",
		ErrorLog: errorLog,
		Handler:  app.routes(),
	}

	infoLog.Printf("Starting server on %s", ":8080")
	err := srv.ListenAndServe()
	errorLog.Fatal(err)
}

func (app *application) routes() http.Handler {
	r := mux.NewRouter()
	r.HandleFunc("/foobar", app.foobar).Methods("POST")

	// Middleware
	r.Use(app.logRequest)
	r.Use(app.logParams)

	return r
}

func (app *application) foobar(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello world")
}

// Middleware

func (app *application) logRequest(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		app.infoLog.Printf("%s - %s", r.Method, r.URL.RequestURI())

		next.ServeHTTP(w, r)
	})
}

func (app *application) logParams(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		lp := logparams.LogParams{Request: r}
		lp.ToLogger(app.infoLog)
		next.ServeHTTP(w, r)
	})
}
> go run main.go
INFO	2020/03/22 11:14:49 Starting server on :8080
INFO	2020/03/22 11:14:51 POST - /foobar
INFO	2020/03/22 11:15:18 Parameters: {"foo" => "bar"}

Optional Values

  • ShowEmpty (bool) will return an empty string, or not print to logger, if there are no parameters. Default is to false if struct arg is not passed.

  • ShowPassword (bool) will show the password and password_confirmation parameters. Default is false if not explicitly passed(DO NOT RECOMMEND).

  • HidePrefix (bool) will hide the Parameters: prefix in the output. Default is to false if struct arg is not passed.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LogParams

type LogParams struct {
	Request      *http.Request
	ShowEmpty    bool
	ShowPassword bool
	HidePrefix   bool
}

LogParams struct Request is the http request HideEmpty will not log or return "" if param is empty. FilterPassword will filter password parameters (default true).

func (*LogParams) ToFields added in v1.1.0

func (lp *LogParams) ToFields() ParamFields

ToLogger will log print all parameters within the http request.

func (*LogParams) ToLogger

func (lp *LogParams) ToLogger(logger *log.Logger)

ToLogger will log print all parameters within the http request.

func (*LogParams) ToString

func (lp *LogParams) ToString() string

ToString will return a string of all parameters within the http request.

type ParamFields added in v1.2.1

type ParamFields struct {
	Form      map[string]string
	Query     map[string]string
	Json      map[string]interface{}
	JsonArray []map[string]interface{}
}

Jump to

Keyboard shortcuts

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