log

package module
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2026 License: MIT Imports: 14 Imported by: 1

README

log

simple structured log wrapper for go, with tint by lmittmann/tint 🌈

badge

examples

import logger "github.com/grackleclub/log"

var log *slog.Logger // a global logger can be set

// If empty handler options are provided, the DEBUG
// env var will determine log level and source info.
_ := os.Setenv("DEBUG", "1")

// No color env var is always respected,
// but no effort is made to autodetect.
_ := os.Unsetenv("NO_COLOR")

log, _ = logger.New(slog.HandlerOptions{})

// Because this is regular slog, you can use log.With
// or attrs or any other slog features.
log = log.With("service", "cool_service")


log.Info("logging initialized", "msg", "hello world")

Documentation

Index

Constants

View Source
const (
	IconDebug = "🔷"
	IconInfo  = "🟢"
	IconWarn  = "🟨"
	IconError = "❌"
	IconRun   = "▶"
)
View Source
const ISO8601 = "2006-01-02T15:04:05.000Z"

Variables

This section is empty.

Functions

func Init added in v0.2.0

func Init(level slog.Level)

Init puts a pretty custom handler on otherwise standard log/slog. WARN:this is the old way of doing it, not preferred. (Why? Delete?)

func New

func New(opts slog.HandlerOptions, writers ...io.Writer) (*slog.Logger, error)

New creates a new slog.Logger with a tint handler that defaults to stderr. If empty slog.HandlerOptions are provided, env var DEBUG is checked. If DEBUG is unset:

  • Level: slog.LevelInfo
  • AddSource: false.

If DEBUG is set:

  • Level: slog.LevelDebug
  • AddSource: true.

NO_COLOR environment variable disables color output.

func NewWithHandlers added in v1.2.0

func NewWithHandlers(opts slog.HandlerOptions, extra ...slog.Handler) (*slog.Logger, error)

NewWithHandlers creates a logger that fans out to the default tint handler (stderr) plus any additional slog.Handlers, such as an OpenTelemetry log handler. Equivalent to New when no extras are given.

func WaitSpinner added in v0.2.0

func WaitSpinner(ctx context.Context, message string, s spinner) (*sync.WaitGroup, context.CancelCauseFunc)

WaitSpinner creates a new spinner, providing a wait group and spinner cancel function. Use it to provide a visual indication of a long running process.

ctx := context.Background()
// start a spinner
wg, cancel := WaitSpinner(, "waiting on foo", runner)
defer wg.Wait()
_, err := doSomething()
// stop the spinner with err values (nil means success)
cancel(err)
// then handle error as normal
if err != nil {
	// handle error
}

Types

type SlogHandler added in v0.2.0

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

func (*SlogHandler) Enabled added in v0.2.0

func (h *SlogHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*SlogHandler) Handle added in v0.2.0

func (h *SlogHandler) Handle(ctx context.Context, r slog.Record) error

func (*SlogHandler) WithAttrs added in v0.2.0

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

func (*SlogHandler) WithGroup added in v0.2.0

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

TODO this is no-op

type TUI

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

func NewTUI

func NewTUI(opts slog.HandlerOptions) (*TUI, error)

NewTUI returns a new TUI printer

func (*TUI) Debug

func (t *TUI) Debug(msg string, fields ...interface{})

func (*TUI) Error

func (t *TUI) Error(msg string, fields ...interface{})

func (*TUI) Info

func (t *TUI) Info(msg string, fields ...interface{})

func (*TUI) Table added in v0.1.9

func (r *TUI) Table(msg string, objects []interface{})

TableHeader prints the table header TODO - experimental - full of bugs :)

func (*TUI) Warn

func (t *TUI) Warn(msg string, fields ...interface{})

Jump to

Keyboard shortcuts

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