-
Notifications
You must be signed in to change notification settings - Fork 507
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
add logging middleware for server #785
Conversation
Here's how the logs look :
In JSON fomat :
|
Codecov Report
@@ Coverage Diff @@
## master #785 +/- ##
==========================================
- Coverage 75.20% 74.99% -0.22%
==========================================
Files 112 113 +1
Lines 3453 3463 +10
==========================================
Hits 2597 2597
- Misses 665 675 +10
Partials 191 191
|
pkg/http-server/start.go
Outdated
@@ -54,14 +56,20 @@ func (g *APIServer) start(routes []*Route, port, certFile, privateKeyFile string | |||
router = mux.NewRouter() // new router | |||
) | |||
|
|||
logWriter := logging.GetLogWriter(logger) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am wondering, why do we need a separate logwriter? The already created logger
won't help in this case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the logwriter consumes the logger.
logger object doesn't implement the Writer interface. so had to create a logwriter object that does.
pkg/logging/logwriter.go
Outdated
return buf | ||
} | ||
|
||
// WriteRequestLog logs an http(s) request to a write object |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if gorillaHandlers
should be a part of the logging package? Looks more like a candiate for http
utils.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but its logging :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the log formatter which is used by the gorilla handler would be only writing http(s) logs. So, I agree with @kanchwala-yusuf.
the log formatter (if required) can be moved.
pkg/logging/logwriter.go
Outdated
// buildCommonLogLine builds a log entry for req in Apache Common Log Format. | ||
// ts is the timestamp with which the entry should be logged. | ||
// status and size are used to provide the response HTTP status and size. | ||
func buildCommonLogLine(req *http.Request, url url.URL, ts time.Time, status int, size int) []byte { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This functions accepts a var of type time.Time
but never uses it.
pkg/http-server/start.go
Outdated
} | ||
|
||
router.NotFoundHandler = gorillaHandlers.CustomLoggingHandler(logWriter, http.HandlerFunc(httputils.NotFound), logging.WriteRequestLog) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was the sole purpose of using a custom logging handler to remove the timestamp that is logged by the default log formatter provided by gorilla handlers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nope. its more than that. its apache standard http server log format.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The reason I asked is because, it seems to be the only difference between the default log formatter and the log formatter you have implemented. Can you please check once?
https://github.com/gorilla/handlers/blob/master/logging.go#L144
pkg/logging/logwriter.go
Outdated
buf = append(buf, ` "`...) | ||
buf = append(buf, req.Method...) | ||
buf = append(buf, " "...) | ||
buf = append(buf, uri...) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since the log writer only logs at the info
level, we would end up logging the webhook apikey
at info level. Is that expected?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch. @kanchwala-yusuf wdyt? would that we severe security threat?
Updated the PR as per the reviews. PTAL |
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
fixes #784