telelogger

package module
v0.0.0-...-6ce409d Latest Latest
Warning

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

Go to latest
Published: Jan 31, 2025 License: MIT Imports: 4 Imported by: 0

README

telelogger

A lightweight, easy-to-use Telegram logging utility for Go applications.

Features

  • Simple integration with Telegram Bot API
  • Multiple log levels (info, error, success, warn)
  • Customizable message formatters
  • Strong Go type system
  • Zero external dependencies

Installation

go get github.com/monkhai/telelogger-golang

Quick Start

package main

import "github.com/monkhai/telelogger-golang"

func main() {
    logger := telelogger.New(telelogger.Config{
        BotToken: "YOUR_BOT_TOKEN",
        ChatID:   YOUR_CHAT_ID,
    })

    // Basic logging
    logger.LogInfo("Hello, world!")
    logger.LogError("Something went wrong!")
    logger.LogSuccess("Operation completed successfully!")
    logger.LogWarn("Warning: Resource running low")
}

Configuration

The New function accepts a Config struct with the following options:

type Config struct {
    // Your Telegram Bot Token
    BotToken string

    // Target Chat ID where messages will be sent
    ChatID int64

    // The formatting of the message
    // Can be ParseModeHTML, ParseModeMarkdown, or ParseModeMarkdownV2
    ParseMode ParseMode

    // Custom formatter for info messages
    InfoFormatter FormatterFunc

    // Custom formatter for error messages
    ErrorFormatter FormatterFunc

    // Custom formatter for success messages
    SuccessFormatter FormatterFunc

    // Custom formatter for warning messages
    WarnFormatter FormatterFunc
}

// FormatterFunc is a function type for message formatting
type FormatterFunc func(message string) string
Custom Formatters Example

You can customize how messages are formatted before they're sent to Telegram. Notice the <b> tags in the formatters, adding bold text to the titles. This allows you to add more information to the messages, such as links, bold text, etc. For more information on the different parse modes, see the Telegram API documentation.

logger := telelogger.New(telelogger.Config{
    BotToken:  "YOUR_BOT_TOKEN",
    ChatID:    YOUR_CHAT_ID,
    ParseMode: telelogger.ParseModeHTML, // allows us to add <b> tags and more
    InfoFormatter: func(msg string) string {
        return fmt.Sprintf("ℹ️ <b>INFO:</b>\n%s", msg)
    },
    ErrorFormatter: func(msg string) string {
        return fmt.Sprintf("❌ <b>ERROR:</b>\n%s", msg)
    },
    SuccessFormatter: func(msg string) string {
        return fmt.Sprintf("✅ <b>SUCCESS:</b>\n%s", msg)
    },
    WarnFormatter: func(msg string) string {
        return fmt.Sprintf("🚨️ <b>WARNING:</b>\n%s", msg)
    },
})
Error Handling

Unlike the TypeScript version, this package follows Go's error handling patterns:

// All logging methods return an error that you can handle
if err := logger.LogInfo("Hello, world!"); err != nil {
    // Handle error
}

// You can pass either a string or an error to LogError
err := someFunction()
if err != nil {
    logger.LogError(err) // Accepts error interface
}
logger.LogError("Something went wrong") // Also accepts string

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Documentation

Overview

Package telelogger provides advanced logging capabilities for Go applications.

Overview

Telelogger is designed to be a flexible and powerful logging solution that can be easily integrated into any Go application. It provides various features for logging management and configuration.

Basic Usage

import "github.com/yohaiwiener/telelogger"

For more examples and detailed documentation, visit: https://pkg.go.dev/github.com/yohaiwiener/telelogger

Package telelogger provides advanced logging capabilities through Telegram.

This package allows users to implement sophisticated logging with various output formats and destinations through Telegram Bot API.

Index

Constants

View Source
const Version = "0.1.0"

Version represents the current version of the package

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// BotToken is the Telegram Bot Token obtained from BotFather
	BotToken string

	// ChatID is the Telegram Chat ID where messages will be sent
	ChatID int64

	// ParseMode specifies the formatting mode for messages
	// Can be HTML, Markdown, or MarkdownV2
	// If not provided, no formatting will be applied
	ParseMode ParseMode

	// InfoFormatter is a custom formatter for info messages
	// If not provided, uses default format with ℹ️ emoji
	InfoFormatter FormatterFunc

	// ErrorFormatter is a custom formatter for error messages
	// If not provided, uses default format with ❌ emoji
	ErrorFormatter FormatterFunc

	// SuccessFormatter is a custom formatter for success messages
	// If not provided, uses default format with ✅ emoji
	SuccessFormatter FormatterFunc

	// WarnFormatter is a custom formatter for warning messages
	// If not provided, uses default format with 🚨 emoji
	WarnFormatter FormatterFunc
}

Config holds the configuration for the Telelogger instance.

type FormatterFunc

type FormatterFunc func(message string) string

FormatterFunc is a function type for message formatting. It takes a message string and returns a formatted string.

type ParseMode

type ParseMode string

ParseMode represents the available formatting modes for Telegram messages. Can be one of: "HTML", "Markdown", or "MarkdownV2".

const (
	// ParseModeHTML enables HTML-style formatting
	ParseModeHTML ParseMode = "HTML"
	// ParseModeMarkdown enables basic Markdown formatting
	ParseModeMarkdown ParseMode = "Markdown"
	// ParseModeMarkdownV2 enables enhanced Markdown formatting with more features
	ParseModeMarkdownV2 ParseMode = "MarkdownV2"
)

type Telelogger

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

Telelogger is the main struct for sending formatted log messages to Telegram. It provides methods for sending different types of messages (info, error, success, warning) with optional message formatting and custom formatters.

func New

func New(config Config) *Telelogger

New creates a new Telelogger instance with the provided configuration.

Example:

logger := telelogger.New(telelogger.Config{
    BotToken: "your-bot-token",
    ChatID:   123456789,
    ParseMode: telelogger.ParseModeHTML,
})

func (*Telelogger) Log

func (t *Telelogger) Log(msg string) error

Log sends a generic message to Telegram.

Example:

err := logger.Log("Generic message")

func (*Telelogger) LogError

func (t *Telelogger) LogError(err interface{}) error

LogError sends an error message to Telegram. The error parameter can be either an error object or a string.

Example:

err := logger.LogError("Database connection failed")
// or
err := logger.LogError(fmt.Errorf("Database connection failed"))

func (*Telelogger) LogInfo

func (t *Telelogger) LogInfo(msg string) error

LogInfo sends an info message to Telegram.

Example:

err := logger.LogInfo("Application started successfully")

func (*Telelogger) LogSuccess

func (t *Telelogger) LogSuccess(msg string) error

LogSuccess sends a success message to Telegram.

Example:

err := logger.LogSuccess("Backup completed successfully")

func (*Telelogger) LogWarn

func (t *Telelogger) LogWarn(msg string) error

LogWarn sends a warning message to Telegram.

Example:

err := logger.LogWarn("Low disk space")

func (*Telelogger) LogWithParseMode

func (t *Telelogger) LogWithParseMode(msg string, parseMode ParseMode) error

LogWithParseMode sends a generic message to Telegram with a specific parse mode.

Example:

err := logger.LogWithParseMode("Message with <b>bold</b> text", telelogger.ParseModeHTML)

Jump to

Keyboard shortcuts

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