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

Make slog Handler backwards compatible when logging custom errors #126

Open
ten4o opened this issue Jun 18, 2024 · 0 comments
Open

Make slog Handler backwards compatible when logging custom errors #126

ten4o opened this issue Jun 18, 2024 · 0 comments
Labels

Comments

@ten4o
Copy link

ten4o commented Jun 18, 2024

Current behavior

When using slog Handler to log custom errors, the error is not converted to string and is not logged correctly.
Simple example to reproduce the problem:

package main

import (
	"fmt"
	"log/slog"
	"os"

	"code.cloudfoundry.org/lager/v3"
)

type CustomError struct {
	error
	statusCode int
}

func NewCustomError(err error, statusCode int) error {
	return &CustomError{
		error:      err,
		statusCode: statusCode,
	}
}

func main() {
	lagerLog := lager.NewLogger("component")
	lagerLog.RegisterSink(lager.NewWriterSink(os.Stdout, lager.DEBUG))
	err := NewCustomError(fmt.Errorf("error message"), 401)

	lagerLog.Error("this works", err)

	slogLog := slog.New(lager.NewHandler(lagerLog))
	slogLog.Error("this does not work", "error", err)
}

This behaviour is not backwards compatible as for parameters of type error error.Error() is called in lager.Error() but not in the slog Handler.

Desired behavior

The desired behaviour is to call Error() for parameters of type error in the slog Handler.

Affected Version

v3.0.3

@ten4o ten4o added the bug label Jun 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

1 participant