Documentation ¶
Index ¶
- Constants
- Variables
- func GetRequestID(ctx context.Context) (string, bool)
- func GetUserID(ctx context.Context) (string, bool)
- func NewLoggerHandler(name string, logger *Logger, next http.Handler) http.Handler
- func NewRequestID() string
- func RequestID(headerName string) func(next http.Handler) http.Handler
- func WithRequestID(ctx context.Context, requestID string) context.Context
- func WithUserID(ctx context.Context, userID string) context.Context
- type CommonLogFormat
- type Logger
- type LoggerHandler
- type ResponseLogger
Constants ¶
const ( // RequestIDKey is the context key that should store the request identifier. RequestIDKey key = iota // UserIDKey is the context key that should store the user identifier. UserIDKey )
Variables ¶
var ErrorKey = logrus.ErrorKey
ErrorKey defines the key used to log errors.
Functions ¶
func GetRequestID ¶
GetRequestID returns the request id from the context if it exists.
func NewLoggerHandler ¶
NewLoggerHandler returns the given http.Handler with the logger integrated.
func NewRequestID ¶
func NewRequestID() string
NewRequestID creates a new request id using github.com/rs/xid.
func RequestID ¶
RequestID returns a new middleware that gets the given header and sets it in the context so it can be written in the logger. If the header does not exists or it's the empty string, it uses github.com/rs/xid to create a new one.
func WithRequestID ¶
WithRequestID returns a new context with the given requestID added to the context.
Types ¶
type CommonLogFormat ¶
type CommonLogFormat struct{}
CommonLogFormat implements the logrus.Formatter interface it writes logrus entries using a CLF format prepended by the request-id.
func (*CommonLogFormat) Format ¶
func (f *CommonLogFormat) Format(entry *logrus.Entry) ([]byte, error)
Format implements the logrus.Formatter interface. It returns the given logrus entry as a CLF line with the following format:
<request-id> <remote-address> <name> <user-id> <time> <duration> "<method> <path> <protocol>" <status> <size>
If a field is not known, the hyphen symbol (-) will be used.
type Logger ¶
Logger is an alias of logrus.Logger.
func New ¶
func New(name string, raw json.RawMessage) (*Logger, error)
New initializes the logger with the given options.
func (*Logger) GetTraceHeader ¶
GetTraceHeader returns the trace header configured
type LoggerHandler ¶
type LoggerHandler struct {
// contains filtered or unexported fields
}
LoggerHandler creates a logger handler
func (*LoggerHandler) ServeHTTP ¶
func (l *LoggerHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements the http.Handler and call to the handler to log with a custom http.ResponseWriter that records the response code and the number of bytes sent.
type ResponseLogger ¶
type ResponseLogger interface { http.ResponseWriter Size() int StatusCode() int Fields() map[string]interface{} WithFields(map[string]interface{}) }
ResponseLogger defines an interface that a responseWrite can implement to support the capture of the status code, the number of bytes written and extra log entry fields.
func NewResponseLogger ¶
func NewResponseLogger(w http.ResponseWriter) ResponseLogger
NewResponseLogger wraps the given response writer with methods to capture the status code, the number of bytes written, and methods to add new log entries. It won't wrap the response writer if it's already a ResponseLogger.