-
Notifications
You must be signed in to change notification settings - Fork 68
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 structured logging #796
Conversation
d8f64ed
to
d120808
Compare
CHANGELOG.md
Outdated
@@ -12,6 +12,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 | |||
|
|||
### Added | |||
|
|||
* Structured logging following JSON ECS format [#796](https://github.com/elastic/package-registry/pull/786). |
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.
Changing the log structure shloud be considered a breaking change.
In the context of changing the log format I've been thinking about how we could make it less breaking for users that collect the logs, especially if it is collected with elastic-agent. The package-registry is its own service which I think also deserves its own package. If we have a package, we could ensure that both log formats can be processed. Also it means we can make sure that we have dashboards versioned and link the apm visualizations. |
d120808
to
14b7674
Compare
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.
LGTM
Is there any other benefit coming from this PR apart from cleanup and a nice console logger for development purposes? I mean, do we log more data or include error context?
For most of requests it is the same data, plus timestamps with timezone and caller file and line. This is one of the lines printed on startup when reading the packages:
For requests we log more data: Before:
After:
We also have log levels now. |
Btw, I wonder if we should log "access" and "error" logs to different streams, as is usual in web servers. We could log "access" logs to stdout, and everything else to stderr. @mtojek wdyt? |
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.
Btw, I wonder if we should log "access" and "error" logs to different streams, as is usual in web servers. We could log "access" logs to stdout, and everything else to stderr. @mtojek wdyt?
I'm fine with such a change if the docker logs
command still works so that we will miss neither access nor error logs.
@mtojek I found that I was not capturing valuable fields from the url, as the path and the query. I have added this and a couple of fields more. And a test for the captured fields. Could you please take another look? |
Umm, yes, let's leave this for a separate change, it can have unexpected implications. |
util/logging.go
Outdated
if user := req.URL.User; user != nil { | ||
if username := user.Username(); username != "" { | ||
fields = append(fields, zap.String("user.name", username)) | ||
fields = append(fields, zap.String("url.username", username)) | ||
} | ||
} |
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.
Hm, we don't expect any users here, right?
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.
Correct, let's remove this.
Add structured logging using JSON ECS format. This includes much more information in logs for http requests. As it uses JSON ECS, it doesn't need special parsing.
-log-level
can be used to select log level.Logger is a global singleton in the
util
package. Not ideal, but was not trivial to pass the logger to all the components. We can iterate on this though.Why am I using
zap
for structured logging? It is one of the two Go loggers currently supported by Elastic for ECS logging. The other one is Logrus. I chosezap
because it is supposed to be faster, but I don't have a strong opinion.Example lines now:
With
-log-type dev
a "developer mode" can be enabled, that logs like this:Follow up of #795.