fblog

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2024 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package fblog provides slog handler and its options to print logs in the fblog-like style.

Index

Constants

View Source
const (
	// SourceFormatNone is the source format without any source.
	SourceFormatNone = 0

	// SourceFormatPos is the short source format,
	// e.g. "file:line".
	SourceFormatPos = 1

	// SourceFormatFunc is the short source format,
	// e.g. "func".
	SourceFormatFunc = 2

	// SourceFormatLong is the long source format,
	// e.g. "full/file/path:line:func".
	SourceFormatLong = 3
)
View Source
const (
	// HeaderKeySize looks for the header size of the log entry and
	// trims all the attribute keys to fit the header size.
	HeaderKeySize = 0
	// UnlimitedKeySize seeks for a maximum key size of the log entry
	// and formats the timestamp and level accordingly.
	UnlimitedKeySize = -1
)

Predefined key size options.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

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

Handler is a handler that prints logs in the fblog-like style, i.e. a new line for each attribute.

func NewHandler

func NewHandler(opts ...Option) *Handler

NewHandler returns a new Handler. The format of the logs entries will be:

	<Timestamp>      <Level>: <Message>
	                 <Attr1>: <Value1>
            <Group1>.<Attr2>: <Value2>
				     ...

func (*Handler) Enabled

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

Enabled returns true if the level is enabled.

func (*Handler) Handle

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

Handle writes the record to the writer.

func (*Handler) WithAttrs

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

WithAttrs returns a new Handler with the given attributes.

func (*Handler) WithGroup

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

WithGroup returns a new Handler with the given group.

type Option

type Option func(*Handler)

Option is an option for a Handler.

func Err

func Err(w io.Writer) Option

Err sets the error writer of the handler.

func Out

func Out(w io.Writer) Option

Out sets the output writer of the handler.

func WithLevel

func WithLevel(lvl slog.Level) Option

WithLevel returns an Option that sets the level of the handler.

func WithLogTimeFormat

func WithLogTimeFormat(f string) Option

WithLogTimeFormat returns an Option that sets the log's individual time format of the handler.

func WithMaxKeySize

func WithMaxKeySize(s int) Option

WithMaxKeySize returns an Option that sets the maximum key size of the handler. By default, the maximum key size is the "timestamp + level" size, if the key is bigger than that it will be trimmed from the left and "..." will be added at the beginning. Minimum length of the key is 7 (length of the level with braces) and any value that falls out of special cases will be set to UnlimitedKeySize.

func WithReplaceAttrs

func WithReplaceAttrs(r func([]string, slog.Attr) slog.Attr) Option

WithReplaceAttrs returns an Option that sets the function that will replace the attributes.

func WithSource

func WithSource(srcFormat SourceFormat) Option

WithSource returns an Option that sets the source of the handler.

func WithTimeFormat

func WithTimeFormat(f string) Option

WithTimeFormat returns an Option that sets the time format of the handler.

type SourceFormat

type SourceFormat uint8

SourceFormat is the source format of the handler.

Directories

Path Synopsis
internal
misc
Package misc provides miscellaneous data types and functions for better processing of log entries.
Package misc provides miscellaneous data types and functions for better processing of log entries.

Jump to

Keyboard shortcuts

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