logrus_sentry

package
v0.8.7 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2015 License: MIT Imports: 5 Imported by: 0

README

Sentry Hook for Logrus

Sentry provides both self-hosted and hosted solutions for exception tracking. Both client and server are open source.

Usage

Every sentry application defined on the server gets a different DSN. In the example below replace YOUR_DSN with the one created for your application.

import (
  "github.com/Sirupsen/logrus"
  "github.com/Sirupsen/logrus/hooks/sentry"
)

func main() {
  log       := logrus.New()
  hook, err := logrus_sentry.NewSentryHook(YOUR_DSN, []logrus.Level{
    logrus.PanicLevel,
    logrus.FatalLevel,
    logrus.ErrorLevel,
  })

  if err == nil {
    log.Hooks.Add(hook)
  }
}

If you wish to initialize a SentryHook with tags, you can use the NewWithTagsSentryHook constructor to provide default tags:

tags := map[string]string{
  "site": "example.com",
}
levels := []logrus.Level{
  logrus.PanicLevel,
  logrus.FatalLevel,
  logrus.ErrorLevel,
}
hook, err := logrus_sentry.NewWithTagsSentryHook(YOUR_DSN, tags, levels)

If you wish to initialize a SentryHook with an already initialized raven client, you can use the NewWithClientSentryHook constructor:

import (
  "github.com/Sirupsen/logrus"
  "github.com/Sirupsen/logrus/hooks/sentry"
  "github.com/getsentry/raven-go"
)

func main() {
  log := logrus.New()

  client, err := raven.New(YOUR_DSN)
  if err != nil {
      log.Fatal(err)
  }

  hook, err := logrus_sentry.NewWithClientSentryHook(client, []logrus.Level{
    logrus.PanicLevel,
    logrus.FatalLevel,
    logrus.ErrorLevel,
  })

  if err == nil {
    log.Hooks.Add(hook)
  }
}

hook, err := NewWithClientSentryHook(client, []logrus.Level{
	logrus.ErrorLevel,
})

Special fields

Some logrus fields have a special meaning in this hook, these are server_name, logger and http_request. When logs are sent to sentry these fields are treated differently.

  • server_name (also known as hostname) is the name of the server which is logging the event (hostname.example.com)
  • logger is the part of the application which is logging the event. In go this usually means setting it to the name of the package.
  • http_request is the in-coming request(*http.Request). The detailed request data are sent to Sentry.

Timeout

Timeout is the time the sentry hook will wait for a response from the sentry server.

If this time elapses with no response from the server an error will be returned.

If Timeout is set to 0 the SentryHook will not wait for a reply and will assume a correct delivery.

The SentryHook has a default timeout of 100 milliseconds when created with a call to NewSentryHook. This can be changed by assigning a value to the Timeout field:

hook, _ := logrus_sentry.NewSentryHook(...)
hook.Timeout = 20*time.Second

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SentryHook

type SentryHook struct {
	// Timeout sets the time to wait for a delivery error from the sentry server.
	// If this is set to zero the server will not wait for any response and will
	// consider the message correctly sent
	Timeout time.Duration
	// contains filtered or unexported fields
}

SentryHook delivers logs to a sentry server.

func NewSentryHook

func NewSentryHook(DSN string, levels []logrus.Level) (*SentryHook, error)

NewSentryHook creates a hook to be added to an instance of logger and initializes the raven client. This method sets the timeout to 100 milliseconds.

func NewWithClientSentryHook added in v0.8.6

func NewWithClientSentryHook(client *raven.Client, levels []logrus.Level) (*SentryHook, error)

NewWithClientSentryHook creates a hook using an initialized raven client. This method sets the timeout to 100 milliseconds.

func NewWithTagsSentryHook added in v0.8.4

func NewWithTagsSentryHook(DSN string, tags map[string]string, levels []logrus.Level) (*SentryHook, error)

NewWithTagsSentryHook creates a hook with tags to be added to an instance of logger and initializes the raven client. This method sets the timeout to 100 milliseconds.

func (*SentryHook) Fire

func (hook *SentryHook) Fire(entry *logrus.Entry) error

Called when an event should be sent to sentry Special fields that sentry uses to give more information to the server are extracted from entry.Data (if they are found) These fields are: logger, server_name and http_request

func (*SentryHook) Levels

func (hook *SentryHook) Levels() []logrus.Level

Levels returns the available logging levels.

Jump to

Keyboard shortcuts

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