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

fix: handle any type that implements error interface #51

Closed
wants to merge 1 commit into from
Closed

fix: handle any type that implements error interface #51

wants to merge 1 commit into from

Conversation

sethyates
Copy link

Description of this change

This change enables tint to accept an error as a normal slog.Value, without having to be wrapped by the internal taintError struct. This means we don't have to reference tint at locations where we log errors, just slog.

Why is this change being made?

  • Chore (non-functional changes)
  • Bug fix (non-breaking change that fixes an issue)
  • New feature (non-breaking change that adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

How was this tested? How can the reviewer verify your testing?

Ran locally in a simple test setup:

package main

import (
	"github.com/lmittmann/tint"
	"log/slog"
)

func main() {
	h := &Handler{handler: tint.NewHandler(os.Stdout, &tint.Options{
		AddSource: true,
	})}
	logger := slog.New(h)
	logger.With("a", "b").With("err", errors.New("oops")).Info("err is not red on main, but is on fork")
	logger.With("a", "b").With(tint.Err(errors.New("oops"))).Error("err is red on both main and fork")
}

Also ran unit tests:

❯ TZ="" go test -tags=faketime -v ./...
=== RUN   TestHandler
=== RUN   TestHandler/0
=== RUN   TestHandler/1
=== RUN   TestHandler/2
=== RUN   TestHandler/3
=== RUN   TestHandler/4
=== RUN   TestHandler/5
=== RUN   TestHandler/6
=== RUN   TestHandler/7
=== RUN   TestHandler/8
=== RUN   TestHandler/9
=== RUN   TestHandler/10
=== RUN   TestHandler/11
=== RUN   TestHandler/12
=== RUN   TestHandler/13
=== RUN   TestHandler/14
=== RUN   TestHandler/15
=== RUN   TestHandler/16
=== RUN   TestHandler/17
=== RUN   TestHandler/18
=== RUN   TestHandler/19
=== RUN   TestHandler/20
=== RUN   TestHandler/21
=== RUN   TestHandler/22
=== RUN   TestHandler/23
=== RUN   TestHandler/24
=== RUN   TestHandler/25
=== RUN   TestHandler/26
=== RUN   TestHandler/27
=== RUN   TestHandler/28
=== RUN   TestHandler/29
=== RUN   TestHandler/30
=== RUN   TestHandler/31
=== RUN   TestHandler/32
=== RUN   TestHandler/33
=== RUN   TestHandler/34
--- PASS: TestHandler (0.00s)
    --- PASS: TestHandler/0 (0.00s)
    --- PASS: TestHandler/1 (0.00s)
    --- PASS: TestHandler/2 (0.00s)
    --- PASS: TestHandler/3 (0.00s)
    --- PASS: TestHandler/4 (0.00s)
    --- PASS: TestHandler/5 (0.00s)
    --- PASS: TestHandler/6 (0.00s)
    --- PASS: TestHandler/7 (0.00s)
    --- PASS: TestHandler/8 (0.00s)
    --- PASS: TestHandler/9 (0.00s)
    --- PASS: TestHandler/10 (0.00s)
    --- PASS: TestHandler/11 (0.00s)
    --- PASS: TestHandler/12 (0.00s)
    --- PASS: TestHandler/13 (0.00s)
    --- PASS: TestHandler/14 (0.00s)
    --- PASS: TestHandler/15 (0.00s)
    --- PASS: TestHandler/16 (0.00s)
    --- PASS: TestHandler/17 (0.00s)
    --- PASS: TestHandler/18 (0.00s)
    --- PASS: TestHandler/19 (0.00s)
    --- PASS: TestHandler/20 (0.00s)
    --- PASS: TestHandler/21 (0.00s)
    --- PASS: TestHandler/22 (0.00s)
    --- PASS: TestHandler/23 (0.00s)
    --- PASS: TestHandler/24 (0.00s)
    --- PASS: TestHandler/25 (0.00s)
    --- PASS: TestHandler/26 (0.00s)
    --- PASS: TestHandler/27 (0.00s)
    --- PASS: TestHandler/28 (0.00s)
    --- PASS: TestHandler/29 (0.00s)
    --- PASS: TestHandler/30 (0.00s)
    --- PASS: TestHandler/31 (0.00s)
    --- PASS: TestHandler/32 (0.00s)
    --- PASS: TestHandler/33 (0.00s)
    --- PASS: TestHandler/34 (0.00s)
=== RUN   TestReplaceAttr
=== RUN   TestReplaceAttr/0
=== RUN   TestReplaceAttr/1
=== RUN   TestReplaceAttr/2
=== RUN   TestReplaceAttr/3
--- PASS: TestReplaceAttr (0.00s)
    --- PASS: TestReplaceAttr/0 (0.00s)
    --- PASS: TestReplaceAttr/1 (0.00s)
    --- PASS: TestReplaceAttr/2 (0.00s)
    --- PASS: TestReplaceAttr/3 (0.00s)
=== RUN   Example
11:00PM INF Starting server addr=:8080 env=production
11:00PM DBG Connected to DB db=myapp host=localhost:5432
11:00PM WRN Slow request method=GET path=/users duration=497ms
11:00PM ERR DB connection lost err="connection reset" db=myapp
--- PASS: Example (0.00s)
PASS
ok      github.com/lmittmann/tint       (cached)

Related issues

Checklist

  • I have added tests to cover my changes.
  • All new and existing tests passed.

@lmittmann lmittmann closed this Nov 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants