zap4echo

package module
v0.0.0-...-e82fa6e Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 7 Imported by: 0

README

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/tomruk/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...
}))

Documentation

Index

Constants

View Source
const DefaultLoggerMsg = "Served"
View Source
const DefaultRecoverMsg = "Recovered"
View Source
const DefaultRequestIDHeader = echo.HeaderXRequestID

Variables

This section is empty.

Functions

func Logger

func Logger(log *zap.Logger) echo.MiddlewareFunc

func LoggerWithConfig

func LoggerWithConfig(log *zap.Logger, config LoggerConfig) echo.MiddlewareFunc

func Recover

func Recover(log *zap.Logger) echo.MiddlewareFunc

func RecoverWithConfig

func RecoverWithConfig(log *zap.Logger, config RecoverConfig) echo.MiddlewareFunc

Types

type LoggerConfig

type LoggerConfig struct {
	// Only log requests that respond with a status code of
	// 3XX, 4XX, or 5XX, or when the handler returns an error.
	ErrorOnly bool

	// Skip the current request depending on the context.
	Skipper func(c echo.Context) bool

	// Custom string for the `msg` field
	CustomMsg string

	// Don't omit the `caller` field. By default, caller will not be printed.
	//
	// Caller gets printed as `zap4echo/logger.go:121`. That is redundant.
	IncludeCaller bool

	// If true, printing of stack trace will be disabled.
	OmitStackTrace bool

	// If true, particular field will not be printed.
	OmitStatusText bool
	OmitClientIP   bool
	OmitUserAgent  bool
	OmitPath       bool
	OmitRequestID  bool
	OmitReferer    bool

	// Custom header name for request ID
	CustomRequestIDHeader string

	// A function for adding custom fields depending on the context.
	FieldAdder func(c echo.Context) []zapcore.Field
}

type RecoverConfig

type RecoverConfig struct {
	// Custom string for the `msg` field
	CustomMsg string

	// Set this to true to enable stack trace.
	// `stacktrace` field will be used to print stack trace.
	StackTrace bool
	// Size allocated on memory for stack trace.
	StackTraceSize int
	// If stack trace is enabled, this is to print stack traces of all goroutines.
	PrintStackTraceOfAllGoroutines bool

	// Custom header name for request ID
	CustomRequestIDHeader string

	// A function for adding custom fields depending on the context.
	FieldAdder func(c echo.Context, err error) []zap.Field

	// The panic was happened, and it was handled and logged gracefully.
	// What's next?
	//
	// This function is called to handle the error of panic.
	ErrorHandler func(c echo.Context, err error)
}

Directories

Path Synopsis
_examples

Jump to

Keyboard shortcuts

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