2021-12-08 18:55:30 -08:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-06-15 06:09:36 -07:00
|
|
|
"fmt"
|
2024-06-14 18:01:48 -07:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2021-12-08 18:55:30 -08:00
|
|
|
|
|
|
|
"github.com/labstack/echo/v4"
|
2024-07-09 17:57:05 -07:00
|
|
|
|
|
|
|
"git.grosinger.net/tgrosinger/saasitone/pkg/log"
|
2021-12-08 18:55:30 -08:00
|
|
|
)
|
|
|
|
|
2024-06-14 18:01:48 -07:00
|
|
|
// SetLogger initializes a logger for the current request and stores it in the context.
|
|
|
|
// It's recommended to have this executed after Echo's RequestID() middleware because it will add
|
|
|
|
// the request ID to the logger so that all log messages produced from this request have the
|
|
|
|
// request ID in it. You can modify this code to include any other fields that you want to always
|
|
|
|
// appear.
|
|
|
|
func SetLogger() echo.MiddlewareFunc {
|
2021-12-08 18:55:30 -08:00
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
2024-06-14 18:01:48 -07:00
|
|
|
return func(ctx echo.Context) error {
|
|
|
|
// Include the request ID in the logger
|
|
|
|
rID := ctx.Response().Header().Get(echo.HeaderXRequestID)
|
|
|
|
logger := log.Ctx(ctx).With("request_id", rID)
|
|
|
|
|
|
|
|
// TODO include other fields you may want in all logs for this request
|
|
|
|
log.Set(ctx, logger)
|
|
|
|
return next(ctx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// LogRequest logs the current request
|
|
|
|
// Echo provides middleware similar to this, but we want to use our own logger
|
|
|
|
func LogRequest() echo.MiddlewareFunc {
|
|
|
|
return func(next echo.HandlerFunc) echo.HandlerFunc {
|
|
|
|
return func(ctx echo.Context) (err error) {
|
|
|
|
req := ctx.Request()
|
|
|
|
res := ctx.Response()
|
|
|
|
|
|
|
|
// Track how long the request takes to complete
|
|
|
|
start := time.Now()
|
|
|
|
if err = next(ctx); err != nil {
|
|
|
|
ctx.Error(err)
|
|
|
|
}
|
|
|
|
stop := time.Now()
|
|
|
|
|
|
|
|
sub := log.Ctx(ctx).With(
|
|
|
|
"ip", ctx.RealIP(),
|
|
|
|
"host", req.Host,
|
|
|
|
"referer", req.Referer(),
|
|
|
|
"status", res.Status,
|
|
|
|
"bytes_in", func() string {
|
|
|
|
cl := req.Header.Get(echo.HeaderContentLength)
|
|
|
|
if cl == "" {
|
|
|
|
cl = "0"
|
|
|
|
}
|
|
|
|
return cl
|
|
|
|
}(),
|
|
|
|
"bytes_out", strconv.FormatInt(res.Size, 10),
|
|
|
|
"latency", stop.Sub(start).String(),
|
|
|
|
)
|
|
|
|
|
2024-06-19 06:32:22 -07:00
|
|
|
msg := fmt.Sprintf("%s %s", req.Method, req.URL.RequestURI())
|
2024-06-14 18:01:48 -07:00
|
|
|
|
|
|
|
if res.Status >= 500 {
|
2024-06-15 06:09:36 -07:00
|
|
|
sub.Error(msg)
|
2024-06-14 18:01:48 -07:00
|
|
|
} else {
|
2024-06-15 06:09:36 -07:00
|
|
|
sub.Info(msg)
|
2024-06-14 18:01:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2021-12-08 18:55:30 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|