Help us shape the future of Go! Take the 2026 Go Developer Survey

zuliprus

package module
v0.0.0-...-8c6da2f Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2020 License: Apache-2.0 Imports: 6 Imported by: 0

README

zuliprus

Zulip hook for Logrus.

Example output

sample

Use

package main

import (
	"os"

	logrus "github.com/sirupsen/logrus"
	"github.com/tiger5226/zuliprus"
)

func main() {

	logrus.SetFormatter(&logrus.JSONFormatter{})

	logrus.SetOutput(os.Stderr)

	logrus.SetLevel(logrus.DebugLevel)

	logrus.AddHook(&zuliprus.ZuliprusHook{
		APIURL:         "https://zulip.mycompany.com/api/v1/",
		APIKey:         "aP8vzq5gwfZHBd4V6ztcYzO4Jugczgt6",
		Email:          "my-bot@zulip.mycompany.com",
		AcceptedLevels: zuliprus.LevelThreshold(logrus.DebugLevel),
		Stream:         "mystream",
		Topic:          "that-topic",
	})

	logrus.Debug("this is a debug level message")
	logrus.Info("this is an info level message")
	logrus.Error("this is an error level message")
	logrus.Warning("this is a warning level message")

}

Parameters

Required
  • APIURL
  • APIKey
  • Email
  • Stream
Optional
  • AcceptedLevels
  • UserEmails
  • Topic
  • Asynchronous
  • Disabled
  • FormatFn

Installation

go get github.com/tiger5226/zuliprus

FormatFn

This can be changed to your liking by setting your own function. It controls the message output sent to zulip for an individual entry

var MsgFmtFn = func(e *logrus.Entry) string {
	return fmt.Sprintf("%s>  *%s*  : %s", LevelPrefixFn(e), e.Time.Format("2006-01-02T15:04:05"), e.Message)
}

LevelPrefixFn

This controls how we color the output to zulip which uses Zenburn style in dark mode. If you are in light mode, this possible won't look like the same. So this is an entry point for you to set your own.

var LevelPrefixFn = func(e *logrus.Entry) string {
	color := ""
	switch e.Level {
	case logrus.DebugLevel:
		color = "```c++\nDEBUG\n```\n"
	case logrus.InfoLevel:
		color = "```bash\nINFO\n```\n"
	case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
		color = "```terraform\nERROR\n```\n"
	default:
		color = "```apacheconf\nWARN\n```\n"
	}

	return color
}

Credits

Based on slackrus hook by johntdyer

Documentation

Overview

Package zuliprus provides a Slack hook for the logrus loggin package.

Index

Constants

View Source
const (
	VERISON = "0.0.3"
)

Project version

Variables

Supported log levels

View Source
var LevelPrefixFn = func(e *logrus.Entry) string {
	color := ""
	switch e.Level {
	case logrus.DebugLevel:
		color = "```c++\nDEBUG\n```\n"
	case logrus.InfoLevel:
		color = "```bash\nINFO\n```\n"
	case logrus.ErrorLevel, logrus.FatalLevel, logrus.PanicLevel:
		color = "```terraform\nERROR\n```\n"
	default:
		color = "```apacheconf\nWARN\n```\n"
	}

	return color
}
View Source
var MsgFmtFn = func(e *logrus.Entry) string {
	return fmt.Sprintf("%s>  *%s*  : %s", LevelPrefixFn(e), e.Time.Format("2006-01-02T15:04:05"), e.Message)
}

Functions

func LevelThreshold

func LevelThreshold(l logrus.Level) []logrus.Level

LevelThreshold - Returns every logging level above and including the given parameter.

Types

type FmtFunction

type FmtFunction func(e *logrus.Entry) string

type LevelPrefix

type LevelPrefix func(e *logrus.Entry) string

type ZuliprusHook

type ZuliprusHook struct {
	// Messages with a log level not contained in this array
	// will not be dispatched. If nil, all messages will be dispatched.
	AcceptedLevels []logrus.Level
	APIURL         string
	APIKey         string
	Email          string
	Stream         string
	UserEmails     []string
	Topic          string
	Asynchronous   bool
	Disabled       bool
	FormatFn       FmtFunction
}

ZuliprusHook is a logrus Hook for dispatching messages to the specified channel on Slack.

func (*ZuliprusHook) Fire

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

Fire - Send event to zulip

func (*ZuliprusHook) Levels

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

Levels sets which levels to sent to slack

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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