telegram_hook

package module
v0.0.0-...-eaf8924 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2018 License: MIT Imports: 9 Imported by: 1

README

Telegam Logrus Hook

Go Report Card GoDoc

This hook emits log messages (and corresponding fields) to the Telegram API for logrus. Currently this hook will only emit messages for the following levels:

  • ERROR
  • FATAL
  • PANIC

Installation

Install the package with:

go get github.com/rossmcdonald/telegram_hook

Usage

See the tests for working examples. Also:

import (
	"time"
	log "github.com/Sirupsen/logrus"
	"github.com/rossmcdonald/telegram_hook"
)

func main() {
	hook, err := telegram_hook.NewTelegramHook(
		"MyCoolApp",
		"MYTELEGRAMTOKEN",
		"@mycoolusername",
		telegram_hook.WithAsync(true),
		telegram_hook.WithTimeout(30 * time.Second),
	)
	if err != nil {
		log.Fatalf("Encountered error when creating Telegram hook: %s", err)
	}
	log.AddHook(hook)
	
	// Receive messages on failures
	log.Errorf("Uh oh...")
	...
	
}

Also you can set custom http.Client to use SOCKS5 proxy for example

import (
	"time"
	"net/http"
	
	"golang.org/x/net/proxy"
	log "github.com/Sirupsen/logrus"
	"github.com/rossmcdonald/telegram_hook"
)

func main() {
	httpTransport := &http.Transport{}
    httpClient := &http.Client{Transport: httpTransport}
    dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:54321", nil, proxy.Direct)
    httpTransport.Dial = dialer.Dial
    
	hook, err := telegram_hook.NewTelegramHookWithClient(
		"MyCoolApp",
		"MYTELEGRAMTOKEN",
		"@mycoolusername",
		httpClient,
		telegram_hook.WithAsync(true),
		telegram_hook.WithTimeout(30 * time.Second),
	)
	if err != nil {
		log.Fatalf("Encountered error when creating Telegram hook: %s", err)
	}
	log.AddHook(hook)
	
	// Receive messages on failures
	log.Errorf("Uh oh...")
	...
	
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config func(*TelegramHook)

Config defines a method for additional configuration when instantiating TelegramHook

func WithAsync

func WithAsync(b bool) Config

Async sets logging to telegram as asynchronous

func WithTimeout

func WithTimeout(t time.Duration) Config

Timeout sets http call timeout for telegram client

type TelegramHook

type TelegramHook struct {
	AppName string
	Level   logrus.Level
	// contains filtered or unexported fields
}

TelegramHook to send logs via the Telegram API.

func NewTelegramHook

func NewTelegramHook(appName, authToken, targetID string, config ...Config) (*TelegramHook, error)

NewTelegramHook creates a new instance of a hook targeting the Telegram API.

func NewTelegramHookWithClient

func NewTelegramHookWithClient(appName, authToken, targetID string, client *http.Client, config ...Config) (*TelegramHook, error)

NewTelegramHook creates a new instance of a hook targeting the Telegram API with custom http.Client.

func (*TelegramHook) Fire

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

Fire emits a log message to the Telegram API.

func (*TelegramHook) Levels

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

Levels returns the log levels that the hook should be enabled for.

Jump to

Keyboard shortcuts

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