You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
873 B
Go
40 lines
873 B
Go
package middleware
|
|
|
|
import (
|
|
"mygo/conf"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
"github.com/zxysilent/logs"
|
|
)
|
|
|
|
var mylog *logs.Logger
|
|
|
|
func init() {
|
|
mylog = logs.NewLogger("./logs/access.log")
|
|
if conf.ISTEST {
|
|
mylog.SetLevel(logs.LDEBUG)
|
|
mylog.SetCaller(true)
|
|
// 设置同时显示到控制台
|
|
// 默认只输出到文件
|
|
mylog.SetConsole(true)
|
|
} else {
|
|
mylog.SetLevel(logs.LINFO)
|
|
}
|
|
}
|
|
|
|
func Request() echo.MiddlewareFunc {
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
defer mylog.Flush()
|
|
host := c.Request().Host
|
|
uri := c.Request().RequestURI
|
|
method := c.Request().Method
|
|
agent := c.Request().UserAgent()
|
|
status := c.Response().Status
|
|
ip := echo.ExtractIPFromXFFHeader()(c.Request())
|
|
mylog.Infof("'%s %s' %d %s '-' '%s' %s", method, uri, status, ip, agent, host)
|
|
return next(c)
|
|
}
|
|
}
|
|
}
|