slacker

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2021 License: MIT Imports: 11 Imported by: 0

README

Slacker

A Slack api for Go, to redirect critical server errors in real time in a Slack channel.

go get github.com/a-novel/slacker

Prerequisite

You need a Slack application that supports incoming webhooks.

Configuration

Create a JSON file with 2 keys : your Slack channel webhook and an optional application name.

{
  "webhook":     "https://hooks.slack.com/services/XXXXX/XXXXX/XXXXXXX",
  "application": "My Super App"
}

Init

Once your configuration file is ready, you can run the init function in your main go file:

package myPackage

import (
	"github.com/a-novel/slacker"
	"log"
)

func init() {
	logger, err := slacker.InitFromFile("/path/to/configuration/file.json")
	if err != nil {
		log.Fatal(err.Error())
	}
}

Handle errors

Once your client is ready, you can use your logger object to send preformatted messages to your Slack channel.

func main() {
    // Do stuff..
	// An error occurs......
	
	logger.Errorf("an error occurred: %s", err.Error())
	// You can also use logger.Fatalf() to shut down your server because of a critical error.
}

Usage with Gin

You can use Slacker to automatically catch gin aborted requests:

package myPackage

import (
	"github.com/a-novel/slacker"
	"github.com/gin-gonic/gin"
)

func main() {
	r := gin.Default()
	
	logger.UseGinFormatter(r)
}

License

2021, A-Novel MIT License.

Documentation

Index

Constants

View Source
const (
	ErrMissingWebHook           = "err_missing_webhook"
	ErrUnexpectedError          = "err_unexpected_error"
	ErrCannotReachSlackServer   = "err_cannot_reach_slack_server"
	ErrUnexpectedServerResponse = "err_unexpected_server_response"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type API

type API struct {
	WebHook     string `json:"webhook"`
	Application string `json:"application"`
}

func Init

func Init(config *API) (*API, *errors.Error)

func InitFromFile

func InitFromFile(configPath string) (*API, *errors.Error)

func (*API) Error

func (api *API) Error(m string) *errors.Error

func (*API) Errorf

func (api *API) Errorf(format string, a ...interface{}) *errors.Error

func (*API) Fatal

func (api *API) Fatal(m string)

func (*API) Fatalf

func (api *API) Fatalf(format string, a ...interface{})

func (*API) GinError added in v0.1.1

func (api *API) GinError(c *gin.Context, m string)

func (*API) GinFormatter

func (api *API) GinFormatter(param gin.LogFormatterParams) string

func (*API) Log added in v0.1.2

func (api *API) Log(m, n, c string) *errors.Error

func (*API) Print

func (api *API) Print(m string) string

func (*API) Send

func (api *API) Send(m string, b map[string]interface{}, at []map[string]interface{}) *errors.Error

func (*API) UseGinFormatter

func (api *API) UseGinFormatter(router *gin.Engine)

func (*API) Verify

func (api *API) Verify() *errors.Error

Jump to

Keyboard shortcuts

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