### Install lgr Go Package Source: https://github.com/go-pkgz/lgr/blob/master/README.md Instructions to install the `lgr` logging library using the standard Go package manager. ```Go go get github.com/go-pkgz/lgr ``` -------------------------------- ### Simple lgr Initialization with Debug and Millisecond Options Source: https://github.com/go-pkgz/lgr/blob/master/README.md A concise example demonstrating how to initialize an `lgr` logger with `lgr.Debug` and `lgr.Msec` options enabled. ```Go l := lgr.New(lgr.Debug, lgr.Msec) ``` -------------------------------- ### Example Output of lgr Logger Source: https://github.com/go-pkgz/lgr/blob/master/README.md Illustrates the formatted output generated by the `lgr` logger, including timestamp, log level, caller information, and the log message, when configured with caller details and milliseconds. ```Text 2018/01/07 13:02:34.000 INFO {svc/handler.go:101 h.MyFunc1} some important message, can't open file myfile.xyz 2018/01/07 13:02:34.015 DEBUG {svc/handler.go:155 h.MyFunc2} some less important message, file is too small ``` -------------------------------- ### Initialize and Use lgr Logger in Go Source: https://github.com/go-pkgz/lgr/blob/master/README.md Demonstrates how to initialize a new `lgr` logger with debug mode, millisecond timestamps, and caller information, then log messages at INFO and DEBUG levels. ```Go l := lgr.New(lgr.Msec, lgr.Debug, lgr.CallerFile, lgr.CallerFunc) l.Logf("INFO some important message, %v", err) l.Logf("DEBUG some less important message, %v", err) ``` -------------------------------- ### Predefined lgr Formatting Templates Source: https://github.com/go-pkgz/lgr/blob/master/README.md Lists the predefined formatting templates available in `lgr` that can be passed directly to `lgr.Format` to customize log output, including options for milliseconds, package, file, and function details. ```Go Short = `{{.DT.Format "2006/01/02 15:04:05"}} {{.Level}} {{.Message}}` WithMsec = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} {{.Message}}` WithPkg = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerPkg}}) {{.Message}}` ShortDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFile}}:{{.CallerLine}}) {{.Message}}` FuncDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFunc}}) {{.Message}}` FullDebug = `{{.DT.Format "2006/01/02 15:04:05.000"}} {{.Level}} ({{.CallerFile}}:{{.CallerLine}} {{.CallerFunc}}) {{.Message}}` ``` -------------------------------- ### Integrate lgr logger with Go's slog package Source: https://github.com/go-pkgz/lgr/blob/master/README.md Shows how to create an `lgr` logger, convert it to a `slog.Handler` using `lgr.ToSlogHandler`, and then create a `slog.Logger` to use `lgr`'s formatting with the standard `slog` API. ```go // Create lgr logger lgrLogger := lgr.New(lgr.Debug, lgr.Msec) // Convert to slog handler and create slog logger handler := lgr.ToSlogHandler(lgrLogger) logger := slog.New(handler) // Use standard slog API with lgr formatting logger.Info("message", "key1", "value1") // Output: 2023/09/15 10:34:56.789 INFO message key1="value1" ``` -------------------------------- ### Configure lgr output with a custom mapper for colorization Source: https://github.com/go-pkgz/lgr/blob/master/README.md Demonstrates how to use `lgr.Mapper` to apply custom functions for coloring different elements of log output (error, warn, info, debug, caller, time) using the `fatih/color` library. The mapper is then applied via `lgr.Map` option. ```go colorizer := lgr.Mapper{ ErrorFunc: func(s string) string { return color.New(color.FgHiRed).Sprint(s) }, WarnFunc: func(s string) string { return color.New(color.FgHiYellow).Sprint(s) }, InfoFunc: func(s string) string { return color.New(color.FgHiWhite).Sprint(s) }, DebugFunc: func(s string) string { return color.New(color.FgWhite).Sprint(s) }, CallerFunc: func(s string) string { return color.New(color.FgBlue).Sprint(s) }, TimeFunc: func(s string) string { return color.New(color.FgCyan).Sprint(s) } } logOpts := []lgr.Option{lgr.Msec, lgr.LevelBraces, lgr.Map(colorizer)} ``` -------------------------------- ### Directly use slog handler within lgr logger Source: https://github.com/go-pkgz/lgr/blob/master/README.md Demonstrates how to initialize an `lgr` logger directly with a `slog.Handler` using `lgr.SlogHandler` option, enabling `lgr`'s API to output through the `slog` backend. ```go // Create a logger that uses slog directly jsonHandler := slog.NewJSONHandler(os.Stdout, nil) logger := lgr.New(lgr.SlogHandler(jsonHandler)) // Use lgr API with slog backend logger.Logf("INFO message") // Output: {"time":"2023-09-15T10:34:56.789Z","level":"INFO","msg":"message"} ``` -------------------------------- ### Apply Custom lgr Formatting Template in Go Source: https://github.com/go-pkgz/lgr/blob/master/README.md Shows how to define and apply a custom log output template using `lgr.Format` for flexible log message structuring, including level, timestamp, and caller package. ```Go lgr.Format(`{{.Level}} - {{.DT.Format "2006-01-02T15:04:05Z07:00"}} - {{.CallerPkg}} - {{.Message}}`) ``` -------------------------------- ### lgr Adaptor Functions Source: https://github.com/go-pkgz/lgr/blob/master/README.md Functions to convert an `lgr` logger to standard Go logging interfaces like `io.Writer`, `*log.Logger`, or `slog.Handler`, and functions to set up global loggers. ```APIDOC lgr.ToWriter(l lgr.L, level string) io.Writer l: The lgr.L instance to convert. level: Optional. If defined (non-empty), will enforce the log level for write operations. Returns: An io.Writer that forwards write operations to the underlying lgr.L. lgr.ToStdLogger(l lgr.L, level string) *log.Logger l: The lgr.L instance to convert. level: Optional. If defined (non-empty), will enforce the log level for standard logger operations. Returns: A standard *log.Logger built on top of lgr.L. lgr.ToSlogHandler(l lgr.L) slog.Handler l: The lgr.L instance to convert. Returns: A slog.Handler for use with the log/slog package. lgr.SetupStdLogger(opts ...Option) opts: Configuration options for the lgr logger. Description: Initializes the standard global logger (log.std) with an lgr logger and given options. All standard methods like log.Print, log.Println, log.Fatal will be forwarded to lgr. lgr.SetupWithSlog(logger *slog.Logger) logger: The slog.Logger instance to use. Description: Sets up the global lgr logger to use a provided slog.Logger as its backend. ``` -------------------------------- ### lgr Supported Log Levels and Behavior Source: https://github.com/go-pkgz/lgr/blob/master/README.md Explains the various log levels supported by `lgr.Logf` (TRACE, DEBUG, INFO, WARN, ERROR, PANIC, FATAL) and their specific behaviors, including filtering rules, output streams, and program termination actions. ```APIDOC Supported Levels: TRACE, DEBUG, INFO, WARN, ERROR, PANIC, FATAL - TRACE: filtered unless lgr.Trace option defined - DEBUG: filtered unless lgr.Debug or lgr.Trace options defined - INFO, WARN: no special behavior - ERROR: sends messages to both out and err writers - FATAL: sends messages to both out and err writers and exit(1) - PANIC: same as FATAL, plus sends dump of callers and runtime info to err ``` -------------------------------- ### Wrap slog handler with lgr interface for unified API Source: https://github.com/go-pkgz/lgr/blob/master/README.md Illustrates how to create a `slog.NewJSONHandler` and then wrap it with the `lgr` interface using `lgr.FromSlogHandler`, allowing the use of `lgr`'s API while leveraging `slog` as the backend. ```go // Create slog handler jsonHandler := slog.NewJSONHandler(os.Stdout, nil) // Wrap it with lgr interface logger := lgr.FromSlogHandler(jsonHandler) // Use lgr API with slog backend logger.Logf("INFO message with %s", "structured data") // Output: {"time":"2023-09-15T10:34:56.789Z","level":"INFO","msg":"message with structured data"} ``` -------------------------------- ### lgr.Func Wrapper Source: https://github.com/go-pkgz/lgr/blob/master/README.md Describes `lgr.Func`, a functional wrapper that allows converting a standard function directly into an `lgr.L` interface implementation. ```APIDOC lgr.Func: function wrapper to create lgr.L from a function ``` -------------------------------- ### Predefined lgr Loggers Source: https://github.com/go-pkgz/lgr/blob/master/README.md Lists the two predefined loggers available in `lgr`: `lgr.NoOp` (a do-nothing logger) and `lgr.Std` (a logger that passes messages directly to the standard library log). ```APIDOC lgr.NoOp: do-nothing logger lgr.Std: passes directly to stdlib log ``` -------------------------------- ### Enable caller information in slog JSON output with lgr Source: https://github.com/go-pkgz/lgr/blob/master/README.md Explains how to configure a `slog.NewJSONHandler` with `AddSource: true` to include caller information (file, line, function) in the JSON log output when used with `lgr`. It also clarifies the distinction between `lgr`'s native caller options and `slog`'s `AddSource`. ```go // Create JSON handler with source information (caller info) jsonHandler := slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{ AddSource: true // This enables caller information in JSON output }) // Use handler with lgr logger := lgr.New(lgr.SlogHandler(jsonHandler)) logger.Logf("INFO message with caller info") // Output will include source file, line and function in JSON ``` -------------------------------- ### lgr.New Functional Options Source: https://github.com/go-pkgz/lgr/blob/master/README.md Details the various functional options available for configuring a new `lgr` logger instance via `lgr.New`, covering aspects like debug modes, output writers, caller information, custom formatting, secret masking, and slog integration. ```APIDOC lgr.New Options: - lgr.Debug: turn debug mode on (allows "DEBUG" level messages) - lgr.Trace: turn trace mode on (allows "TRACE" and "DEBUG" levels) - lgr.Out(io.Writer): sets the output writer (default os.Stdout) - lgr.Err(io.Writer): sets the error writer (default os.Stderr) - lgr.CallerFile: adds caller file info (native text format only) - lgr.CallerFunc: adds caller function info (native text format only) - lgr.CallerPkg: adds caller package info (native text format only) - lgr.LevelBraces: wraps levels with "[" and "]" - lgr.Msec: adds milliseconds to timestamp - lgr.Format(template string): sets a custom template, overwrites other formatting modifiers - lgr.Secret(secret ...interface{}): sets list of secrets to hide from logging outputs - lgr.Map(mapper func(level string, msg string) (string, string)): sets mapper functions to change elements of the logging output based on levels - lgr.StackTraceOnError: turns on stack trace for ERROR level - lgr.SlogHandler(h slog.Handler): delegates logging to the provided slog handler ``` -------------------------------- ### lgr.L Interface Definition Source: https://github.com/go-pkgz/lgr/blob/master/README.md Defines the core `lgr.L` interface with its single `Logf` method for logging formatted messages, which is the primary interface for `lgr`. ```APIDOC interface lgr.L: Logf(format string, args ...interface{}) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.