Skip to content

Commit

Permalink
Merge pull request #297 from airbrake/set-depth-logs
Browse files Browse the repository at this point in the history
Add custom method to set the depth of notices in apex/log and zerolog integration
  • Loading branch information
chimanjain committed Aug 22, 2022
2 parents ccec751 + 48a2611 commit a3539e4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 6 deletions.
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

0 comments on commit a3539e4

Please sign in to comment.