https://github.com/raahii/ecolog
A simple echo middleware for application logs with request context
https://github.com/raahii/ecolog
contextual-logging echo echo-framework go golang logging
Last synced: 5 months ago
JSON representation
A simple echo middleware for application logs with request context
- Host: GitHub
- URL: https://github.com/raahii/ecolog
- Owner: raahii
- Created: 2022-12-17T14:02:02.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-12-21T01:12:10.000Z (over 3 years ago)
- Last Synced: 2024-06-21T20:08:24.612Z (almost 2 years ago)
- Topics: contextual-logging, echo, echo-framework, go, golang, logging
- Language: Go
- Homepage: https://pkg.go.dev/github.com/raahii/ecolog
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# ecolog
[](https://github.com/raahii/ecolog/actions/workflows/test.yml)
Ecolog provides a middleware for [Go Echo framework](https://echo.labstack.com/) to output application logs with request context.
By using ecolog and Echo's standard gommon logging, You can add fields related to HTTP request such as method, URI, Request ID, etc.
```json
{
"time": "2022-12-18T22:22:21+09:00",
"level": "INFO",
"id": "5aWJKbZ1hEDYyfwhidOnUcD7zRyYHaIa",
"remote_ip": "127.0.0.1",
"host": "localhost:1323",
"method": "GET",
"uri": "/",
"user_agent": "curl/7.79.1",
"message": "This is a log in Hello method."
}
```
## Installation
```shell
go get -u github.com/raahii/ecolog
```
## Example
1. Let's use ecolog middleware to override log format.
```go
func main() {
e := echo.New()
// Use ecolog middleware.
// See also ecolog.AppLoggerConfig doc.
e.Use(ecolog.AppLoggerWithConfig(ecolog.AppLoggerConfig{
Format: `{"time":"${time_rfc3339}","level": "${level}",id":"${id}","remote_ip":"${remote_ip}",` +
`"host":"${host}","method":"${method}","uri":"${uri}","user_agent":"${user_agent}"}`,
}))
...
}
```
2. Define an endpoint, and output application log with `echo.Context.Logger()` in your handler.
```go
func Hello(c echo.Context) error {
c.Logger().Infof("This is a log in Hello method.")
return c.JSON(http.StatusOK, "Hello, World")
}
func main() {
...
e.GET("/", Hello)
...
}
```
3. Then, we can observe the application log with the request context.
```shell
❯ go run example/server.go
⇨ http server started on [::]:1323
{"time":"2022-12-18T22:22:21+09:00","level": "INFO","id":"5aWJKbZ1hEDYyfwhidOnUcD7zRyYHaIa","remote_ip":"127.0.0.1","host":"localhost:1323","method":"GET","uri":"/","user_agent":"curl/7.79.1","message":"This is a log in Hello method."}
```
See [example/server.go](https://github.com/raahii/ecolog/blob/main/example/server.go) for details.