Documentation
¶
Overview ¶
slogcolor implements a color handler for log/slog.
Example ¶
package main
import (
"errors"
"log/slog"
"os"
"time"
"github.com/fatih/color"
"github.com/otheroffram/slogcolor"
)
func main() {
opts := slogcolor.DefaultOptions
opts.Level = slog.LevelDebug
opts.LevelTags = map[slog.Level]string{
slog.LevelInfo: color.New(color.BgCyan, color.FgBlack).Sprint("Info"),
}
slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts)))
slog.Info("Initializing")
slog.Debug("Init done", "duration", 500*time.Millisecond)
slog.Warn("Slow request!", "method", "GET", "path", "/api/users", "duration", 750*time.Millisecond)
slog.Error("DB connection lost!", "err", errors.New("connection reset"), "db", "horalky")
}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultLevelTags = map[slog.Level]string{ slog.LevelDebug: color.New(color.BgCyan, color.FgHiWhite).Sprint("DEBUG"), slog.LevelInfo: color.New(color.BgGreen, color.FgHiWhite).Sprint("INFO "), slog.LevelWarn: color.New(color.BgYellow, color.FgHiWhite).Sprint("WARN "), slog.LevelError: color.New(color.BgRed, color.FgHiWhite).Sprint("ERROR"), }
DefaultLevelTags are the default level tags.
Functions ¶
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
Handler is a colored slog handler.
func NewHandler ¶
NewHandler creates a new Handler with the specified options. If opts is nil, uses DefaultOptions.
type Options ¶
type Options struct {
// Level reports the minimum level to log.
// Levels with lower levels are discarded.
// If nil, the Handler uses [slog.LevelInfo].
Level slog.Leveler
// TimeFormat is the time format.
TimeFormat string
// SrcFileMode is the source file mode.
SrcFileMode SourceFileMode
// SrcFileLength to show fixed length filename to line up the log output, default 0 shows complete filename.
SrcFileLength int
// MsgPrefix to show prefix before message, default: white colored "| ".
MsgPrefix string
// MsgColor is the color of the message, default to empty.
MsgColor *color.Color
// MsgLength to show fixed length message to line up the log output, default 0 shows complete message.
MsgLength int
// NoColor disables color, default: false.
NoColor bool
// LevelTags is level tag for message, default: DefaultLevelStyles
LevelTags map[slog.Level]string
}
Options represents the options passed into NewHandler.
type SourceFileMode ¶
type SourceFileMode int
const ( // Nop does nothing. Nop SourceFileMode = iota // ShortFile produces only the filename (for example main.go:69). ShortFile // LongFile produces the full file path (for example /home/user/go/src/myapp/main.go:69). LongFile )
Source Files
¶
Click to show internal directories.
Click to hide internal directories.
