Packages to do common stuff.
Provide a default logfmt-style logger (using go-kit/log/levels) for your app.
logger := log.Default()
err := someAction()
logger.Error().Log(
"msg", "Something happened",
"during", "someAction",
"err", err
) // => level=error msg="Something happened", during=someAction err=<error as string>You can pre-set common fields:
l := logger.With("context", "README")
logger.Info("msg", "the message") // => level=info context=README msg="the message"Supported levels:
DebugInfoWarnErrorCrit
msg: human-readable version of the log, eg."fmt.Sprintf("error loading config: %v", err),"registering gorm callbacks".context: what the system was doing when the info was logged, egoauthprovider.endpoint,dbmigration.err: the error being loggedduring: the function that returned an error or induced the log, eginfluxdb.Client.Write
Use others that are relevant to your app or domain.
Provide a logger that doesn't do anything. This is quite useful for testing.
logger := log.NewNopLogger()export APPKIT_LOG_HUMAN=true Will make the logger outputs to a format that is easily to read for developers.
Helper for starting a HTTP server configured with a log.Logger. Provides Config and ListenAndServe.
-
ETag: will md5 your response body and include the hash in theETagHTTP header. If client provided the same ETag inIf-None-MatchHTTP header, will return304 Not Modifiedand discard the response.Does nothing (ie, passthrough) with non-
GETrequests and non-200 OKresponses. -
LogRequest: logs incoming HTTP requests withlog. Will log start and end of request. Uses logger from requestcontext.Context, so any fields set withlog.Logger.Withwill be included in the log. -
Recovery: recoverspanicin HTTP handlers, sends500 Internal Server Errorto the client, and re-panics the recovered error. -
DefaultMiddleware: Default middleware stack: request -> record HTTP status -> trace -> log -> recover. -
Compose: helper to chain middleware together.
Helper for opening a gorm.DB connection configured with a log.Logger. Provides Config and New.
A basic interface for monitoring request times and other arbitrary data, and recording data into InfluxDB.
Interface for pushing panics and arbitrary errors into error logging systems. Provides implementation for Airbrake monitoring.
This package is a wrapper of gorilla/sessions to fix the potential memory leaking problem.
Context wrappers and http.Handler middleware to setup and use various context.Contexts.
-
RequestTrace: generate unique id for each HTTP request. Useful for tracing everything that happens due to a single request. Used byLogger -
Logger: makes a given logger available viacontext.Context. Integrated withRequestTraceto add the request trace ID to anything logged via the context, if request is being traced. Used byserver.LogRequestmiddleware. -
HTTPStatus: records the HTTP status code for the request. Used byLogRequestto record the final response HTTP status code. -
Gorm: make agorm.DBavailable viacontext.Context.
For an "ABC" context:
ABCwill extract the "value" from acontext.Context. Generally returns the value and aboolto indicate whether the context actually had any value.WithABCishttp.Handlermiddleware that will enable "ABC" in a HTTP handler.ABCContextwill wrap acontext.Contextand provide a new context that can be passed toABC.MustGetABCis a wrapper aroundABCthat willpanicwhenABCwould return false. Useful when you need the context value and the only way you'd handle a missing value would be topanic.
Secret Box provides a simple interface for encryption of data for storage at rest.
It is implemented as a simple wrapper around golang.org/x/crypto/nacl/secretbox that takes care of handling the nonce.