Skip to content

cheatsnake/gtl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go Telegram logger

A tiny Go module for sending logs to Telegram chats.

  • 🚀 Zero dependencies
  • 👶🏻 The simplest API
  • ⚙️ With useful settings

Install

go get -u github.com/cheatsnake/gtl

Usage

The minimum set of parameters for the logger requires only a Telegram bot token and the chat ID where the logs will be sent.

import "github.com/cheatsnake/gtl"
logger, err := gtl.New("PASTE_BOT_TOKEN", gtl.Options{
	ChatID: "PASTE_CHAT_ID",
},)

Now you can easily use the logger to send different messages:

logger.Ok("The data has been successfully saved to the database")
logger.Info("The server will be restarted in 10 minutes")
logger.Warn("High CPU usage was detected")
logger.Err("Failed to connect to a third-party server")
logger.Raw("Hello World!")

Basic usage

Not only strings can be used as data for logs. For example, if you pass a struct, it will be automatically converted into a JSON string.

logger.Info(struct {
	Uptime      int64  `json:"uptime"`
	Status      string `json:"status"`
	LastUpdated string `json:"lastUpdated"`
}{
	Uptime:      3600,
	Status:      "Running",
	LastUpdated: "2024-11-19T12:00:00Z",
})

Struct to JSON auto convertion

Settings

Additionally, you can change some settings to customize your logs. Below are the default settings:

logger, err := gtl.New("bot_token", gtl.Options{
    ChatID:              "chat_id",
    ThreadID:            0,
    ParseMode:           "",
    WrapAsCode:          false,
    CodeExtension:       "",
    TimeMetadata:        false,
    DisableNotification: false,
}
  • ThreadID - The thread (message thread) ID within the chat for sending log messages. Can be used only for forum-style chats.
  • ParseMode - The formatting mode for messages (Markdown, HTML, MarkdownV2). An empty string disables this option.
  • WrapAsCode - If true, the log message will be wrapped in a code block (e.g., using Markdown or HTML preformatted tags).
  • CodeExtension - Specifies the language extension for syntax highlighting in code blocks, if supported (e.g., json, go, js, ts, py, xml).
  • TimeMetadata - If true, appends timestamp metadata to log messages for better traceability.
  • DisableNotification - If true, messages will be sent silently without triggering notifications in the chat.

In addition to setup the Option struct when the Logger is initialised, you can define them individually for each call to one of the methods by passing the Option struct as the second argument:

logger.Raw("Hello World!", gtl.Options{
    ParseMode:    "Markdown",
    WrapAsCode:   true,
    TimeMetadata: true,
})

Fields that have not been filled with any value will be filled with default values depending on their type. Except ChatID and ThreadID - they will be overridden only if non-empty values are passed.

As an example, the included parameters ParseMode, WrapAsCode and TimeMetadata together produce this result:

Wrap as code example

Licence

Released under the MIT License.