Query debugging
Inspect SQL generated by Prisma during development.
Prisma · Module
$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.
$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.
prisma.$on(event, callback)| Name | Type | Description |
|---|---|---|
event | Prisma.LogEvent | The event name to subscribe to, such as query. |
callback | (event) => void | A callback invoked with the matching log event. |
Registers a listener for the selected Prisma Client log event.
query property.params property.duration property.$on() can receive callbacks.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.
Inspect SQL generated by Prisma during development.
Capture query duration and parameters for application telemetry.
Route Prisma log events into custom logging or alerting code.
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.
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().
$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.
PrismaClientemitlevelqueryevent-based loggingstdout