tghook

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

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

Go to latest
Published: Jun 24, 2021 License: MIT Imports: 9 Imported by: 0

README

Telegram Logrus Hook

Instead of the original, this package is able to configure the standard logging level.

Go Report Card GoDoc

This hook emits log messages (and corresponding fields) to the Telegram API for logrus.

Installation

Install the package with:

go get github.com/vad1malekseev/tghook

Usage

See the tests for working examples. Also:

package main

import (
	"time"
	
	
	"github.com/Sirupsen/logrus"
	"github.com/vad1malekseev/tghook"
)

func main() {
	hook, err := tghook.NewTelegramHook(
		"AppName", // needed to format telegram-messages 
		"secret",
		"@username",
		tghook.WithAsync(true),
		tghook.WithTimeout(30 * time.Second),
		tghook.WithMinLevel(logrus.InfoLevel), // default level is ErrorLevel
	)
	if err != nil {
		logrus.Fatalf("Encountered error when creating Telegram hook: %s", err)
	}
	logrus.AddHook(hook)
	
	logrus.Info("App started")
}

All existing levels in descending order:

  • PanicLevel = 0
  • FatalLevel = 1
  • ErrorLevel = 2
  • WarnLevel = 3
  • InfoLevel = 4
  • DebugLevel = 5
  • TraceLevel = 6

If the minimum level equal to InfoLevel, you will receive notifications of the following levels:

  • PanicLevel
  • FatalLevel
  • ErrorLevel
  • WarnLevel
  • InfoLevel

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

package main

import (
	"time"
	"net/http"
	
	"golang.org/x/net/proxy"
	"github.com/Sirupsen/logrus"
	"github.com/vad1malekseev/tghook"
)

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 := tghook.NewTelegramHookWithClient(
		"AppName",
		"secret",
		"@username",
		httpClient,
		tghook.WithAsync(true),
		tghook.WithTimeout(30 * time.Second),
	)
	if err != nil {
		logrus.Fatalf("Encountered error when creating Telegram hook: %s", err)
	}
    logrus.AddHook(hook)
	
	// Receive messages on failures
    logrus.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

WithAsync sets logging to telegram as asynchronous

func WithMinLevel

func WithMinLevel(level logrus.Level) Config

func WithTimeout

func WithTimeout(t time.Duration) Config

WithTimeout sets http call timeout for telegram client

type TelegramHook

type TelegramHook struct {
	AppName string
	// 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)

NewTelegramHookWithClient 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