Documentation ¶
Index ¶
- Constants
- Variables
- func GetReqID(ctx context.Context) string
- func NextRequestID() uint64
- func Recoverer(next bokchoy.Handler) bokchoy.Handler
- func RequestID(next bokchoy.Handler) bokchoy.Handler
- func RequestLogger(f LogFormatter) func(next bokchoy.Handler) bokchoy.Handler
- func Timeout(timeout time.Duration) func(next bokchoy.Handler) bokchoy.Handler
- func WithLogEntry(r *bokchoy.Request, entry LogEntry) *bokchoy.Request
- type DefaultLogFormatter
- type LogEntry
- type LogFormatter
- type LoggerInterface
Constants ¶
const RequestIDKey ctxKeyRequestID = 0
RequestIDKey is the key that holds the unique request ID in a request context.
Variables ¶
var ( // LogEntryCtxKey is the context.Context key to store the request log entry. LogEntryCtxKey = &contextKey{"LogEntry"} // DefaultLogger is called by the Logger middleware handler to log each request. // Its made a package-level variable so that it can be reconfigured for custom // logging configurations. DefaultLogger = RequestLogger(&DefaultLogFormatter{Logger: log.New(os.Stdout, "", log.LstdFlags), NoColor: false}) )
Functions ¶
func GetReqID ¶
GetReqID returns a request ID from the given context if one is present. Returns the empty string if a request ID cannot be found.
func NextRequestID ¶
func NextRequestID() uint64
NextRequestID generates the next request ID in the sequence.
func Recoverer ¶
Recoverer is a middleware that recovers from panics, logs the panic (and a backtrace), and adds the error to the request context
func RequestLogger ¶
func RequestLogger(f LogFormatter) func(next bokchoy.Handler) bokchoy.Handler
RequestLogger returns a logger handler using a custom LogFormatter.
func Timeout ¶
Timeout is a middleware that cancels ctx after a given timeout and return
It's required that you select the ctx.Done() channel to check for the signal if the context has reached its deadline and return, otherwise the timeout signal will be just ignored.
ie. a handler may look like:
queue.HandlerFunc(func(r *bokchoy.Request) { ctx := r.Context() processTime := time.Duration(rand.Intn(4)+1) * time.Second select { case <-ctx.Done(): return case <-time.After(processTime): // The above channel simulates some hard work. } return nil })
Types ¶
type DefaultLogFormatter ¶
type DefaultLogFormatter struct { Logger LoggerInterface NoColor bool }
DefaultLogFormatter is a simple logger that implements a LogFormatter.
func (*DefaultLogFormatter) NewLogEntry ¶
func (l *DefaultLogFormatter) NewLogEntry(r *bokchoy.Request) LogEntry
NewLogEntry creates a new LogEntry for the request.
type LogEntry ¶
type LogEntry interface { Write(r *bokchoy.Request, elapsed time.Duration) Panic(v interface{}, stack []byte) }
LogEntry records the final log when a request completes. See defaultLogEntry for an example implementation.
func GetLogEntry ¶
GetLogEntry returns the in-context LogEntry for a request.
type LogFormatter ¶
LogFormatter initiates the beginning of a new LogEntry per request. See DefaultLogFormatter for an example implementation.
type LoggerInterface ¶
type LoggerInterface interface {
Print(v ...interface{})
}
LoggerInterface accepts printing to stdlib logger or compatible logger.