instrument

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 10, 2024 License: CC0-1.0 Imports: 16 Imported by: 0

README

cover

Usage

package main

import (
    "context"

    "github.com/gaylatea/instrument"
)

func main() {
    ctx := context.Background()
    instrument.Infof(ctx, "Hello!")
}

// Emits a log line:
// {"meta.instance": "018feac5-27db-7dcb-bcaf-483155a5ea06", "meta.timestamp": "2024-06-05T23:39:00Z", "meta.level": "INF", "meta.caller": "main.main", "meta.file": "/Users/gaylatea/src/instrument/example/main.go", "meta.line": 77, "log.message": "Hello!"}

Tags

instrument uses tags to contextualize events, allowing you to trace the path of a log. To add tags, use:

newCtx := instrument.With(ctx, "tag.new", true)
newCtx := instrument.WithAll(ctx, instrument.Tags{
    "key":     "value",
    "another": time.Now(),
})

Logs

To emit a log line with levels, use:

instrument.Tracef(ctx, "Trace text")
instrument.Debugf(ctx, "Debug text")

instrument.Infof(ctx, "Info text")
instrument.Warnf(ctx, "Warning text")
instrument.Errorf(ctx, "Error text")

To force exit the program with a log line, you can use:

instrument.Fatalf(ctx, "This should stop the program.")

All the preceding logs support fmt.Sprintf formatting.

instrument suppresses Debugf or Tracef logs by default. To turn them on:

instrument.SetDebug(true)
instrument.SetTrace(true) // implies SetDebug(true)

Traces

Tracing wraps a block of code with timing and call stack information. To start a trace:

if err := instrument.WithSpan(ctx, "Name", func(ctx context.Context, addToParent func(instrument.Tags)) error {
    // Your code here.
}); err != nil {
    // Use the returned error.
}

The provided addToParent function adds tags to the created span from your code.

Events

To emit an event without the tracing or logging metadata:

instrument.PostEvent(ctx, "Name", instrument.Tags{
    "something": "you need",
})

Unlike logs and traces, raw events don't contain tags from the provided context.

Sinks

Terminal

instrument uses a default terminal sink to emit newline-delimited JSON to stderr, with optional colors in a TTY.

You can turn the default sink off with:

instrument.Silence(true)

Custom

To add a sink, implement the instrument.Sink interface.

To set a sink for all events:

instrument.UseSink(yourSink)

To set a sink for a context and its descendants:

newCtx := instrument.WithSink(ctx, yourSink)

Example

A sample program with all available features: example/main.go

Example instrument output

License

instrument is dedicated to the public domain. See the CC0-1.0 License for details.

Made with Love

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Colorize

func Colorize()

Colorize forces colorized output, even without a TTY.

func Debugf

func Debugf(ctx context.Context, msg string, args ...interface{})

Debugf prints debug information when in debug mode.

func Errorf

func Errorf(ctx context.Context, msg string, args ...interface{})

Errorf prints an error log to the console.

func Fatalf

func Fatalf(ctx context.Context, msg string, args ...interface{})

Fatalf prints an error and quits the app.

func Infof

func Infof(ctx context.Context, msg string, args ...interface{})

Infof prints an informational string to the console.

func PostEvent

func PostEvent(ctx context.Context, name string, givenTags Tags)

PostEvent emits a user-created raw event without contextual metadata.

func ResetColor

func ResetColor()

ResetColor returns to lipgloss' default color handling.

func SetDebug

func SetDebug(to bool)

SetDebug sets the visibility of debug events.

func SetTrace

func SetTrace(to bool)

SetTrace sets the visibility of trace and debug events.

func Silence

func Silence(to bool)

Silence toggles the default terminal output.

func Tracef

func Tracef(ctx context.Context, msg string, args ...interface{})

Tracef prints tracing information when in trace mode.

func UseSink

func UseSink(name string, newSink Sink)

UseSink sets a global sink for all events.

func Warnf

func Warnf(ctx context.Context, msg string, args ...interface{})

Warnf prints a warning message.

func With

func With(ctx context.Context, k string, v any) context.Context

With adds a single tag to the given context.

func WithAll

func WithAll(ctx context.Context, tags Tags) context.Context

WithAll adds multiple tags to the given context.

func WithSink

func WithSink(ctx context.Context, name string, newSink Sink) context.Context

WithSink adds a sink for the given context.

func WithSpan

func WithSpan(ctx context.Context, name string, traced TraceFunc) error

WithSpan runs a given function and emits trace-specific metadata.

Types

type Level

type Level int

Level represents a standard logging level.

const (
	TRACE Level = iota
	DEBUG
	INFO
	WARN
	ERROR
	FATAL
)

func (Level) SetStyle

func (l Level) SetStyle(s *lipgloss.Style)

SetStyle changes the configured lipgloss style for the level.

func (Level) String

func (l Level) String() string

String returns a short name for the level.

func (Level) Style

func (l Level) Style() *lipgloss.Style

Style returns the configured lipgloss style for the level.

type Sink

type Sink interface {
	Event(ctx context.Context, t Tags) error
}

Sink implementers receive events and pass them along to downstream systems.

type Tags

type Tags map[string]any

type TerminalSink

type TerminalSink struct{}

TerminalSink emits events to the terminal with optional colors.

func (*TerminalSink) Event

func (cs *TerminalSink) Event(_ context.Context, givenTags Tags) error

Event writes colorized JSON to the terminal for debugging.

type TraceFunc

type TraceFunc func(ctx context.Context, addToParent func(Tags)) error

TraceFunc implementers run in the context of a trace.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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