Files
firecrawl/apps/api/src/lib/logger.ts
T

82 lines
2.1 KiB
TypeScript
Raw Normal View History

2024-11-07 20:57:33 +01:00
import * as winston from "winston";
2024-09-04 15:57:57 -03:00
import { configDotenv } from "dotenv";
2024-11-07 20:57:33 +01:00
import Transport from "winston-transport";
2024-09-04 15:57:57 -03:00
configDotenv();
2024-11-07 20:57:33 +01:00
const logFormat = winston.format.printf(info =>
`${info.timestamp} ${info.level} [${info.metadata.module ?? ""}:${info.metadata.method ?? ""}]: ${info.message} ${info.level.includes("error") || info.level.includes("warn") ? JSON.stringify(
info.metadata,
(_, value) => {
if (value instanceof Error) {
return {
...value,
name: value.name,
message: value.message,
stack: value.stack,
cause: value.cause,
}
} else {
return value;
}
2024-07-23 17:30:46 -03:00
}
2024-11-07 20:57:33 +01:00
) : ""}`
)
2024-07-23 17:30:46 -03:00
2024-11-07 20:57:33 +01:00
export const logger = winston.createLogger({
level: process.env.LOGGING_LEVEL?.toLowerCase() ?? "debug",
format: winston.format.json({
replacer(key, value) {
if (value instanceof Error) {
return {
...value,
name: value.name,
message: value.message,
stack: value.stack,
cause: value.cause,
}
} else {
return value;
}
}
}),
transports: [
new winston.transports.Console({
format: winston.format.combine(
winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
winston.format.metadata({ fillExcept: ["message", "level", "timestamp"] }),
...(((process.env.ENV === "production" && process.env.SENTRY_ENVIRONMENT === "dev") || (process.env.ENV !== "production")) ? [winston.format.colorize(), logFormat] : []),
),
}),
],
});
2024-07-23 17:30:46 -03:00
2024-11-07 20:57:33 +01:00
export type ArrayTransportOptions = Transport.TransportStreamOptions & {
array: any[];
scrapeId?: string;
};
2024-07-23 17:30:46 -03:00
2024-11-07 20:57:33 +01:00
export class ArrayTransport extends Transport {
private array: any[];
private scrapeId?: string;
constructor(opts: ArrayTransportOptions) {
super(opts);
this.array = opts.array;
this.scrapeId = opts.scrapeId;
2024-07-23 17:30:46 -03:00
}
2024-11-07 20:57:33 +01:00
log(info, next) {
setImmediate(() => {
this.emit("logged", info);
});
if (this.scrapeId !== undefined && info.scrapeId !== this.scrapeId) {
return next();
}
this.array.push(info);
next();
2024-07-23 17:30:46 -03:00
}
2024-11-07 20:57:33 +01:00
}