Skip to content

Commit 42bfdac

Browse files
lmittmannclaude
andauthored
refactor: rename NewHandler to NewTextHandler (#109)
Keep NewHandler as a deprecated wrapper with a //go:fix inline directive so go fix (Go 1.26+) rewrites call sites automatically. Co-authored-by: lmittmann <lmittmann@users.noreply.github.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent 9f1df8f commit 42bfdac

3 files changed

Lines changed: 32 additions & 22 deletions

File tree

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ go get github.com/lmittmann/tint
2727
w := os.Stderr
2828

2929
// Create a new logger
30-
logger := slog.New(tint.NewHandler(w, nil))
30+
logger := slog.New(tint.NewTextHandler(w, nil))
3131

3232
// Set global logger with custom options
3333
slog.SetDefault(slog.New(
34-
tint.NewHandler(w, &tint.Options{
34+
tint.NewTextHandler(w, &tint.Options{
3535
Level: slog.LevelDebug,
3636
TimeFormat: time.Kitchen,
3737
}),
@@ -49,7 +49,7 @@ for details.
4949
const LevelTrace = slog.LevelDebug - 4
5050

5151
w := os.Stderr
52-
logger := slog.New(tint.NewHandler(w, &tint.Options{
52+
logger := slog.New(tint.NewTextHandler(w, &tint.Options{
5353
Level: LevelTrace,
5454
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
5555
if a.Key == slog.LevelKey && len(groups) == 0 {
@@ -67,7 +67,7 @@ logger := slog.New(tint.NewHandler(w, &tint.Options{
6767
// Create a new logger that doesn't write the time
6868
w := os.Stderr
6969
logger := slog.New(
70-
tint.NewHandler(w, &tint.Options{
70+
tint.NewTextHandler(w, &tint.Options{
7171
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
7272
if a.Key == slog.TimeKey && len(groups) == 0 {
7373
return slog.Attr{}
@@ -82,7 +82,7 @@ logger := slog.New(
8282
// Create a new logger that writes all errors in red
8383
w := os.Stderr
8484
logger := slog.New(
85-
tint.NewHandler(w, &tint.Options{
85+
tint.NewTextHandler(w, &tint.Options{
8686
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
8787
if a.Value.Kind() == slog.KindAny {
8888
if _, ok := a.Value.Any().(error); ok {
@@ -104,7 +104,7 @@ e.g., the [`go-isatty`](https://github.com/mattn/go-isatty) package:
104104
```go
105105
w := os.Stderr
106106
logger := slog.New(
107-
tint.NewHandler(w, &tint.Options{
107+
tint.NewTextHandler(w, &tint.Options{
108108
NoColor: !isatty.IsTerminal(w.Fd()),
109109
}),
110110
)
@@ -118,6 +118,6 @@ Color support on Windows can be added by using e.g., the
118118
```go
119119
w := os.Stderr
120120
logger := slog.New(
121-
tint.NewHandler(colorable.NewColorable(w), nil),
121+
tint.NewTextHandler(colorable.NewColorable(w), nil),
122122
)
123123
```

handler.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Create a new logger with a custom TRACE level:
1717
const LevelTrace = slog.LevelDebug - 4
1818
1919
w := os.Stderr
20-
logger := slog.New(tint.NewHandler(w, &tint.Options{
20+
logger := slog.New(tint.NewTextHandler(w, &tint.Options{
2121
Level: LevelTrace,
2222
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
2323
if a.Key == slog.LevelKey && len(groups) == 0 {
@@ -34,7 +34,7 @@ Create a new logger that doesn't write the time:
3434
3535
w := os.Stderr
3636
logger := slog.New(
37-
tint.NewHandler(w, &tint.Options{
37+
tint.NewTextHandler(w, &tint.Options{
3838
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
3939
if a.Key == slog.TimeKey && len(groups) == 0 {
4040
return slog.Attr{}
@@ -48,7 +48,7 @@ Create a new logger that writes all errors in red:
4848
4949
w := os.Stderr
5050
logger := slog.New(
51-
tint.NewHandler(w, &tint.Options{
51+
tint.NewTextHandler(w, &tint.Options{
5252
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
5353
if a.Value.Kind() == slog.KindAny {
5454
if _, ok := a.Value.Any().(error); ok {
@@ -68,7 +68,7 @@ e.g., the [go-isatty] package:
6868
6969
w := os.Stderr
7070
logger := slog.New(
71-
tint.NewHandler(w, &tint.Options{
71+
tint.NewTextHandler(w, &tint.Options{
7272
NoColor: !isatty.IsTerminal(w.Fd()),
7373
}),
7474
)
@@ -79,7 +79,7 @@ Color support on Windows can be added by using e.g., the [go-colorable] package:
7979
8080
w := os.Stderr
8181
logger := slog.New(
82-
tint.NewHandler(colorable.NewColorable(w), nil),
82+
tint.NewTextHandler(colorable.NewColorable(w), nil),
8383
)
8484
8585
[zerolog.ConsoleWriter]: https://pkg.go.dev/github.com/rs/zerolog#ConsoleWriter
@@ -152,9 +152,9 @@ func (o *Options) setDefaults() {
152152
}
153153
}
154154

155-
// NewHandler creates a [slog.Handler] that writes tinted logs to Writer w,
155+
// NewTextHandler creates a [slog.Handler] that writes tinted logs to Writer w,
156156
// using the default options. If opts is nil, the default options are used.
157-
func NewHandler(w io.Writer, opts *Options) slog.Handler {
157+
func NewTextHandler(w io.Writer, opts *Options) slog.Handler {
158158
if opts == nil {
159159
opts = &Options{}
160160
}
@@ -167,6 +167,16 @@ func NewHandler(w io.Writer, opts *Options) slog.Handler {
167167
}
168168
}
169169

170+
// NewHandler creates a [slog.Handler] that writes tinted logs to Writer w,
171+
// using the default options. If opts is nil, the default options are used.
172+
//
173+
// Deprecated: Use [NewTextHandler] instead.
174+
//
175+
//go:fix inline
176+
func NewHandler(w io.Writer, opts *Options) slog.Handler {
177+
return NewTextHandler(w, opts)
178+
}
179+
170180
// handler implements a [slog.Handler].
171181
type handler struct {
172182
attrsPrefix string

handler_test.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020

2121
func Example() {
2222
w := os.Stderr
23-
logger := slog.New(tint.NewHandler(w, &tint.Options{
23+
logger := slog.New(tint.NewTextHandler(w, &tint.Options{
2424
Level: slog.LevelDebug,
2525
TimeFormat: time.Kitchen,
2626
}))
@@ -34,7 +34,7 @@ func Example() {
3434
// Create a new logger that writes all errors in red:
3535
func Example_redErrors() {
3636
w := os.Stderr
37-
logger := slog.New(tint.NewHandler(w, &tint.Options{
37+
logger := slog.New(tint.NewTextHandler(w, &tint.Options{
3838
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
3939
if a.Value.Kind() == slog.KindAny {
4040
if _, ok := a.Value.Any().(error); ok {
@@ -53,7 +53,7 @@ func Example_traceLevel() {
5353
const LevelTrace = slog.LevelDebug - 4
5454

5555
w := os.Stderr
56-
logger := slog.New(tint.NewHandler(w, &tint.Options{
56+
logger := slog.New(tint.NewTextHandler(w, &tint.Options{
5757
Level: LevelTrace,
5858
ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
5959
if a.Key == slog.LevelKey && len(groups) == 0 {
@@ -671,7 +671,7 @@ func TestHandler(t *testing.T) {
671671
if test.Opts == nil {
672672
test.Opts = &tint.Options{NoColor: true}
673673
}
674-
l := slog.New(tint.NewHandler(&buf, test.Opts))
674+
l := slog.New(tint.NewTextHandler(&buf, test.Opts))
675675
test.F(l)
676676

677677
got, foundNewline := strings.CutSuffix(buf.String(), "\n")
@@ -760,7 +760,7 @@ func TestHandler_Consistency(t *testing.T) {
760760
t.Run(strconv.Itoa(i), func(t *testing.T) {
761761
// log with tint.Handler
762762
var tintBuf bytes.Buffer
763-
tintLogger := slog.New(tint.NewHandler(&tintBuf, &tint.Options{
763+
tintLogger := slog.New(tint.NewTextHandler(&tintBuf, &tint.Options{
764764
NoColor: true,
765765
ReplaceAttr: rep,
766766
}))
@@ -813,7 +813,7 @@ func TestReplaceAttr(t *testing.T) {
813813
slogLogger.Log(context.TODO(), slog.LevelInfo, "", test...)
814814

815815
tintRecord := make([]replaceAttrParams, 0)
816-
tintLogger := slog.New(tint.NewHandler(io.Discard, &tint.Options{
816+
tintLogger := slog.New(tint.NewTextHandler(io.Discard, &tint.Options{
817817
ReplaceAttr: replaceAttrRecorder(&tintRecord),
818818
}))
819819
tintLogger.Log(context.TODO(), slog.LevelInfo, "", test...)
@@ -865,7 +865,7 @@ func TestClonedHandlersSynchronizeWriter(t *testing.T) {
865865
logger.Info("test")
866866
}
867867

868-
logger := slog.New(tint.NewHandler(&bytes.Buffer{}, &tint.Options{}))
868+
logger := slog.New(tint.NewTextHandler(&bytes.Buffer{}, &tint.Options{}))
869869

870870
// start and wait for two goroutines
871871
var wg sync.WaitGroup
@@ -885,7 +885,7 @@ func BenchmarkLogAttrs(b *testing.B) {
885885
Name string
886886
H slog.Handler
887887
}{
888-
{"tint", tint.NewHandler(io.Discard, nil)},
888+
{"tint", tint.NewTextHandler(io.Discard, nil)},
889889
{"text", slog.NewTextHandler(io.Discard, nil)},
890890
{"json", slog.NewJSONHandler(io.Discard, nil)},
891891
{"discard", new(discarder)},

0 commit comments

Comments
 (0)