-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
44 lines (38 loc) · 928 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
package server
import (
"log/slog"
"net/http"
"os"
"time"
"github.com/go-chi/httplog/v2"
"github.com/seb-schulz/onegate/internal/config"
)
var (
defaultLoggerMiddleware func(next http.Handler) http.Handler
)
func init() {
opt := httplog.Options{
JSON: true,
LogLevel: slog.LevelInfo,
Concise: true,
RequestHeaders: true,
ResponseHeaders: true,
TimeFieldFormat: time.RFC3339,
Tags: map[string]string{
"version": "latest",
},
QuietDownRoutes: []string{"/query"},
QuietDownPeriod: time.Minute,
}
if config.Config.Logger.File != "" {
f, err := os.OpenFile(config.Config.Logger.File, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0640)
if err != nil {
panic(err)
}
opt.Writer = f
}
defaultLoggerMiddleware = httplog.RequestLogger(httplog.NewLogger("onegate", opt))
}
func loggerMiddleware(next http.Handler) http.Handler {
return defaultLoggerMiddleware(next)
}