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

Log errors using slog.Logger #1849

Merged
merged 2 commits into from
May 16, 2024
Merged

Log errors using slog.Logger #1849

merged 2 commits into from
May 16, 2024

Conversation

maraino
Copy link
Contributor

@maraino maraino commented May 15, 2024

This commit allows logging errors in a slog.Logger injected in the context. This logger type is not currently used directly in step-ca, but this will change in the future.

The main change is in api/log/log.go; the rest is adapting the code to allow this.

I've considered using func Error(ctx context.Context, w http.ResponseWriter, err error), but this won't allow us to add extra things from the request to the log if we need to.

I've also had to change the render methods because they can call Error.

cc: @hslatman

This commit allows logging errors in a slog.Logger injected in the
context. This type of logger is not currently used directly in step-ca,
but this will change in the future.
@maraino maraino requested a review from azazeal May 15, 2024 22:41
@github-actions github-actions bot added the needs triage Waiting for discussion / prioritization by team label May 15, 2024
api/log/log.go Outdated
type loggerKey struct{}

// NewContext creates a new context with the given slog.Logger.
func NewContext(ctx context.Context, logger *slog.Logger) context.Context {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that instead of attaching a slog.Logger reference to the context.Context, we may benefit more by attaching a func. This way we can inject this concern in more elastic ways in the future.

Thoughts?

type HandlerErrorLogger func(http.ResponseWriter, *http.Request, error)

func (fn HandlerErrorLogger) call(w http.ResponseWriter, r *http.Request, err error) {
	if fn == nil {
		return // noop or use a sensible default (print to stderr via slog, etc)
	}

	fn(w, r, err)
}

func WithHandlerErrorLogger(ctx context.Context, fn HandlerErrorLogger) context.Context {
	// ...
}

func HandlerErrorLoggerFromContext(ctx context.Context) HandlerErrorLogger {
	// ...
}

func Error(rw http.ResponseWriter, r *http.Request, err error) {
	HandlerErrorLoggerFromContext(r.Context()).call(rw, r, err)
	// ...
}

Thoughts?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed with c0b7c33

This commit addresses comment in the code review. Now, instead of
injecting an slog.Logger we can inject any method that can use a more
flexible implementation.
@maraino maraino requested a review from azazeal May 16, 2024 18:02
@maraino maraino merged commit 14959db into master May 16, 2024
13 checks passed
@maraino maraino deleted the mariano/log-errors branch May 16, 2024 20:51
@hslatman hslatman added this to the v0.26.2 milestone May 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs triage Waiting for discussion / prioritization by team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants