Guest User

Untitled

a guest
Dec 4th, 2023
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { LoggerText, LogLevel } from 'crawlee';
  2. import * as winston from 'winston';
  3. import { fileURLToPath } from 'url';
  4. import * as path from 'path';
  5. import * as fs from 'fs';
  6.  
  7. const __filename = fileURLToPath(import.meta.url);
  8.  
  9. const __dirname = path.dirname(__filename);
  10.  
  11. const BASE_PATH = path.dirname(path.dirname(__dirname));
  12.  
  13. interface Error {
  14.     name: string;
  15.     message: string;
  16.     stack?: string;
  17. }
  18. export type AdditionalData = Record<string, any> | null | unknown;
  19.  
  20. //mapping winston's log levels to crawlee's on index
  21. // DONT CHANGE
  22. const winstonLogLevelMapping = [
  23.     'info',
  24.     'error',
  25.     'warn',
  26.     'warn',
  27.     'info',
  28.     'debug',
  29.     'verbose',
  30. ];
  31. export class CrawlerLogger extends LoggerText {
  32.     winstonLogger: winston.Logger;
  33.  
  34.     constructor(options: any) {
  35.         super(options);
  36.         //creating logs folder if it doesnt exist
  37.         fs.mkdirSync(`${BASE_PATH}/logs`, { recursive: true });
  38.         const logger = winston.createLogger({
  39.             level: 'info',
  40.             format: winston.format.combine(
  41.                 winston.format.timestamp(),
  42.                 winston.format.json()
  43.             ),
  44.             transports: [
  45.                 new winston.transports.Console({
  46.                     format: winston.format.colorize({ all: true }),
  47.                 }),
  48.                 new winston.transports.File({
  49.                     level: 'info',
  50.                     dirname: `${BASE_PATH}/logs/`,
  51.                     filename: `output.log`,
  52.                     handleExceptions: true,
  53.                     maxsize: 50000000,
  54.                     maxFiles: 10,
  55.                     tailable: true,
  56.                 }),
  57.             ],
  58.         });
  59.         this.winstonLogger = logger;
  60.     }
  61.  
  62.     override log(
  63.         level: LogLevel,
  64.         message: string,
  65.         exception?: Error,
  66.         data?: AdditionalData
  67.     ) {
  68.         const errStack = exception ? this._parseException(exception) : '';
  69.         const dataStr = data ? ` ${JSON.stringify(data)}` : '';
  70.         const logStr = message + errStack + dataStr;
  71.  
  72.         this.winstonLogger.log(winstonLogLevelMapping[level], logStr);
  73.     }
  74. }
  75.  
Advertisement
Add Comment
Please, Sign In to add comment