slogsentry

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 20, 2023 License: MIT Imports: 4 Imported by: 1

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

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 whose attributes consists.

func (*SentryHandler) WithGroup

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

WithGroup returns a new SentryHandler whose group consists.

Jump to

Keyboard shortcuts

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