Skip to content

Instantly share code, notes, and snippets.

@zmwangx
Created December 29, 2020 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zmwangx/295ccfcc09f5c6bfbfc591ccc58d6be2 to your computer and use it in GitHub Desktop.
Save zmwangx/295ccfcc09f5c6bfbfc591ccc58d6be2 to your computer and use it in GitHub Desktop.
Golang echo framework logging with (extended) common log format like nginx/Apache.
import (
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
)
func Serve() {
e := echo.New()
// Common Log Format
logFormat := `${remote_ip} - - [${time_custom}] "${method} ${path} ${protocol}" ${status} ${bytes_out}`
customTimeFormat := "2/Jan/2006:15:04:05 -0700"
if extended {
// NCSA extended/combined log format
logFormat += ` "${referer}" "${user_agent}"`
}
logFormat += "\n"
e.Use(middleware.LoggerWithConfig(middleware.LoggerConfig{
Format: logFormat,
CustomTimeFormat: customTimeFormat,
}))
// ...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment