Documentation ¶
Overview ¶
Example ¶
t := time.Date(2023, 05, 04, 20, 30, 40, 0, time.UTC) logger := slog.New(NewHandler(os.Stdout, nil, nil)) logger.Info("A `level` message `at` `about` and `.about.bar`", "level", slog.LevelInfo, "at", t, "level", 7, "about", slog.GroupValue( slog.Bool("foo", true), slog.Int("bar", 4711), slog.Float64("baz", 3.14159), ), )
Output: A `level:INFO` message `at:2023-05-04T20:30:40Z` `about:[foo=true bar=4711 baz=3.14159]` and `.about.bar:4711` (level=7)
Example (CompareOutputs) ¶
const form = "added `count` x `item` to shopping cart by `user`" args := []any{"count", 7, "item", "Hat", "user", "John Doe"} opts := slog.HandlerOptions{Level: &slog.LevelVar{}, AddSource: true} logger := slog.New(slog.NewTextHandler(os.Stdout, &opts)) fmt.Println("Std Text:") logger.Info(form, args...) logger.WithGroup("gruppe").Info(form, args...) logger = slog.New(slog.NewJSONHandler(os.Stdout, &opts)) fmt.Println("Std JSON:") logger.Info(form, args...) logger.WithGroup("gruppe").Info(form, args...) logger = slog.New(NewHandler( os.Stdout, NewHeader(sllm.Tdefault|sllm.Tyear).Append, &opts, )) fmt.Println("sllm:") logger.Info(form, args...) logger.WithGroup("gruppe").Info(form, args...) // DISABLED Output: // time=2023-05-06T13:24:00.476+02:00 level=INFO msg="added `count` x `item` to shopping cart by `user`" count=7 item=Hat user="John Doe" // {"time":"2023-05-06T13:24:00.476762195+02:00","level":"INFO","msg":"added `count` x `item` to shopping cart by `user`","count":7,"item":"Hat","user":"John Doe"} // 2023-05-06 Sa 13:24:00 INFO added `count:7` x `item:Hat` to shopping cart by `user:John Doe`
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var DefaultOptions = slog.HandlerOptions{ Level: &slog.LevelVar{}, }
Functions ¶
Types ¶
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶
func NewHandler(w io.Writer, header HeaderFunc, opts *slog.HandlerOptions) *Handler
type HeaderFunc ¶
func DefaultHeader ¶
func DefaultHeader() HeaderFunc
Click to show internal directories.
Click to hide internal directories.