Documentation ¶
Index ¶
- Constants
- Variables
- func Authorization(next http.Handler) http.Handler
- func GetAuthorization(w http.ResponseWriter, r *http.Request) (string, error)
- func GetReqID(ctx context.Context) string
- func Logger(next http.Handler) http.Handler
- func New(h http.Handler) func(next http.Handler) http.Handler
- func NextRequestID() uint64
- func PrintPrettyStack(rvr interface{})
- func Recoverer(next http.Handler) http.Handler
- func RequestID(next http.Handler) http.Handler
- func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler
- func WithLogEntry(r *http.Request, entry LogEntry) *http.Request
- type DefaultLogFormatter
- type LogEntry
- type LogFormatter
- type LoggerInterface
- type Request
- type WrapResponseWriter
Constants ¶
const RequestIDKey ctxKeyRequestID = 0
RequestIDKey is the key that holds the unique request ID in a request context.
Variables ¶
var ( // LogEntryCtxKey is the context.Context key to store the request log entry. LogEntryCtxKey = &contextKey{"LogEntry"} // DefaultLogger is called by the Logger middleware handler to log each request. // Its made a package-level variable so that it can be reconfigured for custom // logging configurations. DefaultLogger func(next http.Handler) http.Handler )
var DefaultTelemetry func(next http.Handler) http.Handler
var IsTTY bool
var RequestIDHeader = "X-Request-Id"
RequestIDHeader is the name of the HTTP Header which contains the request id. Exported so that it can be changed by developers
Functions ¶
func GetAuthorization ¶
func GetReqID ¶ added in v0.0.52
GetReqID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.
func Logger ¶ added in v0.0.52
Logger is a middleware that logs the start and end of each request, along with some useful data about what was requested, what the response status was, and how long it took to return. When standard output is a TTY, Logger will print in color, otherwise it will print in black and white. Logger prints a request ID if one is provided.
Alternatively, look at https://github.com/goware/httplog for a more in-depth http logger with structured logging support.
IMPORTANT NOTE: Logger should go before any other middleware that may change the response, such as `middleware.Recoverer`. Example:
```go r := chi.NewRouter() r.Use(middleware.Logger) // <--<< Logger should come before Recoverer r.Use(middleware.Recoverer) r.Get("/", handler) ```
func NextRequestID ¶ added in v0.0.52
func NextRequestID() uint64
NextRequestID generates the next request ID in the sequence.
func PrintPrettyStack ¶ added in v0.0.52
func PrintPrettyStack(rvr interface{})
func Recoverer ¶ added in v0.0.52
Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and returns a HTTP 500 (Internal Server Error) status if possible. Recoverer prints a request ID if one is provided.
Alternatively, look at https://github.com/pressly/lg middleware pkgs.
func RequestID ¶ added in v0.0.52
RequestID is a middleware that injects a request ID into the context of each request. A request ID is a string of the form "host.example.com/random-0001", where "random" is a base62 random string that uniquely identifies this go process, and where the last number is an atomically incremented request counter.
func RequestLogger ¶ added in v0.0.52
func RequestLogger(f LogFormatter) func(next http.Handler) http.Handler
RequestLogger returns a logger handler using a custom LogFormatter.
Types ¶
type DefaultLogFormatter ¶ added in v0.0.52
type DefaultLogFormatter struct { Logger LoggerInterface NoColor bool }
DefaultLogFormatter is a simple logger that implements a LogFormatter.
func (*DefaultLogFormatter) NewLogEntry ¶ added in v0.0.52
func (l *DefaultLogFormatter) NewLogEntry(r *http.Request) LogEntry
NewLogEntry creates a new LogEntry for the request.
type LogEntry ¶ added in v0.0.52
type LogEntry interface { Write(status, bytes int, header http.Header, elapsed time.Duration, extra interface{}) Panic(v interface{}, stack []byte) }
LogEntry records the final log when a request completes. See defaultLogEntry for an example implementation.
func GetLogEntry ¶ added in v0.0.52
GetLogEntry returns the in-context LogEntry for a request.
type LogFormatter ¶ added in v0.0.52
LogFormatter initiates the beginning of a new LogEntry per request. See DefaultLogFormatter for an example implementation.
type LoggerInterface ¶ added in v0.0.52
type LoggerInterface interface {
Print(v ...interface{})
}
LoggerInterface accepts printing to stdlib logger or compatible logger.
type Request ¶ added in v0.0.44
func CallRequests ¶ added in v0.0.44
type WrapResponseWriter ¶ added in v0.0.52
type WrapResponseWriter interface { http.ResponseWriter // Status returns the HTTP status of the request, or 0 if one has not // yet been sent. Status() int // BytesWritten returns the total number of bytes sent to the client. BytesWritten() int // Tee causes the response body to be written to the given io.Writer in // addition to proxying the writes through. Only one io.Writer can be // tee'd to at once: setting a second one will overwrite the first. // Writes will be sent to the proxy before being written to this // io.Writer. It is illegal for the tee'd writer to be modified // concurrently with writes. Tee(io.Writer) // Unwrap returns the original proxied target. Unwrap() http.ResponseWriter }
WrapResponseWriter is a proxy around an http.ResponseWriter that allows you to hook into various parts of the response process.
func NewWrapResponseWriter ¶ added in v0.0.52
func NewWrapResponseWriter(w http.ResponseWriter, protoMajor int) WrapResponseWriter
NewWrapResponseWriter wraps an http.ResponseWriter, returning a proxy that allows you to hook into various parts of the response process.