tlog is a custom Go logger that wraps the standard logger and forwards log messages to a specified Telegram chat. This allows you to easily receive important log messages via Telegram, making it ideal for remote monitoring of applications.
- Log messages with different severity levels (
INFO,ERROR,FATAL,ALERT). - Forward logs to a Telegram chat.
- Control log levels via Telegram commands.
- Group
INFOandERRORlogs periodically for reduced noise. - Mute notifications temporarily for maintenance or to reduce interruptions.
- Filter out specific words from being logged to Telegram.
- Uses standard
logpackage functions (log.Println,log.Fatal, etc.).
To install the library, use:
go get github.com/stevenlawton/go-telegram-alertHere is an example of how to use tlog in your Go project.
package main
import (
"log"
"github.com/stevenlawton/go-telegram-alert"
)
func main() {
botToken := "YOUR_TELEGRAM_BOT_TOKEN"
chatID := int64(YOUR_CHAT_ID) // Ensure this is an int64
appName := "MyApp"
err := tlog.NewTeleLogger(botToken, chatID, appName)
if err != nil {
log.Fatalf("Failed to create TeleLogger: %v", err)
}
// Now use log functions as usual
log.Println("INFO: This is an informational message.")
log.Println("ERROR: This is an error message.")
log.Println("ALERT: This is an alert message.")
log.Fatal("FATAL: This is a fatal error message.")
}You can control the verbosity of the logs sent to Telegram by using the following commands in your Telegram chat:
/set_log_level_info- Sets the log level toINFO(default)./set_log_level_error- Sets the log level toERROR(onlyERRORandFATALlogs will be sent)./set_log_level_fatal- Sets the log level toFATAL(onlyFATALlogs will be sent)./set_log_level_alert- Sets the log level toALERT(onlyALERTlogs will be sent).
/mute_notifications <duration>- Mutes all log notifications for a specified duration. Example:/mute_notifications 10m./filter_word <word>- Filters out logs containing a specific word. Example:/filter_word debug.
Initializes the Telegram logger and sets it as the default logger. You must provide your bot's token, the chat ID where you want the logs to be sent, and the app name to identify your application in the logs.
After initializing the logger with NewTeleLogger, use the standard log functions (log.Println, log.Printf, log.Fatal, etc.) to generate log messages. To ensure the log messages are categorized properly:
- Informational Messages: Use
log.Println("INFO: message")for general information. - Error Messages: Use
log.Println("ERROR: message")to indicate an error. - Alert Messages: Use
log.Println("ALERT: message")for high-priority alerts. - Fatal Messages: Use
log.Fatal("FATAL: message")to log fatal issues that will stop the program.
These messages will be forwarded to the specified Telegram chat based on the set log level.
The logger allows setting different log levels to filter messages sent to Telegram:
- INFO: All log messages will be sent.
- ERROR: Only
ERRORandFATALlog messages will be sent. - FATAL: Only
FATALlog messages will be sent. - ALERT: Only
ALERTlog messages will be sent.
The default log level is INFO. You can change it by sending commands to the bot.
- Grouped Logs:
INFOandERRORlogs can be grouped and sent in bulk every 5 minutes to reduce noise. This feature ensures you still receive all log messages, but without the constant interruptions.
- Mute Notifications: You can mute notifications using the
/mute_notificationscommand to reduce interruptions during specific time periods. - Filtered Words: Use the
/filter_wordcommand to prevent logs containing specific words from being sent to Telegram, which helps reduce unnecessary messages.
MIT License. See the LICENSE file for more details.
Feel free to open issues or submit pull requests to improve the library. Contributions are welcome!
- go-telegram-bot-api - Used for interacting with the Telegram Bot API.