package module
Version: v0.0.0-...-b164435 Latest Latest

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

Go to latest
Published: Feb 27, 2019 License: BSD-2-Clause Imports: 5 Imported by: 0


httplog for Go

Go Report Card

This is a composable HTTP logging library. It doesn't dictate how you should log your stuff. Instead, it provides a toolkit to help roll your own logger. Use the parts you like and ignore the rest.

  • It's based around a LogRequest struct that contains parsed info about a HTTP request.
  • The LogHandler middleware lets you log any HTTP request. You give it a function that gets a LogRequest. The function can format and write log entries any way it likes.
  • To format log entries, you can use the CommonLogLine or CombinedLogLine function. It's also easy to roll your own formatting: LogRequest should have all the info you need.

The hard parts of this library were extracted from the Gorilla Handlers package. Credit for them goes to Mahmud Ridwan. I just added the composable framework around them. The Gorilla libraries are great, but at the time of writing they impose some restrictions that make it hard to do fancy stuff like log into multiple places and use custom formats. This logging library is also framework-agnostic and doesn't depend on Gorilla or anything else outside the Go standard libraries.





This section is empty.


This section is empty.


func AppendQuoted

func AppendQuoted(buf []byte, s string) []byte

AppendQuoted appends a quoted version of string s to buf.

func CombinedLogLine

func CombinedLogLine(r LogRequest) []byte

CombinedLogLine returns a log entry in Apache Combined Log Format.


Always sets the ident field of the log to -

func CommonLogLine

func CommonLogLine(r LogRequest) []byte

CommonLogLine returns a log entry in Apache Common Log Format (CLF).


Always sets the ident field of the log to -

func LogHandler

func LogHandler(handler http.Handler, logger Logger) logHandler

LogHandler makes a http.Handler (middleware for the Go HTTP server). For each HTTP request, it calls the user-supplied handler. At the end of the request, it additionally calls the user-supplied logger to log the request.

The logger gets a LogRequest. Usually you'll want to call one of the log formatting functions in this package or roll your own log formatter. Then write the resulting log entry somewhere.

func ParseHostAndURI

func ParseHostAndURI(r *http.Request) (string, string)

ParseHostAndURI gets the host and URI from a HTTP request. It tries some tricks if they don't have normal values.

func ParseUser

func ParseUser(r *http.Request) string

ParseUser gets the username from a HTTP request, or - if none.


type LogRequest

type LogRequest struct {
	StartTime time.Time
	EndTime   time.Time
	User      string
	Host      string
	Method    string
	URI       string
	Proto     string
	Referer   string
	UserAgent string
	Size      int
	Status    int

LogRequest holds all the logging information pertaining to a single HTTP request. Log formatters turn this struct into a log entry in the desired format. This struct holds the information in a pre-parsed format to make it as easy as possible to write log formatters.

type Logger

type Logger func(r LogRequest)

Logger is any user-supplied function to process one LogRequest.

Jump to

Keyboard shortcuts

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