slogtelegram

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2024 License: MIT Imports: 12 Imported by: 0

README

Telegram handler for slog

Release Software License codecov godoc Go Report Card

This is a handler for the slog package that sends log messages to a Telegram chat.

Installation

go get github.com/platx/slog-telegram

Minimum Go version: 1.23

Usage

License

MIT licensed. See the LICENSE file for details.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BatchSender

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

func NewBatchSender

func NewBatchSender(parent Sender, batchSize uint64, flushInterval time.Duration) *BatchSender

func (*BatchSender) Close

func (s *BatchSender) Close() error

func (*BatchSender) Send

func (s *BatchSender) Send(msg string) error

type Formatter

type Formatter interface {
	Format(record slog.Record) (string, error)
}

func NewFormatter

func NewFormatter(opts FormatterOptions) Formatter

type FormatterOptions

type FormatterOptions struct {
	// Template is a Go template to format the log record.
	Template string

	// LevelFormatter is a custom function to format the log level.
	LevelFormatter func(slog.Record) string

	// TimeFormatter is a custom function to format the log time.
	TimeFormatter func(slog.Record) string

	// MessageFormatter is a custom function to format the log message.
	MessageFormatter func(slog.Record) string

	// AttrsFormatter is a custom function to format the log attributes.
	AttrsFormatter func(slog.Record) string

	// Instance is a custom formatter instance to use.
	Instance Formatter
}

type Handler

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

func DefaultHandler

func DefaultHandler(token string, chatID int64) *Handler

func NewHandler

func NewHandler(opts HandlerOptions) *Handler

func (*Handler) Close

func (h *Handler) Close() error

func (*Handler) Enabled

func (h *Handler) Enabled(_ context.Context, level slog.Level) bool

func (*Handler) Handle

func (h *Handler) Handle(_ context.Context, record slog.Record) error

func (*Handler) WithAttrs

func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*Handler) WithGroup

func (h *Handler) WithGroup(name string) slog.Handler

type HandlerOptions

type HandlerOptions struct {
	// Level reports the minimum record level that will be logged.
	// The handler discards records with lower levels.
	// If Level is nil, the handler assumes LevelInfo.
	// The handler calls Level.Level for each record processed;
	// to adjust the minimum level dynamically, use a LevelVar.
	Level slog.Leveler

	// Formatter is a config to format the log record before sending.
	Formatter FormatterOptions

	// Sender is a config to send the formatted log record to a telegram chat.
	Sender SenderOptions
}

type Sender

type Sender interface {
	Send(msg string) error
	Close() error
}

func NewSender

func NewSender(opts SenderOptions) Sender

type SenderOptions

type SenderOptions struct {
	// Token is the Telegram bot token to send messages. It is required, please generate it from @BotFather.
	Token string

	// ChatID is the chat ID to send messages to (private, group or channel).
	ChatID int64

	// HTTPClient is the client used to send messages to the Telegram API.
	HTTPClient *http.Client

	// BaseURL is the Telegram API basic url to send messages.
	BaseURL string

	// BatchSize is the maximum number of messages to send in a single batch.
	BatchSize uint64

	// FlushInterval is the maximum duration to wait before sending a batch.
	FlushInterval time.Duration

	// Verbose specifies whether to print the Telegram API requests and responses.
	Verbose bool

	// Instance is a custom sender instance to use.
	Instance Sender
}

type TelebotSender

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

func NewTelebotSender

func NewTelebotSender(client client, chatID int64) *TelebotSender

func (*TelebotSender) Close

func (s *TelebotSender) Close() error

func (*TelebotSender) Send

func (s *TelebotSender) Send(msg string) error

Jump to

Keyboard shortcuts

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