diff --git a/.env.default b/.env.default index 43ad37edc..40e84d087 100644 --- a/.env.default +++ b/.env.default @@ -21,10 +21,11 @@ MIGRATE_FROM_OW=false MIGRATION_WITH_LIMITS=true # Logging -LOG_LEVEL=debug +LOG_LEVEL=info LOG_TO_CONSOLE=true +LOG_TO_FILE=true LOG_TO_FOLDER=true -LOG_MAX_FILES=365 +LOG_MAX_FILES=365d # NextAuth NEXTAUTH_URL="http://localhost" diff --git a/docker-compose.base.yml b/docker-compose.base.yml index 48dba40ae..4653fca7d 100644 --- a/docker-compose.base.yml +++ b/docker-compose.base.yml @@ -16,6 +16,7 @@ services: LOG_LEVEL: ${LOG_LEVEL} LOG_TO_CONSOLE: ${LOG_TO_CONSOLE} LOG_TO_FOLDER: ${LOG_TO_FOLDER} + LOG_TO_FILE: ${LOG_TO_FILE} LOG_MAX_FILES: ${LOG_MAX_FILES} NEXTAUTH_URL: ${NEXTAUTH_URL} NEXTAUTH_SECRET: ${NEXTAUTH_SECRET} diff --git a/src/prisma/client.ts b/src/prisma/client.ts index 0c04c1e46..ef665e2ed 100644 --- a/src/prisma/client.ts +++ b/src/prisma/client.ts @@ -1,18 +1,109 @@ -import { PrismaPg } from '@prisma/adapter-pg' +import logger from '@/lib/logger' import { PrismaClient } from '@/prisma-generated-pn-client' +import { PrismaPg } from '@prisma/adapter-pg' // To prevent hot reloading from creating new instances of PrismaClient it is stored in the global object. -// Read more about it in the section "Prevent hot reloading from creating new instances of PrismaClient" here: +// Read more about it in the section 'Prevent hot reloading from creating new instances of PrismaClient' here: // https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/databases-connections +function newPrisma() { + const prisma = new PrismaClient({ + log: [ + { + emit: 'event', + level: 'query', + }, + { + emit: 'event', + level: 'error', + }, + { + emit: 'event', + level: 'info', + }, + { + emit: 'event', + level: 'warn', + }, + ], + adapter: new PrismaPg( + { connectionString: process.env.DB_URI }, + { schema: process.env.DB_SCHEMA }, + ) + }) + + prisma.$on('query', (e) => { + if (e.query.includes('SELECT')) { // Sets select query to debug as it generates an unreasonable amount of logs + logger.log({ + level: 'debug', + message: + `${e.timestamp} + Type: Query + Params: ${e.params} + Query: ${e.query} + Duration: ${e.duration} + Target: ${e.target} + `, + }) + } else { + logger.log({ + level: 'info', + message: + `${e.timestamp} + Type: Query + Params: ${e.params} + Query: ${e.query} + Duration: ${e.duration} + Target: ${e.target} + `, + }) + } + }) + + prisma.$on('error', (e) => { + logger.log({ + level: 'error', + message: + `${e.timestamp} + Type: Error + Message: ${e.message} + Target: ${e.target} + `, + }) + }) + + prisma.$on('info', (e) => { + logger.log({ + level: 'info', + message: + `${e.timestamp} + Type: Info + Message: ${e.message} + Target: ${e.target} + `, + }) + }) + + prisma.$on('warn', (e) => { + logger.log({ + level: 'warn', + message: + `${e.timestamp} + Type: Info + Message: ${e.message} + Target: ${e.target} + `, + }) + }) + + return prisma +} + // This is how the Prisma docs recommend doing it -const globalForPrisma = global as unknown as { prisma: PrismaClient } - -export const prisma = globalForPrisma.prisma || new PrismaClient({ - adapter: new PrismaPg( - { connectionString: process.env.DB_URI }, - { schema: process.env.DB_SCHEMA }, - ) -}) +const globalForPrisma = global as unknown as { + prisma: ReturnType +} + +export const prisma = globalForPrisma.prisma || newPrisma() if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma