Documentation ¶
Overview ¶
Package pretty implements a slog.Handler that writes human-readable and optionally coloured logs.
The output format can be customised with Options.
Index ¶
- func NewHandler(w io.Writer, opts *Options) slog.Handler
- type Buffer
- func (b *Buffer) AppendBool(v bool)
- func (b *Buffer) AppendByte(p byte)
- func (b *Buffer) AppendBytes(p []byte)
- func (b *Buffer) AppendFloat(f float64, bitSize int)
- func (b *Buffer) AppendFloat32(f float32)
- func (b *Buffer) AppendFloat64(f float64)
- func (b *Buffer) AppendInt(i int64)
- func (b *Buffer) AppendQuote(s string)
- func (b *Buffer) AppendString(s string)
- func (b *Buffer) AppendTimeFormat(t time.Time, layout string)
- func (b *Buffer) AppendUint(i uint64)
- func (b *Buffer) Cap() int
- func (b *Buffer) Len() int
- func (b *Buffer) Replace(i int, p byte)
- func (b *Buffer) Reset()
- func (b *Buffer) String() string
- func (b *Buffer) Write(p []byte) (int, error)
- func (b *Buffer) WriteString(s string) (int, error)
- func (b *Buffer) WriteTo(writer io.Writer) (int64, error)
- type LevelFormatter
- type Options
- type ReplaceAttrFunc
- type SourceFormatter
- type TimeFormatter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewHandler ¶
NewHandler returns a slog.Handler that writes human-readable and optionally coloured logs to the writer.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a simple wrapper around a byte slice.
func (*Buffer) AppendBool ¶
AppendBool writes "true" or "false" to the buffer according to the given bool.
func (*Buffer) AppendByte ¶
AppendByte writes the given byte to the buffer.
func (*Buffer) AppendBytes ¶
AppendBytes writes the given byte slice to the buffer.
func (*Buffer) AppendFloat ¶
AppendFloat writes the given float to the buffer with the given bitSize.
func (*Buffer) AppendFloat32 ¶
AppendFloat32 writes the given float32 to the buffer.
func (*Buffer) AppendFloat64 ¶
AppendFloat64 writes the given float64 to the buffer.
func (*Buffer) AppendQuote ¶
AppendQuote writes a double-quoted string to the buffer using the strconv.AppendQuote function.
func (*Buffer) AppendString ¶
AppendString writes the given string to the buffer.
func (*Buffer) AppendTimeFormat ¶
AppendTimeFormat writes a timestamp to the buffer in the given format.
func (*Buffer) AppendUint ¶
AppendUint writes the given uint64 to the buffer.
func (*Buffer) Replace ¶
Replace replaces the byte at index i with the given byte, if the underlying byte slice contains index i.
func (*Buffer) WriteString ¶
WriteString writes the given string to the buffer.
type LevelFormatter ¶
LevelFormatter writes the formatted level to the buffer.
func DefaultLevelFormatter ¶
func DefaultLevelFormatter(color bool) LevelFormatter
DefaultLevelFormatter is the default LevelFormatter.
type Options ¶
type Options struct { // Level is the minimum [slog.Level] that will be logged. // Records with lower levels will be discarded. Level slog.Leveler // ReplaceAttr is used to rewrite each non-group [slog.Attr] before it is // logged. See https://pkg.go.dev/log/slog#HandlerOptions for details. ReplaceAttr ReplaceAttrFunc // AddSource enables computing the source code position of the log // statement and adds [slog.SourceKey] attributes to the output. AddSource bool // DisableColor disables the use of ANSI colour codes in messages. DisableColor bool // TimeFormatter is the [time.Time] formatter used to format log timestamps. TimeFormatter TimeFormatter // LevelFormatter is the [slog.Level] formatter used to format log levels. LevelFormatter LevelFormatter // SourceFormatter is the [slog.Source] formatter used to format log sources. SourceFormatter SourceFormatter }
Options allows you to customise the output format. This is a drop-in replacement for slog.HandlerOptions.
type ReplaceAttrFunc ¶
ReplaceAttrFunc is used to rewrite each non-group slog.Attr before it is logged.
type SourceFormatter ¶
SourceFormatter writes the formatted log source to the buffer.
func DefaultSourceFormatter ¶
func DefaultSourceFormatter(color bool) SourceFormatter
DefaultSourceFormatter is the default SourceFormatter.
type TimeFormatter ¶
TimeFormatter writes the formatted time to the buffer.
func DefaultTimeFormatter ¶
func DefaultTimeFormatter(layout string) TimeFormatter
DefaultTimeFormatter is the default TimeFormatter.