Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultCodeToLevel(logger log.Logger, code int) log.Logger
- func DefaultErrorToCode(_ error) int
- func DefaultFilterLogging(logger log.Logger) log.Logger
- func NewLogger(logLevel, logFormat, debugName string, w io.Writer) log.Logger
- type CodeToLevel
- type Decider
- type Decision
- type DurationToFields
- type ErrorToCode
- type Fields
- type FilterLogging
- type HTTPMiddleware
- type Option
- type ResponseWriterWithStatus
Constants ¶
const ( LogFormatLogfmt = "logfmt" LogFormatJSON = "json" )
Variables ¶
var RequestIDCtxKey struct{}
Functions ¶
func DefaultCodeToLevel ¶
DefaultCodeToLevel is the helper mapper that maps HTTP Response codes to log levels.
func DefaultErrorToCode ¶
DefaultErrorToCode returns an InternalServerError.
func DefaultFilterLogging ¶
DefaultFilterLogging allows logs from all levels to be logged in output.
func NewLogger ¶
NewLogger returns a log.Logger that prints in the provided format at the provided level with a UTC timestamp and the caller of the log entry. If non empty, the debug name is also appended as a field to all log lines. Panics if the log level is not error, warn, info or debug. Log level is expected to be validated before passed to this function.
Types ¶
type CodeToLevel ¶
CodeToLevel function defines the mapping between HTTP Response codes to log levels for server side.
type Decision ¶
type Decision int
Decision defines rules for enabling start and end of logging.
func DefaultDeciderMethod ¶
DefaultDeciderMethod is the default implementation of decider to see if you should log the call by default this is set to LogStartAndFinishCall.
type DurationToFields ¶
DurationToFields function defines how to produce duration fields for logging.
type ErrorToCode ¶
ErrorToCode function determines the error code of the error for the http response.
type Fields ¶
type Fields []string
Fields represents logging fields. It has to have even number of elements (pairs).
func DurationToTimeMillisFields ¶
DurationToTimeMillisFields converts the duration to milliseconds and uses the key `http.time_ms`.
type FilterLogging ¶
FilterLogging makes sure only the logs with level=lvl gets logged, or filtered.
type HTTPMiddleware ¶
type HTTPMiddleware struct {
// contains filtered or unexported fields
}
func NewHTTPServerMiddleware ¶
func NewHTTPServerMiddleware(logger log.Logger, opts ...Option) *HTTPMiddleware
NewHTTPServerMiddleware returns an http middleware.
func (*HTTPMiddleware) WrapHandler ¶
func (m *HTTPMiddleware) WrapHandler(name string, next http.Handler) http.HandlerFunc
type Option ¶
type Option func(*options)
func WithDecider ¶
WithDecider customizes the function for deciding if the HTTP Middlewares/Tripperwares should log.
func WithFilter ¶
func WithFilter(f FilterLogging) Option
WithFilter customizes the function for deciding which level of logging should be allowed. Follows go-kit Allow<level of log> convention.
func WithLevels ¶
func WithLevels(f CodeToLevel) Option
WithLevels customizes the function for mapping HTTP response codes and interceptor log level statements.
type ResponseWriterWithStatus ¶
type ResponseWriterWithStatus struct { http.ResponseWriter // contains filtered or unexported fields }
ResponseWriterWithStatus wraps around http.ResponseWriter to capture the status code of the response.
func WrapResponseWriterWithStatus ¶
func WrapResponseWriterWithStatus(w http.ResponseWriter) *ResponseWriterWithStatus
WrapResponseWriterWithStatus wraps the http.ResponseWriter for extracting status.
func (*ResponseWriterWithStatus) Status ¶
func (r *ResponseWriterWithStatus) Status() string
Status returns http response status.
func (*ResponseWriterWithStatus) StatusCode ¶
func (r *ResponseWriterWithStatus) StatusCode() int
StatusCode returns http response status code.
func (*ResponseWriterWithStatus) WriteHeader ¶
func (r *ResponseWriterWithStatus) WriteHeader(code int)
WriteHeader writes the header.