Use the PrismaClient log parameter to configure log levels , including warnings, errors, and information about the queries sent to the database.

Prisma supports two types of logging:

You can also use the DEBUG environment variable to enable debugging output in the Prisma Client. See Debugging for more information.

Log to stdout

The simplest way to print all log levels to stdout is to pass in an array LogLevel objects:

const prisma = new PrismaClient({
log: ['query', 'info', 'warn', 'error'],
})

This is the short form of passing in an array of LogDefinition objects where the value of emit is always stdout:

const prisma = new PrismaClient({
log: [
{
emit: 'stdout',
level: 'query',
},
{
emit: 'stdout',
level: 'error',
},
{
emit: 'stdout',
level: 'info',
},
{
emit: 'stdout',
level: 'warn',
},
],
})

Event-based logging

To use event-based logging:

  1. Set emit to event for a specific log level, such as query
  2. Use the $on() method to subscribe to the event

The following example subscribes to all query events and write the duration and query to console:

const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'stdout',
level: 'error',
},
{
emit: 'stdout',
level: 'info',
},
{
emit: 'stdout',
level: 'warn',
},
],
})
prisma.$on('query', (e) => {
console.log('Query: ' + e.query)
console.log('Duration: ' + e.duration + 'ms')
})
Hide CLI results
Query: SELECT "public"."User"."id", "public"."User"."email", "public"."User"."name" FROM "public"."User" WHERE 1=1 OFFSET $1
Duration: 3ms
Query: SELECT "public"."Post"."id", "public"."Post"."title", "public"."Post"."authorId" FROM "public"."Post" WHERE "public"."Post"."authorId" IN ($1,$2,$3,$4) OFFSET $5
Duration: 2ms

The exact event (e) type and the properties available depends on the log level.

Edit this page on GitHub