From 48a2611c61901126b348ecfe165444d10e9c9033 Mon Sep 17 00:00:00 2001 From: ChimanJain <36687396+ChimanJain@users.noreply.github.com> Date: Thu, 18 Aug 2022 11:33:14 +0530 Subject: [PATCH] add custom method to set the depth of notices --- apexlog/apexlog.go | 26 +++++++++++++++++++++++--- zerolog/zerolog.go | 12 +++++++++--- 2 files changed, 32 insertions(+), 6 deletions(-) diff --git a/apexlog/apexlog.go b/apexlog/apexlog.go index e2e91fb..2b42bf9 100644 --- a/apexlog/apexlog.go +++ b/apexlog/apexlog.go @@ -1,6 +1,8 @@ package apexlog import ( + "errors" + "github.com/airbrake/gobrake/v5" "github.com/apex/log" @@ -10,12 +12,30 @@ import ( type Handler struct { Gobrake *gobrake.Notifier HandlerSeverity log.Level + depth int +} + +func NewLogger(h *Handler) (*Handler, error) { + if h.Gobrake == nil { + return h, errors.New("airbrake notifier not defined") + } + h = &Handler{h.Gobrake, h.HandlerSeverity, h.depth} + return h, nil } // New returns a function that satisfies apex/log.Handler interface func New(notifier *gobrake.Notifier, level log.Level) *Handler { - h := Handler{notifier, level} - return &h + h, _ := NewLogger(&Handler{ + Gobrake: notifier, + HandlerSeverity: level, + depth: 4, + }) + return h +} + +// SetDepth method is for setting the depth of the notices +func (h *Handler) SetDepth(depth int) { + h.depth = depth } // HandleLog method is used for sending notices to airbrake. @@ -29,7 +49,7 @@ func (h *Handler) notifyAirbrake(level log.Level, msg string, params log.Fields) return } - notice := gobrake.NewNotice(msg, nil, 0) + notice := gobrake.NewNotice(msg, nil, h.depth) parameters := asParams(params) for key, parameter := range parameters { if key == "httpMethod" || key == "route" { diff --git a/zerolog/zerolog.go b/zerolog/zerolog.go index 3ff3741..d1f7e1c 100644 --- a/zerolog/zerolog.go +++ b/zerolog/zerolog.go @@ -13,17 +13,23 @@ import ( type WriteCloser struct { Gobrake *gobrake.Notifier + depth int } // Validates the WriteCloser matches the io.WriteCloser interface var _ io.WriteCloser = (*WriteCloser)(nil) // New creates a new WriteCloser -func New(notifier *gobrake.Notifier) (io.WriteCloser, error) { +func New(notifier *gobrake.Notifier) (*WriteCloser, error) { if notifier == nil { return &WriteCloser{}, errors.New("airbrake notifier not provided") } - return &WriteCloser{Gobrake: notifier}, nil + return &WriteCloser{Gobrake: notifier, depth: 6}, nil +} + +// SetDepth method is for setting the depth of the notices +func (w *WriteCloser) SetDepth(depth int) { + w.depth = depth } // Write parses the log data and sends off error notices to airbrake @@ -63,7 +69,7 @@ func (w *WriteCloser) Write(data []byte) (int, error) { return len(data), nil } - notice := gobrake.NewNotice(ze.message, nil, 6) + notice := gobrake.NewNotice(ze.message, nil, w.depth) notice.Context["severity"] = lvl // Check for the following 2 fields in logEntryData to see if they