slogor

package module
v1.2.10 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2024 License: MIT Imports: 9 Imported by: 11

README

Slogor - A colorful slog handler

Slogor is a Go package designed to enhance logging capabilities. It provides functionalities for configuring the console mode and defining ANSI color codes, allowing developers to create more visually appealing and informative logs within their applications. With Slogor, users can improve the readability and presentation of their logs, making the debugging and monitoring process more efficient and intuitive.

slogor demo

✨ Features

  • 🚀 Fast and memory efficient
  • 🧑‍💻 Standard logging with slog and close to official TextHandler
  • 🎉 Concurrent/Thread-safe
  • 🌈 ANSI colored with automatic Windows support
  • ✅ Dependency free (except under Windows)
  • ♻️ Customizable writer, logging level and time format
  • 🔧 Debug with the caller path

❓ How to use

slog.SetDefault(slog.New(slogor.NewHandler(os.Stderr, slogor.Options{
    TimeFormat: various.DefaultLoggerTimeFormat,
    Level:      various.DefaultLoggerLevel,
    ShowSource: false,
})))

❓ How to hide time ?

Give an empty time format(TimeFormat) to not display the time

Documentation

Overview

Package slogor provides a colorful slog handler.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Err

func Err(err any) slog.Attr

Err creates a slog.Attr error from anything.

Types

type GroupOrAttrs added in v1.2.0

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

type Handler

type Handler struct {
	Writer io.Writer   // Writer is the destination for the log records.
	Mutex  *sync.Mutex // Mutex for handling concurrent access to the handler.

	Options Options // Options is the configuration for the log handler.
	// contains filtered or unexported fields
}

Handler is a slog.Handler that writes Records to an io.Writer as a sequence of colorful time, message, and pairs separated by spaces and followed by a newline.

func NewHandler

func NewHandler(writer io.Writer, options Options) *Handler

NewHandler creates a Handler that writes to w with the provided options.

func (*Handler) Enabled

func (h *Handler) 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.

func (*Handler) Handle

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

Handle processes the log record and writes it to the writer with appropriate formatting.

func (*Handler) WithAttrs

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

WithAttrs returns a new handler with the attrs attached to it.

func (*Handler) WithGroup

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

WithGroup returns a new handler with the new group attached to it.

type Options

type Options struct {
	// TimeFormat specifies the time format for log records.
	// Empty string will remove the time in records.
	TimeFormat string
	// Level is the minimum log level to handle.
	Level slog.Level
	// ShowSource indicates whether to display the source of log records.
	ShowSource bool
}

Options defines the options for configuring the Handler.

Jump to

Keyboard shortcuts

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