From 221cdc66f9fc9baf89e6e25d288c40be2477d576 Mon Sep 17 00:00:00 2001 From: Saleh Saeed Date: Wed, 8 Nov 2023 22:39:19 +0100 Subject: [PATCH] feat: add NewWithHub function to utilize existing Sentry hub --- writer.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/writer.go b/writer.go index 5f1a87e..e7fee6f 100644 --- a/writer.go +++ b/writer.go @@ -2,6 +2,7 @@ package zlogsentry import ( "crypto/x509" + "errors" "io" "net/http" "time" @@ -355,6 +356,29 @@ func New(dsn string, opts ...WriterOption) (*Writer, error) { }, nil } +// NewWithHub creates a writer using an existing sentry Hub and options. +func NewWithHub(hub *sentry.Hub, opts ...WriterOption) (*Writer, error) { + if hub == nil { + return nil, errors.New("hub cannot be nil") + } + + cfg := newDefaultConfig() + for _, opt := range opts { + opt.apply(&cfg) + } + + levels := make(map[zerolog.Level]struct{}, len(cfg.levels)) + for _, lvl := range cfg.levels { + levels[lvl] = struct{}{} + } + + return &Writer{ + hub: hub, + levels: levels, + flushTimeout: cfg.flushTimeout, + }, nil +} + func newDefaultConfig() config { return config{ levels: []zerolog.Level{