diff options
author | Steve Azzopardi <steveazz@outlook.com> | 2019-03-03 00:11:43 +0300 |
---|---|---|
committer | Steve Azzopardi <steveazz@outlook.com> | 2019-03-16 11:18:01 +0300 |
commit | 5665eceebf58993d5864f3add7b80a94b5f90ee7 (patch) | |
tree | a43a59e682f9e8ee7603b19c4299d79ea8399d64 /doc | |
parent | e4b2c3b08b0aced417e072e60d5b299144c40474 (diff) |
Add logging info to Go guide
closes https://gitlab.com/gitlab-org/gitlab-ce/issues/57298
Diffstat (limited to 'doc')
-rw-r--r-- | doc/development/go_guide/index.md | 33 |
1 files changed, 30 insertions, 3 deletions
diff --git a/doc/development/go_guide/index.md b/doc/development/go_guide/index.md index cdc806a2d31..7fd41c5e01f 100644 --- a/doc/development/go_guide/index.md +++ b/doc/development/go_guide/index.md @@ -161,12 +161,39 @@ in the code. ### Logging -The usage of a logging library is strongly recommended for daemons. Even though -there is a `log` package in the standard library, we generally use -[logrus](https://github.com/sirupsen/logrus). Its plugin ("hooks") system +The usage of a logging library is strongly recommended for daemons. Even +though there is a `log` package in the standard library, we generally use +[Logrus](https://github.com/sirupsen/logrus). Its plugin ("hooks") system makes it a powerful logging library, with the ability to add notifiers and formatters at the logger level directly. +#### Structured (JSON) logging + +Every binary ideally must have structured (JSON) logging in place as it helps +with searching and filtering the logs. At GitLab we use structured logging in +JSON format, as all our infrastructure assumes that. When using +[Logrus](https://github.com/sirupsen/logrus) you can turn on structured +logging simply by using the build in [JSON +formatter](https://github.com/sirupsen/logrus#formatters). This follows the +same logging type we use in our [Ruby +applications](../logging.md#use-structured-json-logging). + +#### How to use Logrus + +There are a few guidelines one should follow when using the +[Logrus](https://github.com/sirupsen/logrus) package: + +- When printing an error use + [WithError](https://godoc.org/github.com/sirupsen/logrus#WithError). For + exmaple, `logrus.WithError(err).Error("Failed to do something")`. +- Since we use [structured logging](#structured-json-logging) we can log + fields in the context of that code path, such as the URI of the request using + [`WithField`](https://godoc.org/github.com/sirupsen/logrus#WithField) or + [`WithFields`](https://godoc.org/github.com/sirupsen/logrus#WithFields). For + example, `logrus.WithField("file", "/app/go).Info("Opening dir")`. If you + have to log multiple keys, always use `WithFields` instead of calling + `WithField` more than once. + ### Tracing and Correlation [LabKit](https://gitlab.com/gitlab-org/labkit) is a place to keep common |