Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding configuration to include the request headers in the log #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions logger_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package logger

import "github.com/sirupsen/logrus"

type LoggerConfig struct {
level logrus.Level
includeRequestHeaders bool
}

type LoggerConfigBuilder struct {
loggerConfig *LoggerConfig
}

func NewLoggerConfigBuilder() *LoggerConfigBuilder {
loggerConfig := &LoggerConfig{
level: logrus.InfoLevel,
includeRequestHeaders: false,
}

return &LoggerConfigBuilder{
loggerConfig: loggerConfig,
}
}

func (lcb *LoggerConfigBuilder) WithLoggingLevel(level logrus.Level) *LoggerConfigBuilder {
lcb.loggerConfig.level = level
return lcb
}

func (lcb *LoggerConfigBuilder) WithRequestHeadersIncluded() *LoggerConfigBuilder {
lcb.loggerConfig.includeRequestHeaders = true
return lcb
}

func (lcb *LoggerConfigBuilder) Build() LoggerConfig {
return *lcb.loggerConfig
}
46 changes: 46 additions & 0 deletions logger_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package logger

import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"testing"
)

func TestDefaultConfig(t *testing.T) {
config := NewLoggerConfigBuilder().Build()

assert.NotNil(t, config)
assert.False(t, config.includeRequestHeaders)
assert.Equal(t, logrus.InfoLevel, config.level)
}

func TestConfigWithDebugLevel(t *testing.T) {
config := NewLoggerConfigBuilder().
WithLoggingLevel(logrus.DebugLevel).
Build()

assert.NotNil(t, config)
assert.False(t, config.includeRequestHeaders)
assert.Equal(t, logrus.DebugLevel, config.level)
}

func TestConfigWithIncludedHeaders(t *testing.T) {
config := NewLoggerConfigBuilder().
WithRequestHeadersIncluded().
Build()

assert.NotNil(t, config)
assert.True(t, config.includeRequestHeaders)
assert.Equal(t, logrus.InfoLevel, config.level)
}

func TestConfigWithDebugLevelAndIncludedHeaders(t *testing.T) {
config := NewLoggerConfigBuilder().
WithLoggingLevel(logrus.DebugLevel).
WithRequestHeadersIncluded().
Build()

assert.NotNil(t, config)
assert.True(t, config.includeRequestHeaders)
assert.Equal(t, logrus.DebugLevel, config.level)
}
13 changes: 12 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ func Logger(category string, logger logrus.FieldLogger) func(h http.Handler) htt

// Logger returns a request logging middleware
func LoggerWithLevel(category string, logger logrus.FieldLogger, level logrus.Level) func(h http.Handler) http.Handler {
config := NewLoggerConfigBuilder().
WithLoggingLevel(level).
Build()

return LoggerWithConfig(category, logger, config)
}

func LoggerWithConfig(category string, logger logrus.FieldLogger, config LoggerConfig) func(h http.Handler) http.Handler {
return func(h http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
reqID := middleware.GetReqID(r.Context())
Expand Down Expand Up @@ -46,7 +54,10 @@ func LoggerWithLevel(category string, logger logrus.FieldLogger, level logrus.Le
if len(reqID) > 0 {
fields["request_id"] = reqID
}
logger.WithFields(fields).Logf(level, "%s://%s%s", scheme, r.Host, r.RequestURI)
if config.includeRequestHeaders {
fields["request_headers"] = r.Header
}
logger.WithFields(fields).Logf(config.level, "%s://%s%s", scheme, r.Host, r.RequestURI)
}()

h.ServeHTTP(ww, r)
Expand Down
25 changes: 25 additions & 0 deletions middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,30 @@ func TestLoggerWithLevelTrace(t *testing.T) {
testLoggerWithLevel(t, logrus.TraceLevel)
}

func TestLoggerWithConfig(t *testing.T) {
req := httptest.NewRequest("GET", "/", nil)
req.Host = "localhost:8080"

logger, hook := test.NewNullLogger()
logger.SetLevel(logrus.TraceLevel)

config := NewLoggerConfigBuilder().
WithLoggingLevel(logrus.DebugLevel).
WithRequestHeadersIncluded().
Build()

testRequest(t, req, LoggerWithConfig("router", logger, config))

assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, logrus.DebugLevel, hook.LastEntry().Level)
assert.Nil(t, hook.LastEntry().Data["request_id"])
assert.NotNil(t, hook.LastEntry().Data["request_headers"])
assert.Equal(t, "GET", hook.LastEntry().Data["method"])
assert.Equal(t, 11, hook.LastEntry().Data["bytes"])
assert.Greater(t, hook.LastEntry().Data["duration"], int64(0))
assert.Equal(t, "http://localhost:8080/", hook.LastEntry().Message)
}

func testLoggerWithLevel(t *testing.T, level logrus.Level) {
req := httptest.NewRequest("GET", "/", nil)
req.Host = "localhost:8080"
Expand All @@ -73,6 +97,7 @@ func testLoggerWithLevel(t *testing.T, level logrus.Level) {
assert.Equal(t, 1, len(hook.Entries))
assert.Equal(t, level, hook.LastEntry().Level)
assert.Nil(t, hook.LastEntry().Data["request_id"])
assert.Nil(t, hook.LastEntry().Data["request_headers"])
assert.Equal(t, "GET", hook.LastEntry().Data["method"])
assert.Equal(t, 11, hook.LastEntry().Data["bytes"])
assert.Greater(t, hook.LastEntry().Data["duration"], int64(0))
Expand Down