|
| 1 | +# Echo Logger Middleware |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +The Logger middleware for Echo is a powerful tool designed to log HTTP requests and responses. It's essential for debugging, monitoring application performance, and tracking user interactions with your Echo-based web services. |
| 6 | + |
| 7 | +## Features |
| 8 | + |
| 9 | +- Customizable log format |
| 10 | +- Support for multiple output destinations |
| 11 | +- Ability to skip logging for specific routes |
| 12 | +- Customizable timestamp format |
| 13 | +- Option to hide certain information (e.g., IP addresses) |
| 14 | + |
| 15 | +## Installation |
| 16 | + |
| 17 | +The Logger middleware is included in the Echo framework. To use it, you need to import Echo and its middleware package: |
| 18 | + |
| 19 | +```go |
| 20 | +import ( |
| 21 | + "github.com/labstack/echo/v4" |
| 22 | + "github.com/labstack/echo/v4/middleware" |
| 23 | +) |
| 24 | +``` |
| 25 | + |
| 26 | +## Basic Usage |
| 27 | + |
| 28 | +To add the Logger middleware to your Echo instance with default settings: |
| 29 | + |
| 30 | +```go |
| 31 | +e := echo.New() |
| 32 | +e.Use(middleware.Logger()) |
| 33 | +``` |
| 34 | + |
| 35 | +This will log requests and responses to the console (stdout) using the default format. |
| 36 | + |
| 37 | +## Configuration |
| 38 | + |
| 39 | +The Logger middleware can be customized using `LoggerConfig`. Here's an example of how to use it with custom settings: |
| 40 | + |
| 41 | +```go |
| 42 | +e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ |
| 43 | + Format: "${time_custom} ${status} ${method} ${host}${path} ${latency_human}\n", |
| 44 | + CustomTimeFormat: "2006-01-02 15:04:05.00000", |
| 45 | +})) |
| 46 | +``` |
| 47 | + |
| 48 | +### Configuration Options |
| 49 | + |
| 50 | +- `Skipper` func(c echo.Context) bool |
| 51 | + - Skipper defines a function to skip middleware execution. |
| 52 | +- `Format` string |
| 53 | + - Format is the log format string. |
| 54 | +- `CustomTimeFormat` string |
| 55 | + - CustomTimeFormat is the time format for ${time_custom}. |
| 56 | +- `Output` io.Writer |
| 57 | + - Output is the writer where logs are written. |
| 58 | + |
| 59 | +### Available Format Fields |
| 60 | + |
| 61 | +- `${time_rfc3339}` |
| 62 | +- `${time_unix}` |
| 63 | +- `${time_unix_nano}` |
| 64 | +- `${time_custom}` |
| 65 | +- `${id}` |
| 66 | +- `${remote_ip}` |
| 67 | +- `${host}` |
| 68 | +- `${method}` |
| 69 | +- `${uri}` |
| 70 | +- `${path}` |
| 71 | +- `${protocol}` |
| 72 | +- `${referer}` |
| 73 | +- `${user_agent}` |
| 74 | +- `${status}` |
| 75 | +- `${error}` |
| 76 | +- `${latency}` |
| 77 | +- `${latency_human}` |
| 78 | +- `${bytes_in}` |
| 79 | +- `${bytes_out}` |
| 80 | +- `${header:<NAME>}` |
| 81 | +- `${query:<NAME>}` |
| 82 | +- `${form:<NAME>}` |
| 83 | + |
| 84 | +## Advanced Usage |
| 85 | + |
| 86 | +### Custom Output |
| 87 | + |
| 88 | +To log to a file instead of the console: |
| 89 | + |
| 90 | +```go |
| 91 | +logFile, err := os.OpenFile("server.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) |
| 92 | +if err != nil { |
| 93 | + e.Logger.Fatal(err) |
| 94 | +} |
| 95 | +e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ |
| 96 | + Output: logFile, |
| 97 | +})) |
| 98 | +``` |
| 99 | + |
| 100 | +### Skipping Specific Routes |
| 101 | + |
| 102 | +To skip logging for certain routes: |
| 103 | + |
| 104 | +```go |
| 105 | +e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ |
| 106 | + Skipper: func(c echo.Context) bool { |
| 107 | + return c.Path() == "/health" |
| 108 | + }, |
| 109 | +})) |
| 110 | +``` |
| 111 | + |
| 112 | +### Custom Log Format |
| 113 | + |
| 114 | +For a more detailed log format: |
| 115 | + |
| 116 | +```go |
| 117 | +e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{ |
| 118 | + Format: "${time_rfc3339_nano} ${id} ${remote_ip} ${host} ${method} ${uri} ${user_agent}" + |
| 119 | + " ${status} ${error} ${latency_human}" + |
| 120 | + " ${bytes_in} ${bytes_out}\n", |
| 121 | +})) |
| 122 | +``` |
| 123 | + |
| 124 | +## Best Practices |
| 125 | + |
| 126 | +1. Use a custom format that includes all necessary information for your use case. |
| 127 | +2. Consider using a separate log file for HTTP requests to avoid cluttering your main application logs. |
| 128 | +3. In production environments, use a log aggregation service to centralize and analyze your logs. |
| 129 | +4. Be mindful of sensitive information in your logs, especially when logging headers or form data. |
| 130 | + |
| 131 | +## Performance Considerations |
| 132 | + |
| 133 | +While the Logger middleware is designed to be efficient, logging every request can impact performance in high-traffic scenarios. Consider the following: |
| 134 | + |
| 135 | +- Use sampling in production for very high-traffic services. |
| 136 | +- Benchmark your application with and without the Logger middleware to understand its impact. |
| 137 | + |
| 138 | +## Conclusion |
| 139 | + |
| 140 | +The Echo Logger middleware is a flexible and powerful tool for monitoring and debugging your web applications. By customizing its configuration, you can tailor the logging to your specific needs while maintaining high performance. |
0 commit comments