Skip to content

Full featured logger and panic recovery middleware for Echo. Based on Uber's zap.

License

Notifications You must be signed in to change notification settings

karagenc/zap4echo

Repository files navigation

Zap Middleware for Echo


This is a very customizable middleware that provides logging and panic recovery for Echo using Uber's zap. Please see LoggerConfig and RecoverConfig for documentation of configuration fields. Have fun!

Features

  • Highly customizable
    • There's a Skipper function so you can skip logging of HTTP requests depending on the echo.Context
    • Error only logging: Logging can be limited to requests resulted in error — requests which either returned an error or has a status code of 3xx, 4xx, or 5xx.
    • Custom msg field
    • caller field is not logged by default. Logging can be enabled with IncludeCaller.
    • You can omit certain log fields for convenience or performance reasons.
    • Request IDs are logged. Custom header name can be set with CustomRequestIDHeader
    • Custom log fields can be added depending on the echo.Context using FieldAdder function.
    • Errors given as function argument to panic can be handled with ErrorHandler
    • Logging of the stack trace can be customized.
  • Convenient and quick to use
  • Performant
    • zap4echo is designed to be performant.
    • Echo and zap is one of the most performant framework/logger combination in the Go ecosystem.
  • Well tested
    • 100% test coverage

Fields Logged

Please note that in addition, extra fields can be added with FieldAdder function.

  • Logger
    • proto - Protocol
    • host - Host header
    • method - HTTP method
    • status - Status as integer
    • response_size - Size of the HTTP response
    • latency - Time passed between the start and end of handling the request
    • status_text - HTTP status as text
    • client_ip - Client IP address
    • user_agent - User agent
    • path - URL path
    • request_id - Request ID (Uses echo.HeaderXRequestID by default. Custom header can be set with CustomRequestIDHeader)
    • referer - Referer
  • Recover
    • error - Error of the panic
    • method - HTTP method
    • path - URL path
    • client_ip - Client IP address
    • stacktrace (if enabled)
    • request_id - Request ID (Uses echo.HeaderXRequestID by default. Custom header can be set with CustomRequestIDHeader)

Usage

This is a quick cheat sheet. For complete examples, have a look at: basic and full

go get -u github.com/karagenc/zap4echo
log, _ := zap.NewDevelopment()

e.Use(
    zap4echo.Logger(log),
    zap4echo.Recover(log),
)

Then curl it:

curl http://host:port

Customization

Configuration for customization are documented with comments. For documentation, have a look at LoggerConfig and RecoverConfig

log, _ := zap.NewDevelopment()

e.Use(zap4echo.LoggerWithConfig(log, zap4echo.LoggerConfig{
    // Configure here...
}))

e.Use(zap4echo.RecoverWithConfig(log, zap4echo.RecoverConfig{
    // Configure here...
}))

About

Full featured logger and panic recovery middleware for Echo. Based on Uber's zap.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published