Skip to content

Commit

Permalink
feat: WebLogger supports optional logging request body
Browse files Browse the repository at this point in the history
  • Loading branch information
fufuok committed Sep 18, 2024
1 parent 1e6a086 commit 4c7c133
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions web/fiber/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ func DefaultLogCondition(c *fiber.Ctx, elapsed time.Duration) bool {
}

// WebLogger Web 日志
func WebLogger(cond LogCondition) fiber.Handler {
func WebLogger(cond LogCondition, withBody ...bool) fiber.Handler {
if cond == nil {
cond = DefaultLogCondition
}
withbody := len(withBody) > 0 && withBody[0]
return func(c *fiber.Ctx) error {
start := time.Now()

Expand All @@ -32,19 +33,24 @@ func WebLogger(cond LogCondition) fiber.Handler {

// Manually call error handler
if chainErr != nil {
sampler.Error().Err(chainErr).
Bytes("body", c.Body()).Str("elapsed", elapsed.String()).
Str("client_ip", tproxy.GetClientIP(c)).Str("method", c.Method()).
Msg(c.OriginalURL())
log := sampler.Error().Err(chainErr).
Str("elapsed", elapsed.String()).
Str("client_ip", tproxy.GetClientIP(c)).Str("method", c.Method())
if withbody {
log.Bytes("body", c.Body())
}
log.Msg(c.OriginalURL())
return c.SendStatus(fiber.StatusInternalServerError)
}

if cond(c, elapsed) {
sampler.Warn().
Bytes("body", c.Body()).
log := sampler.Warn().
Str("client_ip", tproxy.GetClientIP(c)).Str("elapsed", elapsed.String()).
Str("method", c.Method()).Int("http_code", c.Response().StatusCode()).
Msg(c.OriginalURL())
Str("method", c.Method()).Int("http_code", c.Response().StatusCode())
if withbody {
log.Bytes("body", c.Body())
}
log.Msg(c.OriginalURL())
}
return nil
}
Expand Down

0 comments on commit 4c7c133

Please sign in to comment.