Documentation ¶
Overview ¶
slogcolor implements a color handler for log/slog.
Example ¶
package main import ( "errors" "log/slog" "os" "time" "github.com/MatusOllah/slogcolor" ) func main() { opts := slogcolor.DefaultOptions opts.Level = slog.LevelDebug 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") }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
NewHandler creates a new Handler.
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 }
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/frajer/go/src/myapp/main.go:69). LongFile )
Click to show internal directories.
Click to hide internal directories.