Skip to content
/ ecolog Public

A simple echo middleware for application logs with request context

Notifications You must be signed in to change notification settings

raahii/ecolog

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ecolog

Go package

Ecolog provides a middleware for Go Echo framework 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.

{
  "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

go get -u github.com/raahii/ecolog

Example

  1. Let's use ecolog middleware to override log format.
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}"}`,
  }))
  
  ...
}
  1. Define an endpoint, and output application log with echo.Context.Logger() in your handler.
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)
  ...
}
  1. Then, we can observe the application log with the request context.
❯ 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 for details.

Releases

No releases published

Languages