updated docs against format strings + correct function With()

This commit is contained in:
miampf 2024-01-22 15:06:30 +01:00
parent 35755c6d12
commit 0c92fa9998
No known key found for this signature in database
GPG key ID: 376EAC0E5307A669

View file

@ -40,22 +40,26 @@ Further we try to adhere to the following guidelines:
log.Error("A critical error occurred!")
```
* Use the `WithAttrs()` method to add structured context to your log messages. The context tags should be easily searchable to allow for easy log filtering. Try to keep consistent tag naming!
* Use the `With()` method to add structured context to your log messages. The context tags should be easily searchable to allow for easy log filtering. Try to keep consistent tag naming!
Example:
```Go
log.WithAttrs(slog.Any("error" someError), slog.String("ip", "192.0.2.1")).Error("Connecting to IP failed")
log.With(slog.Any("error" someError), slog.String("ip", "192.0.2.1")).Error("Connecting to IP failed")
```
* Slog does not support format strings out of the box. You have to wrap any format strings in `fmt.Sprintf()`.
Example:
* Log messages may use format strings to produce human readable messages. However, the information should also be present as structured context fields if it might be valuable for debugging purposes. So, instead of writing
```Go
log.Info(fmt.Sprintf("Starting server on %s:%s", addr, port))
```
You should write
```Go
log.With(slog.String("address", addr), slog.Int("port", port)).Info("Starting server")
```
* Use log levels to configure how detailed the logs of you application should be.
* `Debug()` for log low level and detailed information. This may include variable dumps, but should not disclose sensitive information, e.g. keys or secret tokens.