Skip to content

Commit

Permalink
added option to specify loglevel
Browse files Browse the repository at this point in the history
  • Loading branch information
na4ma4 committed Apr 4, 2024
1 parent a0caa1b commit 28549ca
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
5 changes: 4 additions & 1 deletion handler.go
Expand Up @@ -160,7 +160,8 @@ func writeLog(lh *loggingHandler, req *http.Request, url url.URL, ts time.Time,
zapFieldOrSkip(lh.opts.includeXForwardedFor, zap.String("forwarded_for", req.Header.Get("X-Forwarded-For"))), // 12
}

lh.logger.Info(
lh.logger.Log(
lh.opts.logLevel,
"Request",
fields...,
)
Expand All @@ -173,6 +174,7 @@ func LoggingHTTPHandler(logger *zap.Logger, httpHandler http.Handler, opts ...lo
includeTiming: true,
includeTimestamp: true,
includeXForwardedFor: false,
logLevel: zap.InfoLevel,
}

for _, f := range opts {
Expand All @@ -193,6 +195,7 @@ func LoggingHTTPHandlerWrapper(logger *zap.Logger, opts ...loggingOptionsFunc) f
includeTiming: true,
includeTimestamp: true,
includeXForwardedFor: false,
logLevel: zap.InfoLevel,
}

for _, f := range opts {
Expand Down
13 changes: 13 additions & 0 deletions options.go
@@ -1,9 +1,12 @@
package zaptool

import "go.uber.org/zap/zapcore"

type loggingOptions struct {
includeTiming bool
includeTimestamp bool
includeXForwardedFor bool
logLevel zapcore.Level
}

type loggingOptionsFunc func(o *loggingOptions)
Expand Down Expand Up @@ -34,3 +37,13 @@ func LoggingOptionForwardedFor(state bool) loggingOptionsFunc {
o.includeXForwardedFor = state
}
}

// LoggingOptionLogLevel defines the log level that http messages should output to,
// defaults to Info.
//
//nolint:revive // deliberately not-exported function type.
func LoggingOptionLogLevel(level zapcore.Level) loggingOptionsFunc {
return func(o *loggingOptions) {
o.logLevel = level
}
}

0 comments on commit 28549ca

Please sign in to comment.