Skip to content

Commit 39e1f6a

Browse files
authored
Prevent unformatted logging messages (#392)
* hide unformatted port message * use modern RequestLogger middleware for request logging * dont collect request ID if we aren't logging it
1 parent 3b22e03 commit 39e1f6a

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

internal/app/app.go

+26-2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ func New(cfg *config.Config) {
3636

3737
engine := echo.New()
3838
engine.HideBanner = true
39+
engine.HidePort = true
3940

4041
if cfg.App.AppMode == "development" {
4142
engine.Debug = true
@@ -51,14 +52,37 @@ func New(cfg *config.Config) {
5152

5253
if cfg.Logging.RequestLogging {
5354
logger.Debug("request logging is enabled")
54-
logCfg := middleware.DefaultLoggerConfig
55+
// DH: We might want to make these fields user configurable at some point
56+
logCfg := middleware.RequestLoggerConfig{
57+
LogLatency: true,
58+
LogRemoteIP: true,
59+
LogHost: true,
60+
LogMethod: true,
61+
LogURI: true,
62+
LogUserAgent: true,
63+
LogStatus: true,
64+
LogResponseSize: true,
65+
LogValuesFunc: func(c echo.Context, v middleware.RequestLoggerValues) error {
66+
logger.Info("request",
67+
zap.String("remote_ip", v.RemoteIP),
68+
zap.String("host", v.Host),
69+
zap.String("method", v.Method),
70+
zap.String("uri", v.URI),
71+
zap.Int("status", v.Status),
72+
zap.Int64("latency_ms", v.Latency.Milliseconds()),
73+
zap.Int64("response_bytes", v.ResponseSize),
74+
zap.String("user_agent", v.UserAgent),
75+
)
76+
return nil
77+
},
78+
}
5579
if !cfg.Logging.LogHealthChecks {
5680
logger.Debug("log_health_checks = false; requests to health check endpoint will not be logged")
5781
logCfg.Skipper = func(c echo.Context) bool {
5882
return c.Path() == cfg.Server.BasePath+"/api/health"
5983
}
6084
}
61-
engine.Use(middleware.LoggerWithConfig(logCfg))
85+
engine.Use(middleware.RequestLoggerWithConfig(logCfg))
6286
}
6387

6488
if cfg.Server.EnableGzip {

0 commit comments

Comments
 (0)