Documentation ¶
Overview ¶
Package tint implements a zero-dependency slog.Handler that writes tinted (colorized) logs. The output format is inspired by the zerolog.ConsoleWriter and slog.TextHandler.
The output format can be customized using Options, which is a drop-in replacement for slog.HandlerOptions.
Customize Attributes ¶
Options.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.
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), )
Example ¶
package main import ( "errors" "log/slog" "os" "time" "github.com/rosty-git/tint" ) func main() { slog.SetDefault(slog.New(tint.NewHandler(os.Stderr, &tint.Options{ Level: slog.LevelDebug, TimeFormat: time.Kitchen, }))) slog.Info("Starting server", "addr", ":8080", "env", "production") slog.Debug("Connected to DB", "db", "myapp", "host", "localhost:5432") slog.Warn("Slow request", "method", "GET", "path", "/users", "duration", 497*time.Millisecond) slog.Error("DB connection lost", tint.Err(errors.New("connection reset")), "db", "myapp") }
Output:
Index ¶
Examples ¶
Constants ¶
const ( AnsiReset = "\033[0m" AnsiFaint = "\033[2m" AnsiResetFaint = "\033[22m" AnsiUnderline = "\033[4m" AnsiReverse = "\033[7m" AnsiBlink = "\033[5m" AnsiRapidBlink = "\033[6m" // Rapid blink on AnsiBold = "\033[1m" // Bold on AnsiItalic = "\033[3m" // Italic on AnsiConceal = "\033[8m" // Conceal on AnsiCrossed = "\033[9m" // Crossed-out on AnsiBrightRed = "\033[91m" AnsiBrightGreen = "\033[92m" AnsiBrightYellow = "\033[93m" AnsiBrightWhite = "\033[97m" AnsiBrightRedFaint = "\033[91;2m" AnsiBlack = "\033[30m" // Set text color to black AnsiRed = "\033[31m" // Set text color to red AnsiGreen = "\033[32m" // Set text color to green AnsiYellow = "\033[33m" // Set text color to yellow AnsiBlue = "\033[34m" // Set text color to blue AnsiMagenta = "\033[35m" // Set text color to magenta AnsiCyan = "\033[36m" // Set text color to cyan AnsiWhite = "\033[37m" // Set text color to white AnsiColorReset = "\033[39m" // Reset text color to default AnsiBackgroundBlack = "\033[40m" // Set background color to black AnsiBackgroundBrightBlack = "\033[100m" // Set background color to black AnsiBackgroundRed = "\033[41m" // Set background color to red AnsiBackgroundBrightRed = "\033[101m" // Set background color to red AnsiBackgroundGreen = "\033[42m" // Set background color to green AnsiBackgroundBrightGreen = "\033[102m" // Set background color to green AnsiBackgroundYellow = "\033[43m" // Set background color to yellow AnsiBackgroundBrightYellow = "\033[103m" // Set background color to yellow AnsiBackgroundBlue = "\033[44m" // Set background color to blue AnsiBackgroundMagenta = "\033[45m" // Set background color to magenta AnsiBackgroundCyan = "\033[46m" // Set background color to cyan AnsiBackgroundWhite = "\033[47m" // Set background color to white AnsiBackgroundReset = "\033[49m" // Reset background color to default )
ANSI modes
Variables ¶
This section is empty.
Functions ¶
func Err ¶
Err returns a tinted (colorized) slog.Attr that will be written in red color by the tint.Handler. When used with any other slog.Handler, it behaves as
slog.Any("err", err)
func NewHandler ¶
NewHandler creates a slog.Handler that writes tinted logs to Writer w, using the default options. If opts is nil, the default options are used.
Types ¶
type CustomStyles ¶
type CustomStyles struct { Time []string Level *LevelCustomStyles Source []string Message *MessageCustomStyles }
type LevelCustomStyles ¶
type MessageCustomStyles ¶
type Options ¶
type Options struct { // Enable source code location (Default: false) AddSource bool // Minimum level to log (Default: slog.LevelInfo) Level slog.Leveler // ReplaceAttr is called to rewrite each non-group attribute before it is logged. // See https://pkg.go.dev/log/slog#HandlerOptions for details. ReplaceAttr func(groups []string, attr slog.Attr) slog.Attr // Time format (Default: time.StampMilli) TimeFormat string // Disable color (Default: false) NoColor bool CustomStyles *CustomStyles }
Options for a slog.Handler that writes tinted logs. A zero Options consists entirely of default values.
Options can be used as a drop-in replacement for slog.HandlerOptions.