shandler

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2023 License: Apache-2.0 Imports: 16 Imported by: 2

README

shandler

slog pretty handler

Documentation

Index

Constants

View Source
const (
	ResetSeq     = '0'
	BoldSeq      = "1"
	FaintSeq     = "2"
	ItalicSeq    = "3"
	UnderlineSeq = "4"
	BlinkSeq     = "5"
	ReverseSeq   = "7"
	CrossOutSeq  = "9"
	OverlineSeq  = "53"
	Foreground   = "38"
	Background   = "48"

	ESC = '\x1b'
)

Sequence definitions.

Variables

View Source
var CSI = []byte(string(ESC) + "[")

Functions

func CopyWithPrefix added in v0.0.2

func CopyWithPrefix(prefix string) *slog.Logger

func CopyWithThemes added in v0.0.2

func CopyWithThemes(themes Themes) *slog.Logger

func ThemeSchemaStrings

func ThemeSchemaStrings() []string

ThemeSchemaStrings returns a slice of all String values of the enum

Types

type Buffer

type Buffer []byte

func NewBuffer

func NewBuffer() *Buffer

func (*Buffer) Free

func (b *Buffer) Free()

func (*Buffer) Reset

func (b *Buffer) Reset()

func (*Buffer) String

func (b *Buffer) String() string

func (*Buffer) Write

func (b *Buffer) Write(p []byte) (int, error)

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(c byte)

func (*Buffer) WritePosInt

func (b *Buffer) WritePosInt(i int)

func (*Buffer) WritePosIntWidth

func (b *Buffer) WritePosIntWidth(i, width int)

WritePosIntWidth writes non-negative integer i to the buffer, padded on the left by zeroes to the given width. Use a width of 0 to omit padding.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string)

type Builder

type Builder interface {
	// contains filtered or unexported methods
}

type File

type File interface {
	io.ReadWriter
	Fd() uintptr
}

File represents a file descriptor.

type Handler

type Handler interface {
	WithPrefix(prefix string) slog.Handler
	WithThemes(themes Themes) slog.Handler
}

type JsonHandler

type JsonHandler struct {
	// contains filtered or unexported fields
}

func NewJsonHandler

func NewJsonHandler(opts ...Option) *JsonHandler

func (JsonHandler) Enabled

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

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower. It is called early, before any arguments are processed, to save effort if the log event should be discarded. If called from a Logger method, the first argument is the context passed to that method, or context.Background() if nil was passed or the method does not take a context. The context is passed so Enabled can use its values to make a decision.

func (JsonHandler) Handle

func (h JsonHandler) Handle(_ context.Context, r slog.Record) error

Handle handles the Record. It will only be called when Enabled returns true. The Context argument is as for Enabled. It is present solely to provide Handlers access to the context's values. Canceling the context should not affect record processing. (Among other things, log messages may be necessary to debug a cancellation-related problem.)

Handle methods that produce output should observe the following rules:

  • If r.Time is the zero time, ignore the time.
  • If r.PC is zero, ignore it.
  • Attr's values should be resolved.
  • If an Attr's key and value are both the zero value, ignore the Attr. This can be tested with attr.Equal(Attr{}).
  • If a group's key is empty, inline the group's Attrs.
  • If a group has no Attrs (even if it has a non-empty key), ignore it.

func (JsonHandler) WithAttrs

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

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. The Handler owns the slice: it may retain, modify or discard it.

func (JsonHandler) WithGroup

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

WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.

How this qualification happens is up to the Handler, so long as this Handler's attribute keys differ from those of another Handler with a different sequence of group names.

A Handler should treat WithGroup as starting a Group of Attrs that ends at the end of the log event. That is,

logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2))

should behave like

logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2)))

If the name is empty, WithGroup returns the receiver.

func (*JsonHandler) WithPrefix

func (j *JsonHandler) WithPrefix(prefix string) slog.Handler

func (JsonHandler) WriteColorful added in v0.0.2

func (h JsonHandler) WriteColorful(schema ThemeSchema, buf *Buffer, s string)

type Option

type Option func(*baseHandler)

func WithCaller

func WithCaller() Option

func WithFullCaller

func WithFullCaller() Option

func WithLevel

func WithLevel(level slog.Level) Option

func WithPrefix

func WithPrefix(prefix string) Option

func WithReplacer

func WithReplacer(fn Replacer) Option

WithReplacer please refer to Replacer

func WithTheme

func WithTheme(section ThemeSchema, theme *Theme) Option

func WithTimeFormat

func WithTimeFormat(format string) Option

func WithWriter

func WithWriter(w io.Writer) Option

type Replacer

type Replacer func(groups []string, a slog.Attr) slog.Attr

Replacer is called to rewrite each non-group attribute before it is logged. The attribute's value has been resolved (see slog.Value.Resolve). If Replacer returns an Attr with Key == "", the attribute is discarded.

The built-in attributes with keys "time", "level", "source", and "msg" are passed to this function, except that time is omitted if zero, and source is omitted if AddSource is false.

The first argument is a list of currently open groups that contain the Attr. It must not be retained or modified. Replacer is never called for Group attributes, only their contents. For example, the attribute list

Int("a", 1), Group("g", Int("b", 2)), Int("c", 3)

results in consecutive calls to Replacer with the following arguments:

nil, Int("a", 1)
[]string{"g"}, Int("b", 2)
nil, Int("c", 3)

Replacer can be used to change the default keys of the built-in attributes, convert types (for example, to replace a `time.Time` with the integer seconds since the Unix epoch), sanitize personal information, or remove attributes from the output.

type TextHandler

type TextHandler struct {
	// contains filtered or unexported fields
}

func NewTextHandler

func NewTextHandler(opts ...Option) *TextHandler

func (TextHandler) Enabled

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

Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower. It is called early, before any arguments are processed, to save effort if the log event should be discarded. If called from a Logger method, the first argument is the context passed to that method, or context.Background() if nil was passed or the method does not take a context. The context is passed so Enabled can use its values to make a decision.

func (TextHandler) Handle

func (h TextHandler) Handle(_ context.Context, r slog.Record) error

Handle handles the Record. It will only be called when Enabled returns true. The Context argument is as for Enabled. It is present solely to provide Handlers access to the context's values. Canceling the context should not affect record processing. (Among other things, log messages may be necessary to debug a cancellation-related problem.)

Handle methods that produce output should observe the following rules:

  • If r.Time is the zero time, ignore the time.
  • If r.PC is zero, ignore it.
  • Attr's values should be resolved.
  • If an Attr's key and value are both the zero value, ignore the Attr. This can be tested with attr.Equal(Attr{}).
  • If a group's key is empty, inline the group's Attrs.
  • If a group has no Attrs (even if it has a non-empty key), ignore it.

func (TextHandler) WithAttrs

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

WithAttrs returns a new Handler whose attributes consist of both the receiver's attributes and the arguments. The Handler owns the slice: it may retain, modify or discard it.

func (TextHandler) WithGroup

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

WithGroup returns a new Handler with the given group appended to the receiver's existing groups. The keys of all subsequent attributes, whether added by With or in a Record, should be qualified by the sequence of group names.

How this qualification happens is up to the Handler, so long as this Handler's attribute keys differ from those of another Handler with a different sequence of group names.

A Handler should treat WithGroup as starting a Group of Attrs that ends at the end of the log event. That is,

logger.WithGroup("s").LogAttrs(level, msg, slog.Int("a", 1), slog.Int("b", 2))

should behave like

logger.LogAttrs(level, msg, slog.Group("s", slog.Int("a", 1), slog.Int("b", 2)))

If the name is empty, WithGroup returns the receiver.

func (*TextHandler) WithPrefix

func (t *TextHandler) WithPrefix(prefix string) slog.Handler

func (*TextHandler) WithThemes

func (t *TextHandler) WithThemes(themes Themes) slog.Handler

func (TextHandler) WriteColorful added in v0.0.2

func (h TextHandler) WriteColorful(schema ThemeSchema, buf *Buffer, s string)

type Theme

type Theme struct {
	// contains filtered or unexported fields
}

func NewTheme

func NewTheme() *Theme

func (*Theme) Background

func (t *Theme) Background(light, dark colorful.Color) *Theme

Background sets a background color.

func (t *Theme) Blink(blink ...bool) *Theme

Blink enables blink mode.

func (*Theme) Bold

func (t *Theme) Bold(bold ...bool) *Theme

Bold enables bold rendering.

func (*Theme) CrossOut

func (t *Theme) CrossOut(crossOut ...bool) *Theme

CrossOut enables crossed-out rendering.

func (*Theme) Faint

func (t *Theme) Faint(faint ...bool) *Theme

Faint enables faint rendering.

func (*Theme) Foreground

func (t *Theme) Foreground(light, dark colorful.Color) *Theme

Foreground sets a foreground color.

func (*Theme) Format

func (t *Theme) Format() *Theme

func (*Theme) Italic

func (t *Theme) Italic(italic ...bool) *Theme

Italic enables italic rendering.

func (*Theme) Overline

func (t *Theme) Overline(overline ...bool) *Theme

Overline enables overline rendering.

func (*Theme) Render

func (t *Theme) Render(s string) string

func (*Theme) Reverse

func (t *Theme) Reverse(reverse ...bool) *Theme

Reverse enables reverse color mode.

func (*Theme) Underline

func (t *Theme) Underline(underline ...bool) *Theme

Underline enables underline rendering.

func (*Theme) WriteRendered added in v0.0.2

func (t *Theme) WriteRendered(buf *Buffer, s string)

type ThemeSchema

type ThemeSchema uint
const (
	ThemeTime ThemeSchema = iota + 1
	ThemeDebug
	ThemeInfo
	ThemeWarn
	ThemeError
	ThemePrefix
	ThemeCaller
	ThemeKey
	ThemeBracket // only json handler
)

func ThemeSchemaString

func ThemeSchemaString(s string) (ThemeSchema, error)

ThemeSchemaString retrieves an enum value from the enum constants string name. Throws an error if the param is not part of the enum.

func ThemeSchemaValues

func ThemeSchemaValues() []ThemeSchema

ThemeSchemaValues returns all values of the enum

func (ThemeSchema) IsAThemeSchema

func (i ThemeSchema) IsAThemeSchema() bool

IsAThemeSchema returns "true" if the value is listed in the enum definition. "false" otherwise

func (ThemeSchema) String

func (i ThemeSchema) String() string

type Themes added in v0.0.2

type Themes map[ThemeSchema]*Theme

Jump to

Keyboard shortcuts

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