Documentation
¶
Index ¶
- Constants
- func GetRequestID(ctx context.Context) (string, bool)
- func GetUserID(ctx context.Context) (string, bool)
- 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
- func (l *Logger) Debugf(format string, args ...interface{})
- func (l *Logger) Errorf(format string, args ...interface{})
- func (l *Logger) Infof(format string, args ...interface{})
- func (l *Logger) LogRequests() bool
- func (l *Logger) LogResponses() bool
- func (l *Logger) Name() string
- func (l *Logger) Sync() error
- func (l *Logger) TimeFormat() string
- func (l *Logger) TraceHeader() string
- func (l *Logger) Warnf(format string, args ...interface{})
- type Option
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 ¶
This section is empty.
Functions ¶
func GetRequestID ¶
GetRequestID returns the request id from the context if it exists.
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 a request logger that uses zap.Logger as core.
func (*Logger) LogRequests ¶
LogResponses returns if the logging of requests is enabled.
func (*Logger) LogResponses ¶
LogResponses returns if the logging of responses is enabled.
func (*Logger) Sync ¶
Sync calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.
func (*Logger) TimeFormat ¶
TimeFormat returns the configured time format.
func (*Logger) TraceHeader ¶
GetTraceHeader returns the trace header configured.
type Option ¶
type Option func(o *options) error
Options is the type used to modify logger options.
func WithConfig ¶
func WithConfig(raw json.RawMessage) Option
WithConfig uses a JSON to configure the logger.
func WithFormatJSON ¶
func WithFormatJSON() Option
WithFormatJSON configures the format of the logs as JSON. Defaults to text.
func WithFormatText ¶
func WithFormatText() Option
WithFormatText configures the format of the logs as text. Defaults to text.
func WithLogRequests ¶
func WithLogRequests() Option
WithLogRequests enables the log of the requests.
func WithLogResponses ¶
func WithLogResponses() Option
WithLogResponses enables the log of responses
func WithTimeFormat ¶
WithTimeFormat sets a specific format for the time fields.
func WithTraceHeader ¶
WithTraceHeader defines the name of the header used for tracing. Defaults to 'X-Trace-ID'.