Categorygithub.com/bplotka/go-httplog
modulepackage
0.0.0-20170803104628-fd208923b012
Repository: https://github.com/bplotka/go-httplog.git
Documentation: pkg.go.dev

# README

go-httplog

Build Status Go Report Card

Robust, smart logger for Golang http request/response.

It provides way to log in details every request and response with chosen fields. It requires any structured logger that fits under httplog.FieldLogger interface.

It comes with useful integration with logrus, but it can be extend to use any logger. It fits into standard net/http middleware (http.Handler) pattern, but comes also with echo integration.

It comes with bunch of configurable fields. Not exhausting list of these:

ReqTimeField  = RequestField("req_time")
IDField       = RequestField("req_id")
RemoteIPField = RequestField("req_remote_ip")
HostField     = RequestField("req_host")
URIField      = RequestField("req_uri")
ReqArgsField  = RequestField("req_args")
MethodField   = RequestField("req_method")
PathField     = RequestField("req_path")
BytesInField  = RequestField("req_bytes_in")
AuthField     = RequestField("req_auth_header")

StatusField       = ResponseField("res_status")
BytesOutField     = ResponseField("res_bytes_out")
ResTimeField      = ResponseField("res_time")
ContentTypeField  = ResponseField("res_content_type")
LocationField     = ResponseField("res_location")
LocationArgsField = ResponseField("res_location_args")
LocationHostField = ResponseField("res_location_host")

Example:

package main

import (
    "net/http"
    
    "github.com/Bplotka/go-httplog"
    "github.com/Bplotka/go-httplog/logrus"
    "github.com/sirupsen/logrus"
)

func main() {
    l := logrus.New()
    
    srv := http.Server{
        Handler: httplog.RegisterMiddleware(
            httplogrus.ToHTTPFieldLoggerDebug(l), // or ToHTTPFieldLoggerInfo if you want these logs to be in Info level.
            httplog.DefaultReqResConfig(), // or httplog.DefaultResponseOnlyConfig() for only log line per response. 
        )(
            http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                // Your handler here...
                l.Info("Inside!")
            }),
        ),
    }
    
    err := srv.Serve(...)
    if err != nil {
        // handle err.
    }
}

Example effect:

  • With DefaultReqResConfig on any request you will get:
Debug[0029] Received HTTP request                         req_bytes_in=0 req_host="127.0.0.1:<some-port>" req_method=GET req_path="...." req_remote_ip=127.0.0.1 req_time="2017-04-14T17:20:07+01:00" <any other field that will filled and configured>
Inside!
Debug[0029] Responding to HTTP request                    res_bytes_out=769 res_content_type="application/json" res_status=200 res_time="2017-04-14T17:20:07+01:00" <any other field that will filled and configured>

For redirection the log can be even more useful:

Debug[0029] Received HTTP request                         req_bytes_in=0 req_host="127.0.0.1:<some-port>" req_method=GET req_path="...." req_remote_ip=127.0.0.1 req_time="2017-04-14T17:20:07+01:00" <any other field that will filled and configured>
Inside!
Debug[0029] Redirecting HTTP request                      res_bytes_out=0 res_location_args="code=...&state=..." res_location_host="...." res_status=303 res_time="2017-04-14T17:20:07+01:00" <any other field that will filled and configured>" 

Integrations:

Web frameworks

Structured Loggers

# Packages

No description provided by the author
No description provided by the author

# Functions

DefaultReqResConfig is configuration for logging one entry when request is received and one when response is written.
DefaultResponseOnlyConfig is configuration for logging only an entry when response is written.
New constructs new httplog Logger.
RegisterMiddleware registers handler that will log request at the beginning and served response at the request end.

# Constants

AuthField contains auth header for request.
BytesInField contains size of request in bytes.
BytesOutField contains size of response in bytes.
CompactURIField contains request arguments which are compacted (only keys).
ContentTypeField contains content-type of the response.
HostField contains request host.
IDField contains ID of the request.
LocationCompactArgsField contains arguments of redirection URL in case of redirection response in compacted form (only keys).
LocationField contains full redirection URL in case of redirection response.
LocationHostField contains host of redirection URL in case of redirection response.
MethodField contains request method.
PathField contains path of request.
RemoteIPField contains request remote IP.
ReqTimeField contains time of request receiving.
ResTimeField contains time returning response or redirecting.
StatusField contains status code.
URIField contains full URI of the request.

# Variables

DefaultRequestFields is a list for recommended configuration of request fields.
DefaultResponseFields is a list for recommended configuration of response fields.

# Structs

Config is a configuration for httplog.
Logger is an instance for httplog to register middleware and wrap response.
MockFieldLogger is an autogenerated mock type for the FieldLogger type.

# Interfaces

The FieldLogger interface generalizes structured logging used by httplog.

# Type aliases

Fields type, used to pass to `WithFields`.
RequestField is a log field that can be deducted from http.Request.
ResponseField is a log field that can be deducted from response.