Skip to content
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

Consider switching to structured logs (slog) #450

Closed
manugarg opened this issue Aug 8, 2023 · 5 comments
Closed

Consider switching to structured logs (slog) #450

manugarg opened this issue Aug 8, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@manugarg
Copy link
Contributor

manugarg commented Aug 8, 2023

Go comes with its own structured logger now:
https://pkg.go.dev/log/slog.

We should consider switching to it for generic logging:

func genericLog(severity logging.Severity, name string, s string) {

@manugarg manugarg added the enhancement New feature or request label Aug 8, 2023
@manugarg manugarg self-assigned this Aug 8, 2023
@manugarg
Copy link
Contributor Author

manugarg commented Aug 8, 2023

This will also take care of #406.

@manugarg
Copy link
Contributor Author

manugarg commented Aug 8, 2023

Note: slog is available only with Go 1.21, so we'll have to first upgrade to Go 1.21. Alternatively we can use the experimental version for now:
https://pkg.go.dev/golang.org/x/exp/slog

@manugarg
Copy link
Contributor Author

manugarg commented Aug 9, 2023

The way I envision this:

  • We'll make cloudprober logger use slog.Logger.
  • When we create new Logger for each module, we'll add some attributes to the logger.. something like:
    logger.New().With("probe", "classic_http_probe")
  • We'll also allow adding attributes to individual log lines. For this we'll implement new set of functions: InfoAttrs, WarnAttrs, etc, similar to slog.Logger's functions.
  • We'll keep the existing implementation of Info, Infof, Warning, Warningf, etc.

@manugarg
Copy link
Contributor Author

#462 with the possible implementation.

@manugarg manugarg added this to the v0.12.9 milestone Aug 16, 2023
@manugarg
Copy link
Contributor Author

manugarg commented Aug 16, 2023

And, it's done. New logs look like this:

time=2023-08-17T14:06:35.809-07:00 level=INFO source=cloudprober/probes/http/http.go:533 msg="Probe(staging_companies) targets updated: map[api-gateway_0:ADD]" system=cloudprober probe=staging_companies
time=2023-08-17T14:06:37.095-07:00 level=INFO source=cloudprober/common/oauth/http_token.go:62 msg="oauth2: response from token URL: {\"access_token\":\"eyJ ........ z6i7SxS35qzREAcjJQ\"}" system=cloudprober probe=staging_companies

We can now export logs in JSON format as well, using the flag -logfmt=json

{"time":"2023-08-17T14:08:57.574018-07:00","level":"INFO","source":{"function":"github.com/cloudprober/cloudprober/sysvars.Start","file":"/Users/mgarg/code/cloudprober/sysvars/sysvars.go","line":185},"msg":"1692306537 labels=ptype=sysvars,probe=sysvars hostname=\"aaa.local\" start_timestamp=\"1692306537\" version=\"\"","system":"cloudprober","component":"sysvars"}
{"time":"2023-08-17T14:08:58.007281-07:00","level":"INFO","source":{"function":"github.com/cloudprober/cloudprober/common/oauth.(*httpTokenSource).tokenFromHTTP","file":"cloudprober/common/oauth/http_token.go","line":62},"msg":"oauth2: response from token URL: {\"access_token\":\"eyJ ........ z6i7SxS35qzREAcjJQ\"}","system":"cloudprober","probe":"staging_companies"}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant