54 lines
1.4 KiB
Go
54 lines
1.4 KiB
Go
package middlewares
|
|
|
|
import (
|
|
"time"
|
|
|
|
"doormile/utils"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
func ZapLogger() fiber.Handler {
|
|
return func(c *fiber.Ctx) error {
|
|
start := time.Now()
|
|
|
|
err := c.Next()
|
|
|
|
latency := time.Since(start)
|
|
status := c.Response().StatusCode()
|
|
method := c.Method()
|
|
path := c.Path()
|
|
|
|
// Client identity and the request id travel with every log line.
|
|
//
|
|
// X-Client / X-Platform are what the customer app sends
|
|
// ("doormile-cx/1.0.0+12", "android"), and they are the only way to
|
|
// answer "is this failing on one build or everywhere" — which is the
|
|
// first question asked when an app-side report arrives. The request id
|
|
// is what correlates a customer saying "it failed" with the line that
|
|
// recorded the real error behind the customer-safe message.
|
|
//
|
|
// Recorded as empty rather than omitted when absent, so a caller that
|
|
// sends nothing is visibly a caller that sends nothing.
|
|
fields := []interface{}{
|
|
"method", method,
|
|
"path", path,
|
|
"status", status,
|
|
"latency", latency,
|
|
"client", c.Get("X-Client"),
|
|
"platform", c.Get("X-Platform"),
|
|
}
|
|
if id, ok := c.Locals("requestid").(string); ok && id != "" {
|
|
fields = append(fields, "requestid", id)
|
|
}
|
|
|
|
if err != nil {
|
|
utils.Error("API request error", append(fields, "error", err.Error())...)
|
|
} else {
|
|
utils.Info("API request success", fields...)
|
|
}
|
|
|
|
return err
|
|
}
|
|
}
|