golog

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2025 License: MIT Imports: 18 Imported by: 0

README

Log handler for the Go log/slog package

Overview

There are three handlers implemented in this package with a heavy focus on the development handler.

  1. Development - logging in development mode.
  2. Discard - all gets discarded, useful for integration testing where log lines can become noise.
  3. Production - a wrapper around slog's JSON handler with sensible defaults.

Usage - Development

This handler should only be used in development. It formats the log lines where the attribute keys are color coded.

It is encouraged to always set the default logger with slog.SetDefault(...) in order to not "depend" directly on this logger in your code base.

Example
devLogger := golog.NewDevelopment()
slog.SetDefault(devLogger)

slog.Info("this log will be pretty printed")
Output

Example code used to generate the output: https://go.dev/play/p/Y0d-i5_SutP

Pasted_Image_21_04_2024__14_39

JSON Output

If the data passed in as a string or []byte is a valid JSON object, it will be automatically formatted, colorized, and its type marked as JSON.

image


Usage - Discard

All log lines will be discarded. Should be used for integration tests.

Example
discardLogger := golog.NewDiscard()
slog.SetDefault(discardLogger)

slog.Info("this log will be discarded")

Usage - Production

The production handler is just a wrapper for slog's JSON handler with a few sensible defaults:

  • Logs are output to stderr to align it to the POSIX standard
  • The default log level is slog.LevelInfo
  • slog's TimeKey is replaced in JSON to a property named t
  • slog's source is enabled and formatted to <file:line>, e.g. my_file.go:17
Example
prodLogger := golog.NewProduction()
slog.SetDefault(prodLogger)

slog.Info("this log will be output to stderr and formatted as JSON")

Documentation

Overview

code_snippet.go

dev_tera_handler.go

formatter.go

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatCodeSnippet added in v0.5.0

func FormatCodeSnippet(filePath string, lineNumber int, level slog.Level, config CodeSnippetConfig) string

FormatCodeSnippet extracts and formats a code snippet from a file at the specified line

func NewDevelopment

func NewDevelopment() *slog.Logger

func NewDiscard

func NewDiscard() *slog.Logger

func NewProduction

func NewProduction() *slog.Logger

Types

type CodeSnippetConfig added in v0.5.0

type CodeSnippetConfig struct {
	// Number of context lines to show before and after the target line
	ContextLines int
	// Styling options
	ErrorLineStyle    lipgloss.Style
	DebugLineStyle    lipgloss.Style
	InfoLineStyle     lipgloss.Style
	WarnLineStyle     lipgloss.Style
	ContextLineStyle  lipgloss.Style
	LineNumberStyle   lipgloss.Style
	ErrorPointerStyle lipgloss.Style
	// Text to show under different log levels
	ErrorPointerText string
	DebugPointerText string
	InfoPointerText  string
	WarnPointerText  string
}

CodeSnippetConfig holds configuration options for code snippet display

func DefaultCodeSnippetConfig added in v0.5.0

func DefaultCodeSnippetConfig() CodeSnippetConfig

DefaultCodeSnippetConfig returns the default configuration for code snippets

type DevHandler

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

func (*DevHandler) Enabled

func (p *DevHandler) Enabled(_ context.Context, level slog.Level) bool

func (*DevHandler) Handle

func (p *DevHandler) Handle(_ context.Context, r slog.Record) error

func (*DevHandler) WithAttrs

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

func (*DevHandler) WithGroup

func (p *DevHandler) WithGroup(name string) slog.Handler

type Formatter added in v0.5.0

type Formatter interface {
	// Format returns a formatted byte slice for a given slog record.
	Format(r slog.Record) ([]byte, error)
}

Formatter defines how to convert a slog.Record into styled output.

type TeraHandler added in v0.5.0

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

TeraHandler is a custom slog.Handler that formats log records with borders, colored text, and code snippets.

func NewTeraHandler added in v0.5.0

func NewTeraHandler(opts TeraOptions) *TeraHandler

NewTeraHandler creates a new TeraHandler using the provided options.

func (*TeraHandler) Enabled added in v0.5.0

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

Enabled is part of the slog.Handler interface.

func (*TeraHandler) Handle added in v0.5.0

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

Handle formats the slog record in Tera style and writes it to stdout.

func (*TeraHandler) WithAttrs added in v0.5.0

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

WithAttrs returns a new handler with additional attributes.

func (*TeraHandler) WithGroup added in v0.5.0

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

WithGroup returns a new handler with an additional group.

type TeraOptions added in v0.5.0

type TeraOptions struct {
	HeaderTitle      string         // e.g. "Traceback (most recent call last)"
	BorderColor      lipgloss.Color // For header/footer borders.
	PanelBorderColor lipgloss.Color // For attribute panel borders.
	DateFormat       string         // e.g. time.RFC3339; if empty a default is used.
}

TeraOptions holds style options for the Tera handler.

Directories

Path Synopsis
cmd
demo command
cmd/tool/demo/main.go
cmd/tool/demo/main.go

Jump to

Keyboard shortcuts

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