Documentation
¶
Overview ¶
Package httplog logs http requests and responses. It’s highly configurable, e.g. in production, log all response and requests, but don’t log the body or headers, in your dev environment log everything and so on. httplog also has different ways to log depending on your preference — structured logging via JSON, relational database logging or just plain standard library logging. httplog has logic to turn on/off logging based on options you can either pass in to the middleware handler or from a JSON input file included with the library. httplog offers three middleware choices, each of which adhere to fairly common middleware patterns: a simple HandlerFunc (`LogHandlerFunc`), a function (`LogHandler`) that takes a handler and returns a handler (aka Constructor) (`func (http.Handler) http.Handler`) often used with alice (https://github.com/justinas/alice) and finally a function (`LogAdapter`) that returns an Adapter type. An `httplog.Adapt` function and `httplog.Adapter` type are provided. Beyond logging request and response elements, httplog creates a unique id for each incoming request (using xid (https://github.com/rs/xid)) and sets it (and a few other key request elements) into the request context. You can access these context items using provided helper functions, including a function that returns an audit struct you can add to response payloads that provide clients with helpful information for support.
!!!!WARNING!!!! - This package works, but is something I wrote a long time ago and really needs to be updated. I logged Issue #8 to some day address this.
Index ¶
- func Adapt(h http.Handler, adapters ...Adapter) http.Handler
- func Log2Database(enable bool, reqHdr bool, reqBody bool, respHdr bool, respBody bool) option
- func LogHandler(logger zerolog.Logger, db *sql.DB, o *Opts) (mw func(http.Handler) http.Handler)
- func LogHandlerFunc(next http.HandlerFunc, logger zerolog.Logger, db *sql.DB, o *Opts) http.HandlerFunc
- func LogRequest2Stdout(enable bool, header bool, body bool) option
- func LogRequestViaHTTPUtil(enable bool, body bool) option
- func LogResponse2Stdout(enable bool, header bool, body bool) option
- func RequestFragment(ctx context.Context) (string, error)
- func RequestHost(ctx context.Context) (string, error)
- func RequestID(ctx context.Context) (string, error)
- func RequestPath(ctx context.Context) (string, error)
- func RequestPort(ctx context.Context) (string, error)
- func RequestRawQuery(ctx context.Context) (string, error)
- type Adapter
- type Audit
- type AuditURL
- type DumpRequest
- type HTTPUtil
- type L2SOpt
- type Log2DB
- type Log2StdOut
- type Opts
- type ROpt
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Adapt ¶
Adapt function takes the handler you want to adapt, and a list of our Adapter types. The result of any wrapper function should be an acceptable Adapter. Our Adapt function will simply iterate over all adapters, calling them one by one (in reverse order) in a chained manner, returning the result of the first adapter. - Mat Ryer @matryer
func Log2Database ¶ added in v0.5.0
Log2Database sets the options for logging to the database. enable turns on the functionality - if this is set to false, the parameters afterward are irrelevant as nothing will log. reqHdr logs http request headers reqBody logs the http request body respHdr logs http response headers respBody logs the http response body
func LogHandler ¶
LogHandler records and logs as much as possible about an incoming HTTP request and response
func LogHandlerFunc ¶
func LogHandlerFunc(next http.HandlerFunc, logger zerolog.Logger, db *sql.DB, o *Opts) http.HandlerFunc
LogHandlerFunc middleware records and logs as much as possible about an incoming HTTP request and response
func LogRequest2Stdout ¶ added in v0.5.0
LogRequest2Stdout sets the options for logging http requests to Standard Output (stdout). enable turns on the functionality header logs http request headers body logs the http request body
func LogRequestViaHTTPUtil ¶ added in v0.5.0
LogRequestViaHTTPUtil sets the options for logging requests using the standard HTTPUtil package enable turns on the functionality body logs the http request body This is just wrapper functionality and in hindsight is really kinda silly to include, but alas...
func LogResponse2Stdout ¶ added in v0.5.0
LogResponse2Stdout sets the options for logging http responses to Standard Output (stdout). enable turns on the functionality header logs http response headers body logs the http response body
func RequestFragment ¶
RequestFragment gets the request Fragment details from the context
func RequestHost ¶
RequestHost gets the request host from the context
func RequestPath ¶
RequestPath gets the request URL from the context
func RequestPort ¶
RequestPort gets the request port from the context
Types ¶
type Adapter ¶
Adapter type (it gets its name from the adapter pattern — also known as the decorator pattern) above is a function that both takes in and returns an http.Handler. This is the essence of the wrapper; we will pass in an existing http.Handler, the Adapter will adapt it, and return a new (probably wrapped) http.Handler for us to use in its place. So far this is not much different from just wrapping http.HandlerFunc types, however, now, we can instead write functions that themselves return an Adapter. - Mat Ryer @matryer
type Audit ¶
Audit struct can be included as part of an http response body. This struct sends back the Unique ID generated upon receiving a request as well as echoes back the URL information to help with debugging.
type AuditURL ¶
type AuditURL struct { RequestHost string `json:"host,omitempty"` RequestPort string `json:"port,omitempty"` RequestPath string `json:"path,omitempty"` RequestRawQuery string `json:"query,omitempty"` RequestFragment string `json:"fragment,omitempty"` }
AuditURL has URL info which can be included as part of an http response body
type DumpRequest ¶
DumpRequest holds the options for the net/http/httputil.DumpRequest function
type HTTPUtil ¶
type HTTPUtil struct {
DumpRequest DumpRequest
}
HTTPUtil struct hold the options for using the net/http/httputil package
type L2SOpt ¶
L2SOpt struct holds the log2StdOut options. Enable should be true if you want httplog to write to stdout, set the ROpt Header and Body booleans accordingly if you want to write those
type Log2DB ¶
Log2DB struct holds the options for logging to a database Set Enable to true you want any database logging Set the Request and Response options according to whether you want to log request and/or response to the database Requests/Responses will only be logged if Enable is true
type Log2StdOut ¶
Log2StdOut (Log to Standard Output) struct holds the options for logging requests and responses to stdout (using zerolog)
type Opts ¶
type Opts struct { Log2StdOut Log2StdOut `json:"log_json"` Log2DB Log2DB `json:"log_2DB"` HTTPUtil HTTPUtil `json:"httputil"` }
Opts represent HTTP Logging Options
func FileOpts ¶
FileOpts constructs an Opts struct using the httpLogOpt.json file included with the library The idea here is to have a config file that you can swap out on different servers - many enterprises will not let you touch "source code", but allow for manipulation of a config file like this... go figure
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package errs is a modified copy of the upspin.io/errors package.
|
Package errs is a modified copy of the upspin.io/errors package. |
Package logger has helpers to setup a zerolog.Logger https://github.com/rs/zerolog
|
Package logger has helpers to setup a zerolog.Logger https://github.com/rs/zerolog |