### Install slogcolor Source: https://github.com/sladkycitron/slogcolor/blob/main/README.md Use the go get command to add the package to your project. ```sh go get -u github.com/SladkyCitron/slogcolor ``` -------------------------------- ### Use DefaultOptions Source: https://context7.com/sladkycitron/slogcolor/llms.txt Apply the predefined configuration for standard logging behavior. ```go package main import ( "log/slog" "os" "github.com/SladkyCitron/slogcolor" ) func main() { // DefaultOptions provides: // - Level: slog.LevelInfo // - TimeFormat: time.DateTime ("2006-01-02 15:04:05") // - SrcFileMode: ShortFile (shows filename:line) // - MsgPrefix: white colored "| " // - NoColor: false (colors enabled) // - NoTime: false (timestamps enabled) // - LevelTags: colored DEBUG, INFO, WARN, ERROR badges slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions))) slog.Info("Using default options") slog.Warn("Warning with defaults", "key", "value") } ``` -------------------------------- ### Configure default options Source: https://github.com/sladkycitron/slogcolor/blob/main/README.md Use DefaultOptions or nil to initialize the handler with standard settings. ```go slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions))) // or slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, nil))) ``` -------------------------------- ### Initialize slogcolor Handler Source: https://context7.com/sladkycitron/slogcolor/llms.txt Create a new handler using either default options or custom configurations. ```go package main import ( "errors" "log/slog" "os" "time" "github.com/SladkyCitron/slogcolor" ) func main() { // Create handler with default options handler := slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions) slog.SetDefault(slog.New(handler)) // Or pass nil to use defaults slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, nil))) // Log messages at different levels 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") } ``` -------------------------------- ### Customize output format Source: https://github.com/sladkycitron/slogcolor/blob/main/README.md Define custom Options to control log levels, time formats, and source file display. ```go opts := &slogcolor.Options{ Level: slog.LevelDebug, TimeFormat: time.RFC3339, SrcFileMode: slog.Nop, } slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts))) ``` ```go opts := slogcolor.DefaultOptions opts.Level = slog.LevelDebug opts.TimeFormat = time.RFC3339 opts.SrcFileMode = slog.Nop slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts))) ``` -------------------------------- ### Initialize slogcolor handler Source: https://github.com/sladkycitron/slogcolor/blob/main/README.md Configure the default slog handler to use slogcolor for colored output. ```go package main import ( "os" "time" "errors" "log/slog" "github.com/SladkyCitron/slogcolor" ) func main() { // Configure slog to use slogcolor by default for colored output slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions))) 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") } ``` -------------------------------- ### Configure Handler Options Source: https://context7.com/sladkycitron/slogcolor/llms.txt Customize handler behavior by defining an Options struct or modifying DefaultOptions. ```go package main import ( "log/slog" "os" "time" "github.com/SladkyCitron/slogcolor" "github.com/fatih/color" ) func main() { // Method 1: Create options from scratch opts := &slogcolor.Options{ Level: slog.LevelDebug, // Minimum log level TimeFormat: time.RFC3339, // Custom time format SrcFileMode: slogcolor.ShortFile, // Show filename only SrcFileLength: 20, // Fixed width for alignment MsgPrefix: color.HiWhiteString("| "), // Prefix before message MsgLength: 40, // Fixed message width MsgColor: color.New(color.FgWhite), // Message color NoColor: false, // Enable colors NoTime: false, // Show timestamps } slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts))) // Method 2: Modify default options opts2 := slogcolor.DefaultOptions opts2.Level = slog.LevelDebug opts2.TimeFormat = time.RFC3339 opts2.SrcFileMode = slogcolor.Nop // Disable source file display slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts2))) slog.Info("Application started", "version", "1.0.0") slog.Debug("Configuration loaded", "config", "/etc/app/config.yaml") } ``` -------------------------------- ### Add log prefixes Source: https://github.com/sladkycitron/slogcolor/blob/main/README.md Use the Prefix function to add context to log messages, optionally aliasing it for brevity. ```go slog.Info(slogcolor.Prefix("MyPrefix", "kajšmentke")) slog.Info(slogcolor.Prefix("SceneController", "switching scene"), "scene", "MainMenuScene") // or slog.Info(slogcolor.Prefix("MyPrefix")+"kajšmentke") slog.Info(slogcolor.Prefix("SceneController")+"switching scene", "scene", "MainMenuScene") ``` ```go var P = slogcolor.Prefix slog.Info(P("MyPrefix", "kajšmentke")) // or slog.Info(P("MyPrefix")+"kajšmentke") ``` -------------------------------- ### Configure Source File Display Mode Source: https://context7.com/sladkycitron/slogcolor/llms.txt Control how source file information appears in logs using Nop, ShortFile, or LongFile modes. Set the desired mode on the slogcolor.Options struct. ```go package main import ( "log/slog" "os" "github.com/SladkyCitron/slogcolor" ) func main() { // Nop - no source file shown opts := slogcolor.DefaultOptions opts.SrcFileMode = slogcolor.Nop slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts))) slog.Info("No source file") // Output: 2024-01-15 10:30:45 INFO | No source file // ShortFile - filename:line only opts.SrcFileMode = slogcolor.ShortFile slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts))) slog.Info("Short file") // Output: 2024-01-15 10:30:45 INFO main.go:20 | Short file // LongFile - full path:line opts.SrcFileMode = slogcolor.LongFile slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts))) slog.Info("Long file") // Output: 2024-01-15 10:30:45 INFO /home/user/app/main.go:25 | Long file } ``` -------------------------------- ### Add Colored Prefixes to Log Messages Source: https://context7.com/sladkycitron/slogcolor/llms.txt Prepend colored prefix tags to log messages to identify subsystems. This can be done by passing a prefixed string to slog functions or by concatenating. ```go package main import ( "log/slog" "os" "github.com/SladkyCitron/slogcolor" ) func main() { slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions))) // Method 1: Prefix with message as second argument slog.Info(slogcolor.Prefix("DB", "Connection established")) slog.Info(slogcolor.Prefix("Network", "Listening on port 8080")) slog.Info(slogcolor.Prefix("SceneController", "switching scene"), "scene", "MainMenuScene") // Method 2: Prefix with string concatenation slog.Info(slogcolor.Prefix("Auth") + "User authenticated") slog.Warn(slogcolor.Prefix("Cache") + "Cache miss", "key", "user:123") // Method 3: Create alias for frequent use var P = slogcolor.Prefix slog.Info(P("MyPrefix", "kajšmentke")) slog.Error(P("API") + "Request failed", "status", 500) } // Output (with colors): // 2024-01-15 10:30:45 INFO main.go:12 | DB Connection established // 2024-01-15 10:30:45 INFO main.go:13 | Network Listening on port 8080 // 2024-01-15 10:30:45 INFO main.go:14 | SceneController switching scene scene=MainMenuScene ``` -------------------------------- ### Disable colors conditionally Source: https://github.com/sladkycitron/slogcolor/blob/main/README.md Set NoColor in Options to disable color output, useful when detecting non-terminal environments. ```go w := os.Stderr opts := slogcolor.DefaultOptions opts.NoColor = !isatty.IsTerminal(w.Fd()) slog.SetDefault(slog.New(slogcolor.NewHandler(w, opts))) ``` -------------------------------- ### Customize Log Level Tags Source: https://context7.com/sladkycitron/slogcolor/llms.txt Override default colored badges for log levels by providing a map of slog.Level to custom string representations in slogcolor.Options.LevelTags. ```go package main import ( "log/slog" "os" "github.com/SladkyCitron/slogcolor" "github.com/fatih/color" ) func main() { opts := slogcolor.DefaultOptions opts.Level = slog.LevelDebug // Override specific level tags (others use DefaultLevelTags) opts.LevelTags = map[slog.Level]string{ slog.LevelInfo: color.New(color.BgCyan, color.FgBlack).Sprint("Info"), slog.LevelDebug: color.New(color.BgMagenta, color.FgWhite).Sprint("DBG "), } slog.SetDefault(slog.New(slogcolor.NewHandler(os.Stderr, opts))) slog.Debug("Custom debug tag") slog.Info("Custom info tag") slog.Warn("Default warn tag") // Uses DefaultLevelTags slog.Error("Default error tag") // Uses DefaultLevelTags } ``` -------------------------------- ### Add Persistent Attributes with WithAttrs Source: https://context7.com/sladkycitron/slogcolor/llms.txt Use WithAttrs to attach key-value pairs that will be included in all subsequent log messages from the derived logger. This is useful for adding context like component names or host information. ```go package main import ( "log/slog" "os" "github.com/SladkyCitron/slogcolor" ) func main() { handler := slogcolor.NewHandler(os.Stderr, slogcolor.DefaultOptions) logger := slog.New(handler) // WithAttrs - add persistent attributes dbLogger := logger.WithAttrs([]slog.Attr{ slog.String("component", "database"), slog.String("host", "localhost:5432"), }) dbLogger.Info("Connected") dbLogger.Error("Query failed", "query", "SELECT * FROM users") // WithGroup - namespace attributes under a group requestLogger := logger.WithGroup("request") requestLogger.Info("Received", "method", "POST", "path", "/api/users") // Output: ... request.method=POST request.path=/api/users // Combine both apiLogger := logger.WithGroup("api").WithAttrs([]slog.Attr{ slog.String("version", "v2"), }) apiLogger.Info("Request processed", "duration", "45ms") } ``` -------------------------------- ### Disable Colors Based on Terminal Output Source: https://context7.com/sladkycitron/slogcolor/llms.txt Automatically disable ANSI color codes when logs are not being output to a terminal. This is useful for piping logs to files or other non-interactive destinations. ```go package main import ( "log/slog" "os" "github.com/SladkyCitron/slogcolor" "github.com/mattn/go-isatty" ) func main() { w := os.Stderr opts := slogcolor.DefaultOptions // Automatically disable colors when not running in a terminal opts.NoColor = !isatty.IsTerminal(w.Fd()) slog.SetDefault(slog.New(slogcolor.NewHandler(w, opts))) slog.Info("Adaptive color output", "terminal", isatty.IsTerminal(w.Fd())) slog.Warn("Colors enabled only in terminal") } // In terminal: colored output // Piped to file: plain text without ANSI codes ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.