logger

package module
v0.0.0-...-1191ed2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2017 License: MIT Imports: 6 Imported by: 0

README

GoMiddleware : Logger

Middleware that logs a request.

Synopsis

package main

import (
	"net/http"

	"github.com/gomiddleware/logger"
)

func handler(w http.ResponseWriter, r *http.Request) {
	w.Write([]byte(r.URL.Path))
}

func main() {
	// create the logger middleware
	log := logger.New()

	// make the http.Hander and wrap it with the log middleware
	handle := http.HandlerFunc(handler)
	http.Handle("/", log(handle))

	http.ListenAndServe(":8080", nil)
}

When curling this server, you'll see the following in the logs:

2016/10/01 10:05:43.625813 --- GET /about/
2016/10/01 10:05:43.625844 200 GET /about/ 7 34.568µs
2016/10/01 10:05:44.835607 --- GET /
2016/10/01 10:05:44.835636 200 GET / 1 26.633µs
2016/10/01 10:05:50.301461 --- POST /sdf?this=that
2016/10/01 10:05:50.301517 200 POST /sdf?this=that 4 50.09µs

Note the incoming requests (which have --- in their lines) followed by the response (which, in this case all have 200). Then the method, URL, bytes written and time elapsed.

Author

Written by Andrew Chilton for Apps Attic Ltd.

License

ISC.

(Ends)

Documentation

Overview

Package logger provides a logger which can be used as Go Middleware. It logs a line on an incoming request and another line when that request has finished, along with the final status code and the time elapsed.

It also adds a RequestID to the logger which is pulled from the request context, which should be set with the gomiddleware/reqid middleware before this middleware runs.

Incoming fields logged are : method, uri, ip.

Outgoing fields logged are : status, size (bytes), duration.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoggerFromContext

func LoggerFromContext(ctx context.Context) log.Logger

LoggerFromContext can be used to obtain the Log from the request.

func LoggerFromRequest

func LoggerFromRequest(r *http.Request) log.Logger

LoggerFromRequest can be used to obtain the Log from the request.

func New

func New(logger log.Logger) func(http.Handler) http.Handler

NewLogger logger middleware with the given log.Logger.

Types

type Logger

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

Logger middleware.

func (*Logger) ServeHTTP

func (l *Logger) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implementation.

func (*Logger) SetLogger

func (l *Logger) SetLogger(logger log.Logger)

SetLogger sets the logger to `log`. If you have used logger.New(), you can use this to set your logger. Alternatively, if you already have your log.Logger, then you can just call logger.NewLogger() directly.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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