Configuring logging
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:
- Logging to stdout (default)
- Event-based logging (use
$on()
method to subscribe to events)
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 short form of passing in an array of LogDefinition
objects where the value of emit
is always stdout
:
const prisma = new PrismaClient({log: [{emit: 'event',level: 'query',},{emit: 'stdout',level: 'error',},{emit: 'stdout',level: 'info',},{emit: 'stdout',level: 'warn',},],})
Event-based logging
To use event-based logging:
- Set
emit
toevent
for a specific log level, such as query - Use the
$on()
method to subscribe to the event
The following example subscribes to all query
events:
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(e)})
The exact event (e
) type and the properties available depends on the log level.