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

use log/slog for structured logging #3502

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

seankhliao
Copy link
Contributor

@seankhliao seankhliao commented May 3, 2024

Overview

Switches pkg/log and logrus for the standard library's slog logger.

What this PR does / why we need it

This is more or less a mechanical replacement of the internal log.Logger interface and logrus logger with the standard library's slog.
This appears to be the general community consensus on a standard logging interface, where slog.Logger is used as the common interface, and implementations can switch out the slog.Handler if necessary.

Connectors have their type and id added as attributes.
Log lines are lowercased for consistency.

Closes #2020

Special notes for your reviewer

If dex is used as a library, this will be a breaking change.
Log line output will be different from before.

Copy link
Member

@sagikazarmark sagikazarmark left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @seankhliao, I really appreciate you tackling this.

I'm mostly satisfied with how things look. I made a few comments, curious what you think.

cmd/dex/serve.go Outdated
Comment on lines 103 to 105
"go_version", runtime.Version(),
"go_os", runtime.GOOS,
"go_arch", runtime.GOARCH,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I wonder if using slog's group feature here would make sense. As far as I understand, that basically means using dot-notated labels (ie. go.version instead of go_version).

https://pkg.go.dev/log/slog#hdr-Groups

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to use groups.
It'll be go.version in the text output, and in json "go": {"version": ...}

cmd/dex/serve.go Outdated
Comment on lines 546 to 529
formatter.f = &logrus.TextFormatter{DisableColors: true}
slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})
case "json":
formatter.f = &logrus.JSONFormatter{}
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: level,
})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although I don't think it makes a lot of difference, this changes where logs go (stdout instead of stderr).

We should at least highlight it somewhere.

WDYT @nabokihms ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to stdout. I just wasn't sure which one it should have gone to.

Copy link
Member

@nabokihms nabokihms May 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be a problem, but mentioning it in the changelog should be fine enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I meant I changed it back to stderr.

if c.BaseURL == "" {
return nil, fmt.Errorf("crowd: no baseURL provided for crowd connector")
}
return &crowdConnector{Config: *c, logger: logger}, nil
return &crowdConnector{Config: *c, logger: logger.With("connector_type", "atlassiancrowd", "connector_id", id)}, nil
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing about connectors: maybe use slog.Group?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to use groups too

@seankhliao seankhliao force-pushed the slog branch 3 times, most recently from a5a5908 to 45e201f Compare May 13, 2024 11:46
connector/ldap/ldap.go Outdated Show resolved Hide resolved
@nabokihms nabokihms added the release-note/new-feature Release note: Exciting New Features label May 13, 2024
Signed-off-by: Sean Liao <sean+git@liao.dev>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note/new-feature Release note: Exciting New Features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Introduce structured logging
3 participants