Yo, let’s dive into this cool tool I’ve been hacking on: telegram-logger. Imagine this: You’re chilling, coding away, and you need to keep an eye on your app’s logs, but you don’t want to keep flicking back to a boring terminal window. That’s where telegram-logger jumps in! This badass server receives log entries via HTTP POST requests and then, like a ninja, sneaks them over to a Telegram bot. It’s like getting your app to text you its diary. 🤖
What’s super slick about this is how it handles logs. These aren’t just bland text strings; we’re talking about JSON objects packed with all sorts of log data and metadata. You get the full picture of what’s going down with your app, right in your Telegram chat. It’s like your app sliding into your DMs with all the juicy gossip!
Why You Need telegram-logger: Key Benefits
Alright, so why should you give a damn about telegram-logger? First off, it’s like having a spy in your app, feeding you intel directly. No more alt-tabbing to check logs; you get them live and direct in Telegram.
It’s all about convenience and speed. You’re in a meeting, or maybe grabbing a coffee, and BAM! Your app’s logs are right there on your phone. Plus, since it uses JSON, the logs are structured, making them easy to read and understand – no more cryptic log messages that look like they’re written in ancient hieroglyphs.
The real kicker? This tool is customizable as hell. You set it up to match exactly what you and your app need. It’s like having a bespoke suit; it just fits right.
Under the Hood: Exploring the Tech Stack
Now, let’s pop the hood and see what makes telegram-logger tick. This isn’t some clunky, slapped-together script. We’re riding on GoLang, the language that’s as fast as it is efficient. GoLang means this tool is like a sports car: sleek, fast, and reliable.
Dependencies? We’ve got ’em, but only the ones we need. The codebase is clean and well-organized, like a well-oiled machine. Everything from HTTP server setup to logging and Telegram bot communication is crafted with care, ensuring that telegram-logger isn’t just powerful, but also a breeze to work with.
And let’s not forget about the configuration – it’s all YAML, baby! You’ve got control over everything: log levels, formats, Telegram bot settings, even the storage backend. It’s like having the remote control to your app’s logging, and every button does something awesome.
Getting Started: Prerequisites and Initial Setup
Alright, let’s get down to business and set up telegram-logger. Here’s the lowdown on what you need to kick things off:
- A Machine That Can Run Docker (Optional): If you’re into Docker, great! You can run telegram-logger in a Docker container. But hey, if Docker’s not your jam, no sweat – it’s not a must.
- Telegram Account: You’ll need this to get those log messages. No account, no fun. It’s easy to set up, so if you haven’t already, now’s the time.
- Configuration File Savvy: telegram-logger uses a YAML file for configuration. You don’t need to be a YAML wizard, but knowing your way around it helps. You’ll be setting up things like the server address, log levels, and your Telegram bot info here.
- Basic Understanding of HTTP and JSON: Since telegram-logger works with HTTP POST requests and JSON-formatted log entries, a basic grasp of these will make your life easier.
- Environment for Running the Binary (If Not Using Docker): If you’re going old school and not using Docker, you’ll need an environment where you can run the telegram-logger binary. This means having the necessary permissions and a suitable OS.
And that’s pretty much it! Not too crazy, right? With these in your toolkit, you’re ready to roll into the actual setup. Stay tuned for the step-by-step guide on getting telegram-logger up and running! 🚀
Installing and Running
Whether you’re a Docker fan or prefer running binaries directly, I’ve got you covered:
Running via Docker
Run with Docker Command:
Use the following command to start telegram-logger with Docker:
docker run -it \
--env CONFIGFILE=/app/config.yml \
--env LOGGER_LEVEL=debug \
--env LOGGER_FORMAT=json \
--env TELEGRAMBOT_TOKEN=TOKEN \
--env TELEGRAMBOT_SUPERUSERCHATID=CHATID \
--env STORAGE_TYPE=badgerDB \
--env STORAGE_BADGERDB_DSN=/app/db \
--mount type=bind,src=$(pwd)/config.yml,dst=/app/config.yml,readonly \
--mount type=bind,src=$(pwd)/db,dst=/app/db \
-p 0.0.0.0:8080:80 \
psyb0t/telegram-logger
Run with Docker Compose:
Alternatively, use docker-compose
with the provided docker-compose.yml
file:
version: "3.8"
services:
server:
image: psyb0t/telegram-logger
ports:
- 8080:80
volumes:
- ./config.yml:/app/config.yml:ro
- ./db:/app/db
environment:
- CONFIGFILE=/app/config.yml
- LOGGER_LEVEL=debug
- LOGGER_FORMAT=json
- TELEGRAMBOT_TOKEN=TOKEN
- TELEGRAMBOT_SUPERUSERCHATID=CHATID
- STORAGE_TYPE=badgerDB
- STORAGE_BADGERDB_DSN=/app/db
Then run:
docker compose -f docker-compose.yml up
Running via Latest Released Binary
Download the Latest Release:
Use the download-latest.sh
script to download the latest binary:
#!/usr/bin/env bash
owner=psyb0t
repo=telegram-logger
asset_name=telegram-logger-linux-amd64
echo "Looking up the latest release of $asset_name for github.com/$owner/$repo..."
releases=$(curl -s https://api.github.com/repos/$owner/$repo/releases)
latest_release=$(echo "$releases" | jq -r '.[0]')
asset_url=$(echo "$latest_release" | jq -r ".assets[] | select(.name == \"$asset_name\") | .browser_download_url")
echo "Downloading $asset_url..."
curl -s -L -o "$asset_name" "$asset_url"
chmod +x "$asset_name"
Run the Binary:
Set the necessary environment variables and run the binary using run.sh
:
export LISTENADDRESS=0.0.0.0:8080
export LOGGER_LEVEL=debug
export LOGGER_FORMAT=json
export TELEGRAMBOT_TOKEN=TOKEN
export TELEGRAMBOT_SUPERUSERCHATID=CHATID
export STORAGE_TYPE=badgerDB
export STORAGE_BADGERDB_DSN=./db
./telegram-logger-linux-amd64
And there you have it! Whether you’re a Docker enthusiast or prefer to run binaries directly, these steps should get telegram-logger up and running in no time. Remember, replace the placeholder tokens and chat IDs with your actual Telegram bot details.
Configuration and Customization: Making telegram-logger Your Own
So, you’ve got telegram-logger installed, and now it’s time to tweak it to fit like a glove. Let’s dive into how you can customize this bad boy to your liking. It’s all about the YAML configuration file and environment variables. Here we go:
YAML Configuration File
Your main tool for configuration is a YAML file. It’s where you set up everything telegram-logger needs to know. Check out these key sections:
- ListenAddress: Where should telegram-logger listen for incoming log messages? Set it with
ListenAddress
. By default, it’s set to0.0.0.0:80
. - Logger Settings: Get your logs just how you like them.
- Level: Set the log level (
debug
,info
,warning
,error
). - Format: Choose between
text
orjson
formats.
- Level: Set the log level (
- TelegramBot Config: Hook up your Telegram bot.
- Token: Your bot’s token goes here.
- SuperuserChatID: The chat ID for your superuser.
- Storage: Decide how to store your logs.
- Type: Choose your storage backend (like
badgerDB
). - BadgerDB: If you use BadgerDB, set up the DSN here.
- Type: Choose your storage backend (like
Here’s a sample to get your gears turning:
listenAddress: 0.0.0.0:8080
logger:
level: debug
format: json
telegramBot:
token: YOUR_BOT_TOKEN
superuserChatID: YOUR_CHAT_ID
storage:
type: badgerDB
badgerDB:
dsn: /path/to/db/dir
Environment Variables
You can also use environment variables, and they’ll override the YAML settings. It’s super handy for quick changes or keeping secrets out of config files. Here’s the rundown:
LISTENADDRESS
: OverrideslistenAddress
.LOGGER_LEVEL
: Sets the log level.LOGGER_FORMAT
: Chooses the log format.TELEGRAMBOT_TOKEN
: Your Telegram bot token.TELEGRAMBOT_SUPERUSERCHATID
: The superuser’s chat ID.STORAGE_TYPE
: The type of storage backend.STORAGE_BADGERDB_DSN
: The DSN for BadgerDB.
Putting It All Together
Whether you’re using Docker, docker-compose, or running the binary directly, these configs are your control panel. Set them up in the YAML file, or pass them as environment variables, and you’re golden.
That’s pretty much it! With these settings in hand, you can make telegram-logger behave exactly how you want. It’s like having a custom-tailored suit; it just fits right. Now go ahead, play around with it, and make it your own! 🎛️💻 ​​
How to Create a Telegram Bot: Laying the Foundation
Creating a Telegram bot is straightforward and essential for getting telegram-logger up and running. Here’s how you do it:
- Start a Chat with BotFather: BotFather is Telegram’s official bot for creating and managing other bots. Start by searching for ‘@BotFather’ in the Telegram app and start a chat.
- Create a New Bot: Send the
/newbot
command to BotFather. It will guide you through the process. You’ll need to choose a name and a username for your bot. The username must end in ‘bot’, like ‘logger_bot’. - Store Your Bot’s Token: After the setup, BotFather will give you a token. This token is super important – it’s like the secret password between your telegram-logger and your bot. Keep it safe and don’t share it!
- Configure Your Bot (Optional): You can further customize your bot by setting a profile picture, description, and more via BotFather commands. This step is optional but helps to personalize your bot.
- Test Your Bot: Search for your bot’s username in Telegram and start a chat. Send a message to see if it’s responsive. Initially, it won’t do much until you integrate it with telegram-logger.
- Integrate with Telegram-Logger: You’ll use the token you got from BotFather to configure telegram-logger. This is how telegram-logger knows which bot to send logs to.
- Find Your Chat ID (Optional): If you need your Telegram Chat ID for configuring telegram-logger, you can message ‘@userinfobot’ on Telegram, and it will give you your Chat ID.
And that’s it! Creating a Telegram bot is like setting up a mailbox for your app’s logs. Once you have your bot, you can move on to integrating it with telegram-logger and start receiving logs right in your Telegram chat. It’s like getting texts from your app! 🤖💬 ​​
Integrating with Telegram: Connecting the Dots
Great, let’s weave together the final piece of the puzzle by integrating telegram-logger with your Telegram bot. This integration is what turns your Telegram into a real-time log alert system. Here’s how to get it all connected and test the setup:
Update Configuration:
Open your config.yml
file for telegram-logger and make sure the Telegram bot details are correctly entered.
telegramBot:
token: YOUR_BOT_TOKEN
superuserChatID: YOUR_CHAT_ID
This links telegram-logger to your Telegram bot.
Restart telegram-logger:
To apply these new settings, restart telegram-logger. If you’re using Docker, this means restarting your container.
Test the Integration:
Now, let’s test if the integration works. You can send a test log message to telegram-logger using curl
:
curl -X POST -H "Content-Type: application/json" -H "X-ID: abcdef" -d '{
"caller": "myService",
"time": "2022-03-11T12:34:56.789Z",
"level": "warning",
"message": "Something went wrong!",
"error": "Error: XYZ",
"requestID": "abc123",
"traceID": "def456",
"spanID": "ghi789",
"data": {
"someKey": "someValue",
"anotherKey": 123
}
}' http://localhost:8080/
Modify the URL in the command to match where your telegram-logger is running. This command simulates sending a log message.
Check Your Telegram Chat:
After running the curl
command, head over to your Telegram chat. If all is set up correctly, the log message sent via curl
should appear there.
Troubleshoot if Needed:
If the log message doesn’t show up in Telegram, revisit your telegram-logger configuration. Ensure that it’s running correctly and the address in the curl
command matches its listening address.
With these steps, you should have successfully integrated telegram-logger with your Telegram bot. This setup not only gives you a direct line to your app’s logs but also revolutionizes how you monitor and react to your application’s behavior. It’s like having a chat with your app, keeping you informed every step of the way! 🛠️💬🚀
Usage Scenarios and Tips: Maximizing telegram-logger’s Potential
Real-Time Monitoring of Critical Systems
Usage Scenario: Use telegram-logger to monitor critical systems or services. For instance, if you have a web server, set up telegram-logger to forward logs related to server errors, downtime, or security breaches directly to your Telegram.
Tip: Configure telegram-logger to filter and forward only high-priority logs (like errors or warnings), so you don’t get overwhelmed by routine information.
Debugging During Development
Usage Scenario: While developing an application, configure telegram-logger to send logs to your Telegram for real-time debugging. This way, you can instantly see how code changes affect the app’s behavior.
Tip: Set the logger level to debug
or info
during development to get detailed logs for comprehensive insights.
Alerts for Unusual Activity
Usage Scenario: Set up telegram-logger to alert you of unusual or suspicious activity within your application. This could include multiple failed login attempts, unexpected data changes, or API errors.
Tip: Integrate telegram-logger with your application’s security monitoring tools to enhance your cybersecurity posture.
Remote Diagnostics
Usage Scenario: If you manage remote systems or IoT devices, use telegram-logger to receive logs from these devices. This can help in diagnosing issues without needing physical access to the device.
Tip: Ensure that your remote devices are configured to send critical logs over secure channels to maintain data privacy and security.
Collaborative Troubleshooting
Usage Scenario: Share your bot with team members to collaborate on troubleshooting. When everyone receives the same log data, it’s easier to discuss and solve problems quickly.
Tip: Use the superuser feature to control who can access the log data and maintain operational security.
Performance Monitoring
Usage Scenario: Use telegram-logger to monitor the performance of your applications. For example, set up alerts for long response times or high resource usage.
Tip: Combine log data with performance metrics for a comprehensive view of your application’s health.
Customer Support and Feedback
Usage Scenario: Track logs related to user-reported issues. When a user encounters a problem, you can quickly check logs sent to your Telegram for immediate insights.
Tip: Keep user privacy in mind and ensure sensitive data is not logged or is appropriately anonymized.
These scenarios demonstrate the versatility and utility of telegram-logger. By customizing it to suit your specific needs, you can turn it into a powerful tool for maintaining, monitoring, and improving your applications and systems. Remember, the key to maximizing telegram-logger’s potential lies in thoughtful configuration and integration with your existing tools and workflows. 🚀💡 ​​
External Links
As always I’ll leave you with the link to the Github repository: