UNPKG

@soinlabs/hawk

Version:

Package to better manage errors, logs, and its notifications

134 lines (122 loc) 4.05 kB
const winston = require('winston') const ecsFormat = require('@elastic/ecs-winston-format') const path = require('path') const fs = require('fs') const DailyRotateFile = require('winston-daily-rotate-file') /** * Class FileWriter to write logs in files */ class FileWriter { /** * Give the log winston format and write it to a file * @param {JSON Object} log * @param {String} dir - directory path to store logs * @param {String} maxsize - max size of the file. if it is exeded winston creates new file * @param {String} maxfiles - time to keep files * @param {String} datepattern - datepattern to name files default YYYY-MM-DD */ constructor(referenceCode, dir, maxsize, maxfiles, datepattern) { this._validate(dir) this.rotateTransportError = new DailyRotateFile({ filename: path.join(dir, `${referenceCode}-error-%DATE%.log`), dirname: dir, datePattern: datepattern || 'YYYY-MM-DD', maxSize: maxsize || '20m', maxFiles: maxfiles || '3d', level: 'error', handleExceptions: false, }) this.rotateTransportWarn = new DailyRotateFile({ filename: path.join(dir, `${referenceCode}-warn-%DATE%.log`), dirname: dir, datePattern: datepattern || 'YYYY-MM-DD', maxSize: maxsize || '20m', maxFiles: maxfiles || '3d', level: 'warn', handleExceptions: false, }) this.rotateTransportInfo = new DailyRotateFile({ filename: path.join(dir, `${referenceCode}-info-%DATE%.log`), dirname: dir, datePattern: datepattern || 'YYYY-MM-DD', maxSize: maxsize || '20m', maxFiles: maxfiles || '3d', level: 'info', handleExceptions: false, }) this.rotateTransportHttp = new DailyRotateFile({ filename: path.join(dir, `${referenceCode}-http-%DATE%.log`), dirname: dir, datePattern: datepattern || 'YYYY-MM-DD', maxSize: maxsize || '20m', maxFiles: maxfiles || '3d', level: 'http', handleExceptions: false, }) this.rotateTransportVerbose = new DailyRotateFile({ filename: path.join(dir, `${referenceCode}-verbose-%DATE%.log`), dirname: dir, datePattern: datepattern || 'YYYY-MM-DD', maxSize: maxsize || '20m', maxFiles: maxfiles || '3d', level: 'verbose', handleExceptions: false, }) this.rotateTransportDebug = new DailyRotateFile({ filename: path.join(dir, `${referenceCode}-debug-%DATE%.log`), dirname: dir, datePattern: datepattern || 'YYYY-MM-DD', maxSize: maxsize || '20m', maxFiles: maxfiles || '3d', level: 'debug', handleExceptions: false, }) this.rotateTransportSilly = new DailyRotateFile({ filename: path.join(dir, `${referenceCode}-silly-%DATE%.log`), dirname: dir, datePattern: datepattern || 'YYYY-MM-DD', maxSize: maxsize || '20m', maxFiles: maxfiles || '3d', level: 'silly', handleExceptions: false, }) this.logger = winston.createLogger({ format: ecsFormat(), transports: [ this.rotateTransportError, this.rotateTransportWarn, this.rotateTransportInfo, this.rotateTransportHttp, this.rotateTransportVerbose, this.rotateTransportDebug, this.rotateTransportSilly, new winston.transports.Console({ handleExceptions: true, format: winston.format.combine( winston.format.timestamp(), winston.format.colorize(), winston.format.prettyPrint(), winston.format.simple(), winston.format.printf( info => `[${info.timestamp}] ${info.level}: ${info.message}` ) ), }), ], }) } write(log) { this.logger.log(log) } /** * Validate if directory exists * if it doesnt exist throw an error * @param {String} dir */ _validate(dir) { if (!fs.existsSync(dir)) { throw new Error('Directory not found: ' + dir) } } } module.exports = FileWriter