winston-prisma-transporter
Prisma transport for winston@3.x logger
Installation
base dependencies:
npm install --save winston-prisma-transporter
pnpm add winston-prisma-transporter
peer dependencies:
npm install --save winston @prisma/client
pnpm add winston @prisma/client
Usage
Create models:
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
model UserLog {
id Int @id @default(autoincrement()) @db.UnsignedInt
timestamp DateTime @default(now()) @db.Timestamp(0)
level String @db.Text
message String @db.Text
meta Json?
}
Run your prisma migrations and you're ready to go!
Setup with default configs:
import { createLogger, format } from "winston";
import { PrismaClient } from "@prisma/client";
import { PrismaWinstonTransporter } from "winston-prisma-transporter";
const prisma = new PrismaClient();
const logger = winston.createLogger({
transports: [
format: format.json(),
new PrismaWinstonTransporter({
level: "http",
prisma
}),
],
});
Customizations
Out of the box, the logger will log all levels to the Table called UserLog
. That can be changed to a different table by passing in the custom table name.
import { createLogger, format } from "winston";
import { PrismaClient } from "@prisma/client";
import { PrismaWinstonTransporter } from "winston-prisma-transporter";
const prisma = new PrismaClient();
const logger = winston.createLogger({
transports: [
format: format.json(),
new PrismaWinstonTransporter({
level: "http",
prisma,
tableName: "CustomTableName"
}),
],
});
Additionally, you can even pass in a custom log function, where prisma is passed in. This allows completely overriding the default behavior.
import { createLogger, format } from "winston";
import { PrismaClient } from "@prisma/client";
import { PrismaWinstonTransporter } from "winston-prisma-transporter";
const prisma = new PrismaClient();
const logger = winston.createLogger({
transports: [
format: format.json(),
new PrismaWinstonTransporter({
level: "http",
prisma,
log: (info, callback) => {
}
}),
],
});
All options avaliable while initiating the transport:
export interface PrismaTransporterOptions extends TransportStreamOptions {
prisma: PrismaClient;
tableName?: string;
log?: (
info: ILogInfo,
callback?: (error?: Error, value?: unknown) => void
) => void;
}
ToDos: