Skip to content

Commit 796f0e7

Browse files
nogweiikevinmcconnell
authored andcommitted
Teach thruster to optionally not log requests
Adds an environment variable, $LOG_REQUESTS, that when set to a falsey value will not add the logging middleware to the HTTP handler, disabling request logs. Other logs, like the startup messages, are left alone. Closes #49.
1 parent 6ebd928 commit 796f0e7

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

internal/config.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ const (
3333
defaultHttpReadTimeout = 30 * time.Second
3434
defaultHttpWriteTimeout = 30 * time.Second
3535

36-
defaultLogLevel = slog.LevelInfo
36+
defaultLogLevel = slog.LevelInfo
37+
defaultLogRequests = true
3738
)
3839

3940
type Config struct {
@@ -62,7 +63,8 @@ type Config struct {
6263

6364
ForwardHeaders bool
6465

65-
LogLevel slog.Level
66+
LogLevel slog.Level
67+
LogRequests bool
6668
}
6769

6870
func NewConfig() (*Config, error) {
@@ -99,7 +101,8 @@ func NewConfig() (*Config, error) {
99101
HttpReadTimeout: getEnvDuration("HTTP_READ_TIMEOUT", defaultHttpReadTimeout),
100102
HttpWriteTimeout: getEnvDuration("HTTP_WRITE_TIMEOUT", defaultHttpWriteTimeout),
101103

102-
LogLevel: logLevel,
104+
LogLevel: logLevel,
105+
LogRequests: getEnvBool("LOG_REQUESTS", true),
103106
}
104107

105108
config.ForwardHeaders = getEnvBool("FORWARD_HEADERS", !config.HasTLS())

internal/config_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ func TestConfig_override_defaults_with_env_vars(t *testing.T) {
116116
usingEnvVar(t, "GZIP_COMPRESSION_ENABLED", "0")
117117
usingEnvVar(t, "DEBUG", "1")
118118
usingEnvVar(t, "ACME_DIRECTORY", "https://acme-staging-v02.api.letsencrypt.org/directory")
119+
usingEnvVar(t, "LOG_REQUESTS", "false")
119120

120121
c, err := NewConfig()
121122
require.NoError(t, err)
@@ -127,6 +128,7 @@ func TestConfig_override_defaults_with_env_vars(t *testing.T) {
127128
assert.Equal(t, false, c.GzipCompressionEnabled)
128129
assert.Equal(t, slog.LevelDebug, c.LogLevel)
129130
assert.Equal(t, "https://acme-staging-v02.api.letsencrypt.org/directory", c.ACMEDirectoryURL)
131+
assert.Equal(t, false, c.LogRequests)
130132
}
131133

132134
func TestConfig_override_defaults_with_env_vars_using_prefix(t *testing.T) {
@@ -136,6 +138,7 @@ func TestConfig_override_defaults_with_env_vars_using_prefix(t *testing.T) {
136138
usingEnvVar(t, "THRUSTER_HTTP_READ_TIMEOUT", "5")
137139
usingEnvVar(t, "THRUSTER_X_SENDFILE_ENABLED", "0")
138140
usingEnvVar(t, "THRUSTER_DEBUG", "1")
141+
usingEnvVar(t, "THRUSTER_LOG_REQUESTS", "0")
139142

140143
c, err := NewConfig()
141144
require.NoError(t, err)
@@ -145,6 +148,7 @@ func TestConfig_override_defaults_with_env_vars_using_prefix(t *testing.T) {
145148
assert.Equal(t, 5*time.Second, c.HttpReadTimeout)
146149
assert.Equal(t, false, c.XSendfileEnabled)
147150
assert.Equal(t, slog.LevelDebug, c.LogLevel)
151+
assert.Equal(t, false, c.LogRequests)
148152
}
149153

150154
func TestConfig_prefixed_variables_take_precedence_over_non_prefixed(t *testing.T) {

internal/handler.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ type HandlerOptions struct {
1717
xSendfileEnabled bool
1818
gzipCompressionEnabled bool
1919
forwardHeaders bool
20+
logRequests bool
2021
}
2122

2223
func NewHandler(options HandlerOptions) http.Handler {
2324
handler := NewProxyHandler(options.targetUrl, options.badGatewayPage, options.forwardHeaders)
2425
handler = NewCacheHandler(options.cache, options.maxCacheableResponseBody, handler)
2526
handler = NewSendfileHandler(options.xSendfileEnabled, handler)
27+
handler = NewRequestStartMiddleware(handler)
28+
2629
if options.gzipCompressionEnabled {
2730
handler = gzhttp.GzipHandler(handler)
2831
}
@@ -31,8 +34,9 @@ func NewHandler(options HandlerOptions) http.Handler {
3134
handler = http.MaxBytesHandler(handler, int64(options.maxRequestBody))
3235
}
3336

34-
handler = NewRequestStartMiddleware(handler)
35-
handler = NewLoggingMiddleware(slog.Default(), handler)
37+
if options.logRequests {
38+
handler = NewLoggingMiddleware(slog.Default(), handler)
39+
}
3640

3741
return handler
3842
}

internal/handler_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,5 +293,6 @@ func handlerOptions(targetUrl string) HandlerOptions {
293293
maxCacheableResponseBody: 1024,
294294
badGatewayPage: "",
295295
forwardHeaders: true,
296+
logRequests: true,
296297
}
297298
}

internal/service.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ func (s *Service) Run() int {
2727
maxRequestBody: s.config.MaxRequestBody,
2828
badGatewayPage: s.config.BadGatewayPage,
2929
forwardHeaders: s.config.ForwardHeaders,
30+
logRequests: s.config.LogRequests,
3031
}
3132

3233
handler := NewHandler(handlerOptions)

0 commit comments

Comments
 (0)