-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.go
46 lines (40 loc) · 875 Bytes
/
logger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package fiberlogrus
import (
"os"
"time"
"github.com/gofiber/fiber/v2"
log "github.com/sirupsen/logrus"
)
// getLogrusFields calls FuncTag functions on matching keys
func getLogrusFields(ftm map[string]FuncTag, c *fiber.Ctx, d *data) log.Fields {
f := make(log.Fields)
for k, ft := range ftm {
f[k] = ft(c, d)
}
return f
}
// New creates a new middleware handler
func New(config ...Config) fiber.Handler {
var cfg Config
if len(config) == 0 {
cfg = ConfigDefault
} else {
cfg = config[0]
}
d := new(data)
// Set PID once
d.pid = os.Getpid()
ftm := getFuncTagMap(cfg, d)
return func(c *fiber.Ctx) error {
d.start = time.Now()
err := c.Next()
d.end = time.Now()
switch cfg.Logger {
case nil:
log.WithFields(getLogrusFields(ftm, c, d)).Info()
default:
cfg.Logger.WithFields(getLogrusFields(ftm, c, d)).Info()
}
return err
}
}