Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom method to set the depth of notices in apex/log and zerolog integration #297

Merged
merged 1 commit into from
Aug 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions apexlog/apexlog.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package apexlog

import (
"errors"

"github.com/airbrake/gobrake/v5"

"github.com/apex/log"
Expand All @@ -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.
Expand All @@ -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" {
Expand Down
12 changes: 9 additions & 3 deletions zerolog/zerolog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down