Documentation ¶
Index ¶
- Constants
- Variables
- func CopyWithPrefix(prefix string) *slog.Logger
- func CopyWithThemes(themes Themes) *slog.Logger
- func ThemeSchemaStrings() []string
- type Buffer
- type Builder
- type File
- type Handler
- type JsonHandler
- func (h JsonHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h JsonHandler) Handle(_ context.Context, r slog.Record) error
- func (h JsonHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h JsonHandler) WithGroup(name string) slog.Handler
- func (j *JsonHandler) WithPrefix(prefix string) slog.Handler
- func (h JsonHandler) WriteColorful(schema ThemeSchema, buf *Buffer, s string)
- type Option
- func WithCaller() Option
- func WithFullCaller() Option
- func WithLevel(level slog.Level) Option
- func WithPrefix(prefix string) Option
- func WithReplacer(fn Replacer) Option
- func WithTheme(section ThemeSchema, theme *Theme) Option
- func WithTimeFormat(format string) Option
- func WithWriter(w io.Writer) Option
- type Replacer
- type TextHandler
- func (h TextHandler) Enabled(_ context.Context, level slog.Level) bool
- func (h TextHandler) Handle(_ context.Context, r slog.Record) error
- func (h TextHandler) WithAttrs(attrs []slog.Attr) slog.Handler
- func (h TextHandler) WithGroup(name string) slog.Handler
- func (t *TextHandler) WithPrefix(prefix string) slog.Handler
- func (t *TextHandler) WithThemes(themes Themes) slog.Handler
- func (h TextHandler) WriteColorful(schema ThemeSchema, buf *Buffer, s string)
- type Theme
- func (t *Theme) Background(light, dark colorful.Color) *Theme
- func (t *Theme) Blink(blink ...bool) *Theme
- func (t *Theme) Bold(bold ...bool) *Theme
- func (t *Theme) CrossOut(crossOut ...bool) *Theme
- func (t *Theme) Faint(faint ...bool) *Theme
- func (t *Theme) Foreground(light, dark colorful.Color) *Theme
- func (t *Theme) Format() *Theme
- func (t *Theme) Italic(italic ...bool) *Theme
- func (t *Theme) Overline(overline ...bool) *Theme
- func (t *Theme) Render(s string) string
- func (t *Theme) Reverse(reverse ...bool) *Theme
- func (t *Theme) Underline(underline ...bool) *Theme
- func (t *Theme) WriteRendered(buf *Buffer, s string)
- type ThemeSchema
- type Themes
Constants ¶
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 ¶
var CSI = []byte(string(ESC) + "[")
Functions ¶
func CopyWithPrefix ¶ added in v0.0.2
func CopyWithThemes ¶ added in v0.0.2
func ThemeSchemaStrings ¶
func ThemeSchemaStrings() []string
ThemeSchemaStrings returns a slice of all String values of the enum
Types ¶
type Buffer ¶
type Buffer []byte
func (*Buffer) WritePosInt ¶
func (*Buffer) WritePosIntWidth ¶
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 ¶
type JsonHandler ¶
type JsonHandler struct {
// contains filtered or unexported fields
}
func NewJsonHandler ¶
func NewJsonHandler(opts ...Option) *JsonHandler
func (JsonHandler) Enabled ¶
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 ¶
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 ¶
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 ¶
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 WithPrefix ¶
func WithTheme ¶
func WithTheme(section ThemeSchema, theme *Theme) Option
func WithTimeFormat ¶
func WithWriter ¶
type Replacer ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 (*Theme) Background ¶
Background sets a background color.
func (*Theme) Foreground ¶
Foreground sets a foreground color.
func (*Theme) WriteRendered ¶ added in v0.0.2
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