Documentation ¶
Overview ¶
Package bug provides a simple & slow structured logger. It writes JSONL to stdout.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var Now func() time.Time = time.Now
Now is called by Measure to measure elapsed time.
Functions ¶
func HTTPPanicMiddleware ¶
HTTPPanicMiddleware returns an HTTP handler that recovers from a panic caused by the next handler. It logs a panic event including the value, type, and source location. After logging, the handler tries to respond with an internal server error.
func HTTPResponseMiddleware ¶
HTTPResponseMiddleware returns an HTTP handler that logs the response.
func Log ¶
Log calls Write to write a log event.
Example ¶
package main import ( "context" "os" "github.com/jbarnette/bug" ) func main() { bug.Write = bug.JSONL(os.Stdout) ctx := context.Background() bug.Log(ctx, "hello", bug.Tag("greeting", true), bug.Tag("subject", "world")) }
Output: {"at":"hello","greeting":true,"subject":"world"}
func With ¶
With returns a copy of parent with additional taggers.
Example ¶
package main import ( "context" "os" "github.com/jbarnette/bug" ) func main() { bug.Write = bug.JSONL(os.Stdout) ctx := context.Background() bug.Log(ctx, "with-background") ctx = bug.With(ctx, bug.Tag("outer", "value")) bug.Log(ctx, "with-outer") ctx = bug.With(ctx, bug.Tag("inner", "value")) bug.Log(ctx, "with-inner", bug.Tag("inline", "value")) }
Output: {"at":"with-background"} {"at":"with-outer","outer":"value"} {"at":"with-inner","inline":"value","inner":"value","outer":"value"}
Types ¶
type Span ¶
A Span is a context that covers the lifetime of an operation.
func WithSpan ¶
WithSpan returns a copy of parent with a CancelFunc that calls Log. The CancelFunc adds an "elapsed" tag, calculated by subtracting Now from the time when Span was called.
Example ¶
package main import ( "context" "os" "time" "github.com/jbarnette/bug" ) func main() { bug.Write = bug.JSONL(os.Stdout) ctx := context.Background() t := &time.Time{} bug.Now = func() time.Time { return *t } span, cancel := bug.WithSpan(ctx, "outside") defer cancel() span.Append(bug.Tag("outer", "value")) bug.Log(span, "inside", bug.Tag("inner", "value")) *t = t.Add(1 * time.Minute) }
Output: {"at":"inside","inner":"value"} {"at":"outside","elapsed":60,"outer":"value"}
type Tagger ¶
type Tagger func(tag func(key string, value any))
A Tagger is a generator function for log event tags.
func Error ¶
Error returns a tagger for the provided error. If the error is non-nil, the tagger generates error=true, error.message, and error.type tags.
Example ¶
package main import ( "context" "errors" "os" "github.com/jbarnette/bug" ) func main() { bug.Write = bug.JSONL(os.Stdout) ctx := context.Background() bug.Log(ctx, "no-boom", bug.Error(nil)) err := errors.New("kaboom") bug.Log(ctx, "boom", bug.Error(err)) }
Output: {"at":"no-boom"} {"at":"boom","error":true,"error.message":"kaboom","error.type":"*errors.errorString"}
type Writer ¶
A Writer is a function that generates and writes a log event.
func JSONL ¶
JSONL returns a log writer that writes JSON lines to w.
Example ¶
package main import ( "context" "os" "github.com/jbarnette/bug" ) type stringer struct{ string } func (s stringer) String() string { return s.string } func main() { bug.Write = bug.JSONL(os.Stdout) ctx := context.Background() bug.Log(ctx, "func", bug.Tag("value", func() {})) bug.Log(ctx, "chan", bug.Tag("value", make(chan struct{}))) bug.Log(ctx, "stringer", bug.Tag("value", stringer{"value"})) }
Output: {"at":"func","value":"💥"} {"at":"chan","value":"💥"} {"at":"stringer","value":"value"}