lrhook

package
v0.0.0-...-0762aee Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: BSD-2-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package lrhook provides logrus hook for the Slack.

It can post messages to slack based on the notification level of the logrus entry including the ability to rate limit messages.

See: https://godoc.org/github.com/sirupsen/logrus#Hook

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLevelColors is the default level colors used if none are present in the configuration.
	DefaultLevelColors = map[string]string{
		"trace":   "#797979",
		"debug":   "#9B30FF",
		"info":    "good",
		"warning": "danger",
		"error":   "danger",
		"fatal":   "panic",
		"panic":   "panic",
	}

	// DefaultUnknownColor is the default UnknownColor if one is not present in the configuration.
	DefaultUnknownColor = "warning"
)

Functions

func SetConfigDefaults

func SetConfigDefaults(cfg *Config)

SetConfigDefaults sets defaults on the configuration if needed to ensure the cfg is valid.

Types

type Config

type Config struct {
	// MinLevel is the minimal level at which the hook will trigger.
	MinLevel logrus.Level

	// LevelColors is a hash of logrus level names to colors used for the attachment in the messages.
	LevelColors map[string]string

	// AttachmentText is the text message used for the attachment when fields are present in the log entry.
	AttachmentText string

	// UnknownColor is the color to use if there is no match for the log level in LevelColors.
	UnknownColor string

	// Async if true then messages are sent to slack asynchronously.
	// This means that Fire will never return an error.
	Async bool

	// Limit if none zero limits the number of messages to Limit posts per second.
	Limit rate.Limit

	// Burst sets the burst limit.
	// Ignored if Limit is zero.
	Burst int

	// Message defines the details of the messages sent from the hook.
	Message chat.Message

	// Attachment defines the details of the attachment sent from the hook.
	// Field Text - Will be set to that of log entry Message.
	// Field Fields - Will be created to match the log entry Fields.
	// Field Color - Will be set according to the LevelColors or UnknownColor if a match is not found..
	Attachment chat.Attachment
}

Config is the configuration of a slack logrus.Hook.

type Hook

type Hook struct {
	Config
	// contains filtered or unexported fields
}

Hook is a logrus hook that sends messages to Slack.

func New

func New(cfg Config, url string) *Hook

New returns a new Hook with the given configuration that posts messages using the webhook URL. It ensures that the cfg is valid by calling SetConfigDefaults on the cfg.

Example
cfg := Config{
	MinLevel: logrus.ErrorLevel,
	Message: chat.Message{
		Username:  "My App",
		Channel:   "#slack-testing",
		IconEmoji: ":ghost:",
	},
}
h := New(cfg, "https://hooks.slack.com/services/T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX")
logrus.SetFormatter(&logrus.JSONFormatter{})
logrus.SetLevel(logrus.InfoLevel)
logrus.AddHook(h)
logrus.WithFields(logrus.Fields{"field1": "test field", "field2": 1}).Error("test error")
Output:

func NewClient

func NewClient(cfg Config, client slack.Client) *Hook

NewClient returns a new Hook with the given configuration using the slack.Client c. It ensures that the cfg is valid by calling SetConfigDefaults on the cfg.

func (*Hook) Fire

func (sh *Hook) Fire(e *logrus.Entry) error

Fire implements logrus.Hook. It sends a slack message for the log entry e.

func (*Hook) Levels

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

Levels implements logrus.Hook. It returns the logrus.Level's that are lower or equal to that of MinLevel. This means setting MinLevel to logrus.ErrorLevel will send slack messages for log entries at Error, Fatal and Panic.

Jump to

Keyboard shortcuts

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