Skip to content

rosty-git/tint

Β 
Β 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

43 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

This is a fork of github.com/lmittmann/tint

tint: 🌈 slog.Handler that writes tinted logs



Package tint implements a zero-dependency slog.Handler that writes tinted (colorized) logs. Its output format is inspired by the zerolog.ConsoleWriter and slog.TextHandler.

which is a drop-in replacement for slog.HandlerOptions.

go get github.com/lmittmann/tint

// go.mod
replace github.com/lmittmann/tint v1.0.4 => github.com/rosty-git/tint v0.0.1

Usage

w := os.Stderr

// create a new logger
logger := slog.New(tint.NewHandler(w, nil))

// set global logger with custom options
slog.SetDefault(slog.New(
    tint.NewHandler(w, &tint.Options{
        Level:      slog.LevelDebug,
        TimeFormat: time.Kitchen,
    }),
))

Customize Attributes

ReplaceAttr can be used to alter or drop attributes. If set, it is called on each non-group attribute before it is logged. See slog.HandlerOptions for details.

// create a new logger that doesn't write the time
w := os.Stderr
logger := slog.New(
    tint.NewHandler(w, &tint.Options{
        ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr {
            if a.Key == slog.TimeKey && len(groups) == 0 {
                return slog.Attr{}
            }
            return a
        },
    }),
)

Automatically Enable Colors

Colors are enabled by default and can be disabled using the Options.NoColor attribute. To automatically enable colors based on the terminal capabilities, use e.g. the go-isatty package.

w := os.Stderr
logger := slog.New(
    tint.NewHandler(w, &tint.Options{
        NoColor: !isatty.IsTerminal(w.Fd()),
    }),
)

Windows Support

Color support on Windows can be added by using e.g. the go-colorable package.

w := os.Stderr
logger := slog.New(
    tint.NewHandler(colorable.NewColorable(w), nil),
)

Customize Styles

CustomStyles can be used to set your own styles for time, level and source.

w := os.Stderr

slog.SetDefault(slog.New(
    tint.NewHandler(w, &tint.Options{
        Level:     slog.LevelDebug,
        NoColor:   !isatty.IsTerminal(w.Fd()),
        AddSource: true,
        CustomStyles: &tint.CustomStyles{
            Time: []string{tint.AnsiBlack},
            Level: &tint.LevelCustomStyles{
                Info:    []string{tint.AnsiBackgroundGreen, tint.AnsiBrightWhite},
                Error:   []string{tint.AnsiBackgroundBrightRed, tint.AnsiBrightWhite},
                Debug:   []string{tint.AnsiItalic},
                Warning: []string{tint.AnsiBrightYellow, tint.AnsiBold, tint.AnsiUnderline},
			},
            Source: []string{tint.AnsiItalic},
            Message: &tint.MessageCustomStyles{
                Main:  []string{tint.AnsiBlack, tint.AnsiBold},
                Key:   []string{tint.AnsiBackgroundBrightGreen, tint.AnsiBrightWhite},
                Value: []string{tint.AnsiBackgroundBrightYellow, tint.AnsiBrightWhite},
            },
        },
    }),
))

About

🌈 slog.Handler that writes tinted (colorized) logs

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%