Overview

You can view the generated database queries that Prisma Client sends to your database as well as warnings and other information by configuring different log levels in the PrismaClient constructor. You can do this via the log option:

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

This logs all log levels:

  • query: Logs the SQL queries that are generated by a Prisma Client API call
  • info: Logs general information
  • warn: Logs warnings

The log option

Type

The log options is optional and has the following type:

log?: Array<LogLevel | LogDefinition>

If the log option is not provided, nothing will be logged.

Here's the type definitions for LogLevel and LogDefinition:

type LogLevel = 'info' | 'query' | 'warn';
type LogDefinition = {
level: LogLevel;
emit: 'stdout' | 'event';
};

By default, logs are printed to stdout so you can directly observe them. Alternatively, you can also configure event-based logging and observe the logs by providing a callback to the on() function.

Logging to stdout

If you want to print your logs to stdout, you can provide the LogLevel values directly to the log option:

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

Since stdout is the default, the above code snippet is equivalent to the following one where you're explicitly passing stdout to the emit option:

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

Event-based logging

If you want to apply some custom logic to your logs, you can also set emit to event:

const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'query',
},
{
emit: 'event',
level: 'info',
},
{
emit: 'event',
level: 'warn',
},
],
});
// ... must now call prisma.on(...) in order to handle the logging events

If you do this, you need to call the .on function on your PrismaClient instance and provide a callback function which then receives the logging event. Depending on the selected log level, the input event will carry different arguments.

Logging query events

Here is a sample snippet that shows how to log an incoming event e (for the log level query):

const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'query',
},
],
});
prisma.on('query', e => {
e.timestamp;
e.query;
e.params;
e.duration;
e.target;
console.log(e);
});

The incoming event e is of type QueryEvent which is defined as follows:

export type QueryEvent = {
timestamp: Date;
query: string;
params: string;
duration: number;
target: string;
};

Note that query contains the SQL query and params contains any query parameters for the SQL query.

With the above configuration, assume you're sending the following query with Prisma Client:

const allPosts = await prisma.post.findMany({
where: { published: true },
include: {
author: true,
},
})

This API call generates two SQL queries that are being sent as events to the callback configured via on(). Here's a sample for what these events might look like:

{
timestamp: 2020-03-10T08:36:57.430Z,
query: 'SELECT `dev`.`Post`.`id`, `dev`.`Post`.`title`, ' +
'`dev`.`Post`.`content`, `dev`.`Post`.`published`, ' +
'`dev`.`Post`.`author` FROM `dev`.`Post` WHERE ' +
'`dev`.`Post`.`published` = ? LIMIT ? OFFSET ?',
params: '[true,-1,0]',
duration: 0,
target: 'quaint::connector::metrics'
}
{
timestamp: 2020-03-10T08:36:57.430Z,
query: 'SELECT `dev`.`User`.`id`, `dev`.`User`.`email`, `dev`.`User`.`name` ' +
'FROM `dev`.`User` WHERE `dev`.`User`.`id` IN (?,?) LIMIT ? OFFSET ?',
params: '[3,2,-1,0]',
duration: 0,
target: 'quaint::connector::metrics'
}

Logging info events

Here is a sample snippet that shows how to log an incoming event e (for the log level info):

const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'info',
},
],
});
prisma.on('info', e => {
e.timestamp;
e.message;
e.target;
console.log(e);
});

The incoming event e is of type LogEvent which is defined as follows:

export type LogEvent = {
timestamp: Date;
message: string;
target: string;
};

Logging warn events

Here is a sample snippet that shows how to log an incoming event e (for the log level warn):

const prisma = new PrismaClient({
log: [
{
emit: 'event',
level: 'warn',
},
],
});
prisma.on('warn', e => {
e.timestamp;
e.message;
e.target;
console.log(e);
});

The incoming event e is of type LogEvent which is defined as follows:

export type LogEvent = {
timestamp: Date;
message: string;
target: string;
};
Edit this page on Github