Documentation ¶
Index ¶
- type Handler
- func (h *Handler) Enabled(_ context.Context, level slog.Level) bool
- func (h *Handler) Handle(_ context.Context, record slog.Record) error
- func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h *Handler) WithGroup(name string) slog.Handler
- func (h *Handler) WithOutput(output io.Writer) slog.Handler
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Handler ¶
type Handler struct { // Output is the writer to write to. Defaults to os.Stderr. Output io.Writer // Level is the minimum level to log. Defaults to slog.LevelInfo. Level slog.Leveler // ExcludeTime, if true, will exclude the time from the output. ExcludeTime bool // ExcludeLevel, if true, will exclude the level from the output. ExcludeLevel bool // AddSource, if true, will add the source file and line number to the output. AddSource bool // contains filtered or unexported fields }
Handler is a slog.Handler that writes human-readable log entries. Because it is for human consumption, changes to the format are not considered breaking changes. Entries may be multi-line. The current format looks like this:
<message> <attributes as yaml>
No escaping is done on the message. Attributes are in YAML format with the top level indented to make it visually distinct from the message.
Example ¶
package main import ( "fmt" "golang.org/x/exp/slog" "os" "github.com/willabides/actionslog/human" ) func main() { logger := slog.New(&human.Handler{ Output: os.Stdout, ExcludeTime: true, }) logger = logger.With(slog.String("func", "Example")) logger.Info("hello", slog.String("object", "world")) logger.Warn("This is a stern warning") logger.Error("got an error", slog.Any("err", fmt.Errorf("omg"))) logger.Debug("this is a debug message") logger.Info("this is a\nmultiline\nmessage") logger.Info("multiline value", slog.String("value", "this is a\nmultiline\nvalue")) }
Output: hello level: INFO func: Example object: world This is a stern warning level: WARN func: Example got an error level: ERROR func: Example err: omg this is a multiline message level: INFO func: Example multiline value level: INFO func: Example value: |- this is a multiline value
func (*Handler) WithOutput ¶ added in v0.5.0
WithOutput returns a new Handler that writes to output. This is primarily meant for use with github.com/willabides/actionslog.Wrapper
Click to show internal directories.
Click to hide internal directories.