Guides / Monitoring
// app/api/health/route.ts
import { db } from "@/lib/db";
export async function GET() {
try {
await db.query("SELECT 1");
return Response.json({ status: "ok", timestamp: Date.now() });
} catch {
return Response.json({ status: "error" }, { status: 503 });
}
}
Use a cron service (Vercel Cron, Trigger.dev, or a simple external pinger) to hit your health endpoint every minute and log failures:
// cron/health-check.ts (runs every minute)
import { logit } from "@/lib/logit";
const HEALTH_URL = "https://yourapp.com/api/health";
const TIMEOUT_MS = 10000;
export async function checkHealth() {
const start = Date.now();
try {
const res = await fetch(HEALTH_URL, { signal: AbortSignal.timeout(TIMEOUT_MS) });
const duration = Date.now() - start;
if (!res.ok) {
await logit.now("uptime", {
event: "Health check failed",
description: `Status ${res.status} โ app may be down`,
icon: "๐จ",
notify: true,
metadata: { status: res.status, durationMs: duration, url: HEALTH_URL },
});
}
} catch (error) {
await logit.now("uptime", {
event: "App unreachable",
description: error instanceof Error ? error.message : "Timeout or network error",
icon: "๐ด",
notify: true,
metadata: {
error: error instanceof Error ? error.message : String(error),
url: HEALTH_URL,
durationMs: Date.now() - start,
},
});
}
}
// Track when the app comes back up after a failure
await logit.now("uptime", {
event: "App recovered",
description: "Health check passing again",
icon: "โ
",
notify: true,
metadata: { durationMs: Date.now() - start },
});
// vercel.json
{
"crons": [
{
"path": "/api/cron/health-check",
"schedule": "* * * * *"
}
]
}
Tip: If you log when the app goes down AND when it comes back up, you can calculate downtime duration from the LogIt event timestamps.
Try LogIt free
7-day trial. No credit card required.