human

package
v0.4.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2023 License: MIT Imports: 11 Imported by: 4

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithOutput

func WithOutput(w io.Writer) slog.Handler

WithOutput returns a new slog.Handler with the given output writer and permissive level. This is primarily for use with actionslog.Wrapper.

Types

type Handler

type Handler struct {
	// 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>

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.New(&human.Options{
		Output: os.Stdout,
	}))
	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
  func: Example
  object: world
This is a stern warning
  func: Example
got an error
  func: Example
  err: omg
this is a
multiline
message
  func: Example
multiline value
  func: Example
  value: |-
    this is a
    multiline
    value

func New

func New(opts *Options) *Handler

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, record slog.Record) error

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

type Options

type Options 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
}

Options are options for New.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL