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.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // DefaultLevelColors is the default level colors used if none are present in the configuration. DefaultLevelColors = map[string]string{ "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 ¶
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 ¶
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.