Standard library / log/slog
slog.SetDefault()
Replace the process default logger and bridge standard log output to its handler. Expected stdout: '{"level":"INFO","msg":"ready"}\n'.
Formal Syntax & Structure
go syntaxgo
func SetDefault(l *Logger)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| l | *Logger | Input to the operation as described above. |
Working Example
Example Snippetgo
package main
import ( "bytes" "fmt" "log/slog")
func logger(b *bytes.Buffer) *slog.Logger { return slog.New(slog.NewJSONHandler(b, &slog.HandlerOptions{Level: slog.LevelDebug, ReplaceAttr: func(groups []string, a slog.Attr) slog.Attr { if len(groups) == 0 && a.Key == slog.TimeKey { return slog.Attr{} } return a }}))}
func main() { var b bytes.Buffer l := logger(&b) slog.SetDefault(l) slog.Info("ready") fmt.Print(b.String())}