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

Added configurable reqlog middleware #80

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
55 changes: 50 additions & 5 deletions extra/reqlog/middleware.go
Expand Up @@ -15,8 +15,11 @@ import (
)

type middleware struct {
enabled bool
verbose bool
enabled bool
verbose bool
enabledReferer bool
enabledRemoteAddr bool
enabledQuery bool
}

type Option func(m *middleware)
Expand All @@ -35,11 +38,36 @@ func WithVerbose(on bool) Option {
}
}

// WithEnabledReferer configures the middleware to log request referer.
func WithEnabledReferer(on bool) Option {
return func(m *middleware) {
m.enabledReferer = on
}
}

// WithEnabledRemoteAddr configures the middleware to log request address.
func WithEnabledRemoteAddr(on bool) Option {
return func(m *middleware) {
m.enabledRemoteAddr = on
}
}

// WithEnabledQuery configures the middleware to log request query params.
func WithEnabledQuery(on bool) Option {
return func(m *middleware) {
m.enabledQuery = on
}
}

// WithEnv configures the middleware using the environment variable value.
// For example, WithEnv("BUNDEBUG"):
// - BUNDEBUG=0 - disables the middleware.
// - BUNDEBUG=1 - enables the middleware.
// - BUNDEBUG=2 - enables the middleware and verbose mode.
// - BUNDEBUG=3 - enables the middleware and logs request referer.
// - BUNDEBUG=4 - enables the middleware and logs request remote address.
// - BUNDEBUG=5 - enables the middleware and logs request query params.

func FromEnv(keys ...string) Option {
if len(keys) == 0 {
keys = []string{"BUNDEBUG"}
Expand All @@ -49,6 +77,9 @@ func FromEnv(keys ...string) Option {
if env, ok := os.LookupEnv(key); ok {
m.enabled = env != "" && env != "0"
m.verbose = env == "2"
m.enabledReferer = env == "3"
m.enabledRemoteAddr = env == "4"
m.enabledQuery = env == "5"
break
}
}
Expand All @@ -57,8 +88,11 @@ func FromEnv(keys ...string) Option {

func NewMiddleware(opts ...Option) bunrouter.MiddlewareFunc {
c := &middleware{
enabled: true,
verbose: true,
enabled: true,
verbose: true,
enabledReferer: false,
enabledRemoteAddr: false,
enabledQuery: false,
}
for _, opt := range opts {
opt(c)
Expand Down Expand Up @@ -97,9 +131,20 @@ func (m *middleware) Middleware(next bunrouter.HandlerFunc) bunrouter.HandlerFun
formatStatus(statusCode),
fmt.Sprintf(" %10s ", dur.Round(time.Microsecond)),
formatMethod(req.Method),
req.URL.String(),
)

if m.enabledReferer {
args = append(args, req.Header.Get("Referer"))
}

if m.enabledRemoteAddr {
args = append(args, req.URL.String())
}

if m.enabledQuery {
args = append(args, req.URL.Query())
}

if err != nil {
typ := reflect.TypeOf(err).String()
args = append(args,
Expand Down