Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Prisma · Module

$on

$on() subscribes to Prisma Client's event-based log notifications. Configure a log level with emit: "event", then register a callback for events such as query.

How it works

$on() registers a callback for Prisma Client log events. First configure the selected log level with emit: "event"; then subscribe by naming the event, such as query, and inspect the event object delivered to the callback.

Syntax

prisma.$on(event, callback)

Parameters

NameTypeDescription
eventPrisma.LogEventThe event name to subscribe to, such as query.
callback(event) => voidA callback invoked with the matching log event.

Returns

Registers a listener for the selected Prisma Client log event.

Key points

  • Prisma Client provides stdout logging and event-based logging.
  • A query subscription receives every emitted query event.
  • The query event exposes the executed query text through its query property.
  • The event object includes query parameters through its params property.
  • The event exposes execution time through its duration property.

Common mistakes

  • The relevant log level must be configured with event emission before $on() can receive callbacks.

Example: Subscribe to query events

import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient({
  log: [{ emit: "event", level: "query" }],
});

prisma.$on("query", (event) => {
  console.log(event.query);
  console.log(event.params);
  console.log(`${event.duration}ms`);
});

Enables query events and prints the SQL, parameters, and duration for each emitted query.

When to use it

Query debugging

Inspect SQL generated by Prisma during development.

Performance monitoring

Capture query duration and parameters for application telemetry.

Custom observability

Route Prisma log events into custom logging or alerting code.

Interview questions

QHow do you subscribe to Prisma query events?

Configure the desired log level with emit: "event" in the Prisma Client constructor, then call $on() with the event name and a callback. For query logging, the callback receives query details such as the SQL text, parameters, and duration.

Follow-up: What happens if the level is configured with emit: "stdout" instead? The event callback is not the configured delivery mechanism; use event emission for $on() subscriptions.

QWhat information is available in a query-event callback?

Use prisma.$on("query", callback) after enabling query events in the client configuration. The callback receives an event object that can be used to log query, params, and duration.

Follow-up: How would you monitor another supported log category? Configure that category with emit: "event", then pass its event name to $on().

QWhat is the purpose of $on()?

$on() is Prisma Client's event-based logging subscription method. It lets application code react to emitted log events instead of relying only on Prisma's default stdout logging.

Follow-up: Why might an application choose event-based logging? It can route structured event details, such as query duration, into custom monitoring or filtering logic.

  • PrismaClient
  • emit
  • level
  • query
  • event-based logging
  • stdout

Source: Logging | Prisma Documentation. Checked 2026-10-16.