Code Style: Prefer WithFields over Infof (#349)
* Code Style: Prefer WithFields over Infof * Update CODE_STYLE.md
This commit is contained in:
parent
e5f5bb5961
commit
d44dc2d5e6
|
@ -52,6 +52,29 @@ github. These can be added just before merging of the PR to master, and the
|
||||||
issue number should be added to the comment, e.g. `// TODO(#324): ...`
|
issue number should be added to the comment, e.g. `// TODO(#324): ...`
|
||||||
|
|
||||||
|
|
||||||
|
## Logging
|
||||||
|
|
||||||
|
We generally prefer to log with static log messages and include any dynamic
|
||||||
|
information in fields.
|
||||||
|
|
||||||
|
```golang
|
||||||
|
logger := util.GetLogger(ctx)
|
||||||
|
|
||||||
|
// Not recommended
|
||||||
|
logger.Infof("Finished processing keys for %s, number of keys %d", name, numKeys)
|
||||||
|
|
||||||
|
// Recommended
|
||||||
|
logger.WithFields(logrus.Fields{
|
||||||
|
"numberOfKeys": numKeys,
|
||||||
|
"entityName": name,
|
||||||
|
}).Info("Finished processing keys")
|
||||||
|
```
|
||||||
|
|
||||||
|
This is useful when logging to systems that natively understand log fields, as
|
||||||
|
it allows people to search and process the fields without having to parse the
|
||||||
|
log message.
|
||||||
|
|
||||||
|
|
||||||
## Visual Studio Code
|
## Visual Studio Code
|
||||||
|
|
||||||
If you use VSCode then the following is an example of a workspace setting that
|
If you use VSCode then the following is an example of a workspace setting that
|
||||||
|
|
Loading…
Reference in a new issue