Created
December 29, 2020 09:44
-
-
Save zmwangx/295ccfcc09f5c6bfbfc591ccc58d6be2 to your computer and use it in GitHub Desktop.
Golang echo framework logging with (extended) common log format like nginx/Apache.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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