logging

package
v0.0.0-...-03b241a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 25, 2020 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
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

View Source
var ErrorKey = logrus.ErrorKey

ErrorKey defines the key used to log errors.

Functions

func GetRequestID

func GetRequestID(ctx context.Context) (string, bool)

GetRequestID returns the request id from the context if it exists.

func GetUserID

func GetUserID(ctx context.Context) (string, bool)

GetUserID returns the request id from the context if it exists.

func NewLoggerHandler

func NewLoggerHandler(name string, logger *Logger, next http.Handler) http.Handler

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

func RequestID(headerName string) func(next http.Handler) http.Handler

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

func WithRequestID(ctx context.Context, requestID string) context.Context

WithRequestID returns a new context with the given requestID added to the context.

func WithUserID

func WithUserID(ctx context.Context, userID string) context.Context

WithUserID decodes the token, extracts the user from the payload and stores it in 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

type Logger struct {
	*logrus.Logger
	// contains filtered or unexported fields
}

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) GetImpl

func (l *Logger) GetImpl() *logrus.Logger

GetImpl returns the real implementation of the logger.

func (*Logger) GetTraceHeader

func (l *Logger) GetTraceHeader() string

GetTraceHeader returns the trace header configured

func (*Logger) Middleware

func (l *Logger) Middleware(next http.Handler) http.Handler

Middleware returns the logger middleware that will trace the request of the given handler.

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL