Guides / Platforms & Tools

How to Track Events in Bun + Hono Apps

3 min readยทPlatforms & Tools

Setup

bash
bun add logit hono
ts
// lib/logit.ts
import { init } from "logit";
export const logit = init({ token: process.env.LOGIT_API_KEY! });

Basic Hono app with event tracking

ts
// index.ts
import { Hono } from "hono";
import { logit } from "./lib/logit";

const app = new Hono();

app.post("/checkout", async (c) => {
  const body = await c.req.json<{ email: string; plan: string }>();

  await logit.now("payments", {
    event: "Checkout started",
    description: `${body.email} โ€” plan: ${body.plan}`,
    icon: "๐Ÿ’ณ",
    notify: true,
    tags: { plan: body.plan },
    metadata: { email: body.email, plan: body.plan },
  });

  return c.json({ ok: true });
});

app.onError(async (err, c) => {
  await logit.now("errors", {
    event: "Hono error",
    description: err.message,
    icon: "โŒ",
    notify: true,
    metadata: { path: c.req.path, method: c.req.method },
  });
  return c.json({ error: "Internal error" }, 500);
});

export default {
  port: 3000,
  fetch: app.fetch,
};

Response time middleware

ts
import type { MiddlewareHandler } from "hono";

export const responseTime: MiddlewareHandler = async (c, next) => {
  const start = Date.now();
  await next();
  const duration = Date.now() - start;
  c.header("X-Response-Time", `${duration}ms`);

  if (duration > 2000) {
    await logit.now("perf", {
      event: "Slow Hono response",
      description: `${c.req.method} ${c.req.path} โ€” ${duration}ms`,
      icon: "๐Ÿข",
      notify: false,
      metadata: { path: c.req.path, durationMs: duration },
    });
  }
};

app.use("*", responseTime);

Bun cron with Logit

ts
// Using Bun.cron (Bun v1.1+)
Bun.cron("0 * * * *", async () => {
  await logit.now("cron", {
    event: "Hourly job started",
    description: "Running scheduled cleanup",
    icon: "โฐ",
    notify: false,
    metadata: { runtime: "bun", version: Bun.version },
  });
  await runCleanup();
});

Try LogIt free

7-day trial. No credit card required.

Start free