こう。
1import config from '@/libs/config'; 2import { Injectable, Logger, OnModuleInit } from '@nestjs/common'; 3import { Prisma, PrismaClient } from '@prisma/client'; 4 5@Injectable() 6export class PrismaService 7 extends PrismaClient<Prisma.PrismaClientOptions, Prisma.LogLevel> 8 implements OnModuleInit 9{ 10 constructor() { 11 super({ log: ['query', 'info', 'warn', 'error'] }); 12 } 13 14 private readonly logger = new Logger(PrismaService.name); 15 16 async onModuleInit() { 17 if (config().nodeEnv !== 'production') { 18 this.$on('query', (event) => { 19 this.logger.log( 20 `Query: ${event.query}`, 21 `Params: ${event.params}`, 22 `Duration: ${event.duration} ms`, 23 ); 24 }); 25 this.$on('info', (event) => { 26 this.logger.log(`message: ${event.message}`); 27 }); 28 this.$on('error', (event) => { 29 this.logger.log(`error: ${event.message}`); 30 }); 31 this.$on('warn', (event) => { 32 this.logger.log(`warn: ${event.message}`); 33 }); 34 } 35 await this.$connect(); 36 } 37}