Blog / Guide
Telegram alerts for Python scripts that fail at night
Send logs from Python cron jobs and workers to JustLog3 Cloud, get a Telegram message when a run starts looping, and ask the AI assistant why it failed.
Scripts that run on a timer fail quietly: a cron job that syncs orders, a worker that sends emails, a scraper that ran fine for months. This guide uses the justlog3 package to send their logs to JustLog3 Cloud and to get a Telegram message when a run starts misbehaving.
Install and set the token
pip install justlog3
Copy the API token from your dashboard into an environment variable, JUSTLOG3_TOKEN, so it never ends up in the code or in Git.
Log with levels
import os
import time
from justlog3 import BasicLogger, set_api_token
set_api_token(os.environ['JUSTLOG3_TOKEN'])
logger = BasicLogger('sync.log', console_level='INFO')
def sync_orders():
for attempt in range(1, 6):
try:
count = fetch_and_store_orders() # your code
logger.success(f'synced {count} orders')
return
except ConnectionError as error:
logger.warning(f'attempt {attempt}/5 failed: {error}')
time.sleep(2 ** attempt)
logger.critical('orders sync gave up after 5 attempts')
logger.info('sync started')
sync_orders()
Every line goes to sync.log and to the cloud; console_level only filters what the terminal shows. log() and the level methods never wait for the network: a background thread sends lines every 5 seconds or every 75 lines, and the rest on exit.
If the cloud can't be reached, the lines stay in memory (up to 10,000 per logger) and are resent with the same sequence number, so a retry never stores them twice. The local file is written either way.
One run, one session
Each process gets its own random session id, so every run of the script is a separate session in Logs. You see at a glance which run failed and read only its lines.
Alerts that mean something
Link Telegram from the dashboard: Telegram notifications → Create secure link, then press Start in the bot. From then on:
- When one session grows past another 1,000 lines, you get a message. A normal sync logs a handful of lines; a loop that hammers a dead API logs thousands. The alert comes while it's happening, not the next morning.
- At 80% and 100% of the day's log lines you get a heads-up, so a noisy script can't eat the quota unnoticed.
Ask why it failed
Open AI Assistant, switch on Logs and ask: "Why did the orders sync fail at 03:00?". The assistant reads your recent lines, newest first, with repeats collapsed, and points at the attempt that broke. With GitHub on as well, it can open the file the traceback mentions.
Shutdown matters
Lines are flushed on a normal exit and on SIGTERM (docker stop, systemd). kill -9 or a power cut loses at most the lines still in the buffer. If you end the process with os._exit(), call justlog3.shutdown() first.
Free plan
5,000 log lines a day kept for 2 days and 12 assistant replies a day, with no card. More in Plans and limits and the Python docs.
Try it on your own logs
The Free plan takes 5,000 lines a day, with Telegram alerts and the AI assistant included. No card.
Start free