rollrus

package module
v0.0.0-...-fb9fad3 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2018 License: MIT Imports: 6 Imported by: 0

README

Build Status GoDoc

What

Rollrus is what happens when Logrus meets Roll.

When a .Error, .Fatal or .Panic logging function is called, report the details to rollbar via a Logrus hook.

Delivery is synchronous to help ensure that logs are delivered.

If the error includes a StackTrace, that StackTrace is reported to rollbar.

Usage

Examples available in the tests or on GoDoc.

Documentation

Overview

Package rollrus combines github.com/stvp/roll with github.com/sirupsen/logrus via logrus.Hook mechanism, so that whenever logrus' logger.Error/f(), logger.Fatal/f() or logger.Panic/f() are used the messages are intercepted and sent to rollbar.

Using SetupLogging should suffice for basic use cases that use the logrus singleton logger.

More custom uses are supported by creating a new Hook with NewHook and registering that hook with the logrus Logger of choice.

The levels can be customized with the WithLevels OptionFunc.

Specific errors can be ignored with the WithIgnoredErrors OptionFunc. This is useful for ignoring errors such as context.Canceled.

See the Examples in the tests for more usage.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReportPanic

func ReportPanic(token, env string)

ReportPanic attempts to report the panic to rollbar if the token is set

func SetupLogging

func SetupLogging(token, env string)

SetupLogging for use on Heroku. If token is not an empty string a rollbar hook is added with the environment set to env. The log formatter is set to a TextFormatter with timestamps disabled.

Example
SetupLogging("some-long-token", "staging")

// This will not be reported to Rollbar
logrus.Info("OHAI")

// This will be reported to Rollbar
logrus.WithFields(logrus.Fields{"hi": "there"}).Fatal("The end.")
Output:

func SetupLoggingForLevels

func SetupLoggingForLevels(token, env string, levels []logrus.Level)

SetupLoggingForLevels works like SetupLogging, but allows you to set the levels on which to trigger this hook.

Types

type Hook

type Hook struct {
	roll.Client
	// contains filtered or unexported fields
}

Hook is a wrapper for the rollbar Client and is usable as a logrus.Hook.

func NewHook

func NewHook(token string, env string, opts ...OptionFunc) *Hook

NewHook creates a hook that is intended for use with your own logrus.Logger instance. Uses the defualt report levels defined in wellKnownErrorFields.

Example
log := logrus.New()
hook := NewHook("my-secret-token", "production")
log.Hooks.Add(hook)

// This will not be reported to Rollbar
log.WithFields(logrus.Fields{"power_level": "9001"}).Debug("It's over 9000!")

// This will be reported to Rollbar
log.Panic("Boom.")
Output:

func NewHookForLevels

func NewHookForLevels(token string, env string, levels []logrus.Level) *Hook

NewHookForLevels provided by the caller. Otherwise works like NewHook.

func (*Hook) Fire

func (r *Hook) Fire(entry *logrus.Entry) error

Fire the hook. This is called by Logrus for entries that match the levels returned by Levels().

func (*Hook) Levels

func (r *Hook) Levels() []logrus.Level

Levels returns the logrus log.Levels that this hook handles

func (*Hook) ReportPanic

func (r *Hook) ReportPanic()

ReportPanic attempts to report the panic to rollbar using the provided client and then re-panic. If it can't report the panic it will print an error to stderr.

type OptionFunc

type OptionFunc func(*Hook)

OptionFunc that can be passed to NewHook.

func WithIgnoreErrorFunc

func WithIgnoreErrorFunc(fn func(error) bool) OptionFunc

WithIgnoreErrorFunc is an OptionFunc that receives the error that is about to be logged and returns true/false if it wants to fire a rollbar alert for.

func WithIgnoredErrors

func WithIgnoredErrors(errors ...error) OptionFunc

WithIgnoredErrors is an OptionFunc that whitelists certain errors to prevent them from firing.

func WithLevels

func WithLevels(levels ...logrus.Level) OptionFunc

WithLevels is an OptionFunc that customizes the log.Levels the hook will report on.

Jump to

Keyboard shortcuts

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