PrettyLogger is a middleware for the Echo web framework that enhances the visibility of server logs through color-coded and formatted output. It's designed to improve the readability of logs by highlighting different aspects of HTTP requests and responses such as status codes, method types, and data transfer sizes.
- Color-Coded Output: Uses ANSI color codes to differentiate between HTTP methods, status codes, and other log components for quick visual parsing.
- Formatted Path and Method Display: Ensures consistent formatting for request paths and methods, including truncation with ellipses for long paths.
- Readable Data Sizes: Converts byte counts into a more readable format (b, Kb, Mb, Gb) for both incoming and outgoing data.
- Performance Timing: Logs the duration of each request in milliseconds, helping identify slow endpoints.
To use PrettyLogger in your Echo project, follow these steps:
- Import the PrettyLogger package into your Echo application.
- Add the
Logger
function as middleware to your Echo instance.
Example:
package main
import (
"github.com/labstack/echo/v4"
"github.com/rdbell/echo-pretty-logger"
)
func main() {
e := echo.New()
// Add PrettyLogger middleware
e.Use(prettylogger.Logger)
// Define your routes
e.GET("/", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
})
// Start server
e.Start(":8080")
}
To demo PrettyLogger's capabilities, navigate to the example
folder within this repository and run go run main.go
in your terminal. This command will launch an Echo server demonstrating the middleware's color-coded and formatted logging output.
Currently, PrettyLogger does not require any configuration and works out of the box by providing sensible defaults for logging HTTP requests and responses. It automatically handles different HTTP methods, status codes, and content sizes in a visually distinct manner.
Contributions are welcome! If you'd like to improve PrettyLogger or suggest new features, feel free to fork the repository, make your changes, and submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for more details.