Log your model queries in a simple and customizable way.
npm i prisma-extension-log
// custom options type
type Options = {
includeResult?: boolean;
};
const prisma = new PrismaClient().$extends(
extension({
log: (data: LogData, options?: Options) => {
const includeResult = options?.includeResult ?? false;
const logData = { ...data };
if (!includeResult) {
delete logData.result;
}
if (logData.error) {
console.error(logData);
} else {
console.log(logData);
}
},
})
);
await prisma.user.findFirst();
await prisma.user.count({
log: false, // disable logging
});
await prisma.user.findMany({
log: {
includeResult: true, // use your options with type safety
},
});
type LogData = {
/**
* @example User
*/
model: string;
/**
* @example findMany
*/
operation: string;
args: unknown;
/**
* Milliseconds with some precision, using
* performance.now()
*/
time: Milliseconds;
result?: unknown;
error?: unknown;
}