telegramhook

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2024 License: MIT Imports: 11 Imported by: 0

README

Telegram Logrus Hook

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

Installation

Install the package with:

go get github.com/andoma-go/logrus-hook-telegram

Usage

See the tests for working examples. Also:

import (
	"time"

	log "github.com/andoma-go/logrus"
	telegramhook "github.com/andoma-go/logrus-hook-telegram"
)

func main() {
	hook, err := telegramhook.NewTelegramHook(
		"MyCoolApp",
		"MYTELEGRAMTOKEN",
		"@mycoolusername",
		telegramhook.WithAsync(true),
		telegramhook.WithTimeout(30 * time.Second),
		telegramhook.WithLevel(logrus.ErrorLevel),
	)
	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 (
	"context"
	"net"
	"net/http"
	"time"

	log "github.com/andoma-go/logrus"
	telegramhook "github.com/andoma-go/logrus-hook-telegram"
	"golang.org/x/net/proxy"
)

func main() {
	dialer, err := proxy.SOCKS5("tcp", "127.0.0.1:54321", nil, proxy.Direct)
	dialContext := func(ctx context.Context, network, address string) (net.Conn, error) {
		return dialer.Dial(network, address)
	}
	httpTransport := &http.Transport{
		DialContext:       dialContext,
		DisableKeepAlives: true,
	}
	httpClient := &http.Client{Transport: httpTransport}

	hook, err := telegramhook.NewTelegramHookWithClient(
		"MyCoolApp",
		"MYTELEGRAMTOKEN",
		"@mycoolusername",
		httpClient,
		telegramhook.WithAsync(true),
		telegramhook.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...")
	...

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Option

type Option func(*TelegramHook)

Option defines a method for additional configuration when instantiating TelegramHook

func WithAsync

func WithAsync(async bool) Option

Async sets logging to telegram as asynchronous

func WithLevel

func WithLevel(level logrus.Level) Option

WithLevel set level

func WithTimeout

func WithTimeout(timeout time.Duration) Option

Timeout sets http call timeout for telegram client

type TelegramHook

type TelegramHook struct {
	// contains filtered or unexported fields
}

TelegramHook to send logs via the Telegram API.

func NewTelegramHook

func NewTelegramHook(appName, authToken, chatId, threadId string, options ...Option) (*TelegramHook, error)

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

func NewTelegramHookWithClient

func NewTelegramHookWithClient(appName, authToken, chatId, threadId string, client *http.Client, options ...Option) (*TelegramHook, error)

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

func (*TelegramHook) ApiEndpoint

func (h *TelegramHook) ApiEndpoint() string

ApiEndpoint

func (*TelegramHook) AppName

func (h *TelegramHook) AppName() string

AppName

func (*TelegramHook) Async

func (h *TelegramHook) Async() bool

Async

func (*TelegramHook) AuthToken

func (h *TelegramHook) AuthToken() string

AuthToken

func (*TelegramHook) ChatId

func (h *TelegramHook) ChatId() string

ChatId

func (*TelegramHook) Fire

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

Fire emits a log message to the Telegram API.

func (*TelegramHook) Level

func (h *TelegramHook) Level() logrus.Level

Level

func (*TelegramHook) Levels

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

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

func (*TelegramHook) SetAppName

func (h *TelegramHook) SetAppName(appName string)

func (*TelegramHook) SetAsync

func (h *TelegramHook) SetAsync(async bool)

func (*TelegramHook) SetAuthToken

func (h *TelegramHook) SetAuthToken(authToken string)

func (*TelegramHook) SetChatId

func (h *TelegramHook) SetChatId(chatId string)

func (*TelegramHook) SetLevel

func (h *TelegramHook) SetLevel(level logrus.Level)

func (*TelegramHook) SetThreadId

func (h *TelegramHook) SetThreadId(threadId string)

func (*TelegramHook) ThreadId

func (h *TelegramHook) ThreadId() string

ThreadId

Jump to

Keyboard shortcuts

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