slogsentry

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 6 Imported by: 0

README

Slog-Sentry

Sentry hook for the standard Golang logger slog.

A minimalist hook for the standard Golang logger slog, which enables sending our logs to the Sentry.

Upon hook initialization, you need to provide it with a standard log handler (TextHandler, JSONHandler, etc.), as well as the log levels you want to send to Sentry.

Messages of the Error level expect the original error as one of the following arguments: err or error.

Example
package main

import (
	"io"
	"log/slog"
	"os"
	"time"

	"github.com/getsentry/sentry-go"
	"github.com/ihippik/slog-sentry"
)

func main() {
	if err := sentry.Init(sentry.ClientOptions{
		Dsn: "myDSN",
	}); err != nil {
		slog.Error("init sentry", "err", err)
	}

	defer sentry.Flush(time.Second * 2)

	opt := slog.HandlerOptions{
		Level: slog.LevelDebug,
	}

	handler := slog.NewTextHandler(os.Stdout, &opt)
	hook := slogsentry.NewSentryHandler(handler, []slog.Level{slog.LevelWarn, slog.LevelError})

	logger := slog.New(hook)
	logger.Info("info message")
	logger.Warn("warn message")
	logger.Error("error message", "err", io.ErrNoProgress)
}

Documentation

Overview

Package slogsentry provides a slog Handler wrapper for the Sentry-go library.

Slog attributes are passed on to Sentry as context values. Some attribute keys are special. Attributes with a key prefixed with "tag_" are not stored in the context, but in the tags part of the Sentry event. The value of an attribute with the name "err" or "error" is included in the event message. Attributes with a name equal to a slog default key are ignored.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SentryHandler

type SentryHandler struct {
	slog.Handler
	// contains filtered or unexported fields
}

SentryHandler is a Handler that writes log records to the Sentry.

func NewSentryHandler

func NewSentryHandler(
	handler slog.Handler,
	levels []slog.Level,
) *SentryHandler

NewSentryHandler creates a SentryHandler that writes to w, using the given options.

func (*SentryHandler) Enabled

func (s *SentryHandler) Enabled(ctx context.Context, level slog.Level) bool

Enabled reports whether the handler handles records at the given level.

func (*SentryHandler) Handle

func (s *SentryHandler) Handle(ctx context.Context, record slog.Record) error

Handle intercepts and processes logger messages. In our case, send a message to the Sentry.

func (*SentryHandler) WithAttrs

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

WithAttrs returns a new SentryHandler with the given attributes stored.

func (*SentryHandler) WithGroup

func (s *SentryHandler) WithGroup(name string) slog.Handler

WithGroup returns a new SentryHandler whose group consists.

type SlogError

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

SlogEror contains both the slog msg and the actual error.

func (SlogError) Error

func (e SlogError) Error() string

Error appends both the msg and err from the SlogError.

func (SlogError) Unwrap

func (e SlogError) Unwrap() error

Jump to

Keyboard shortcuts

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