All files / src logger.js

64.28% Statements 9/14
25% Branches 2/8
20% Functions 1/5
64.28% Lines 9/14

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 631x 1x 1x         1x 1x       1x                                           1x 1x                                                   1x  
const fs = require('fs');
const path = require('path');
const winston = require('winston');
 
class Logger {
  constructor() {
    // Ensure logs directory exists
    const logsDir = path.join(__dirname, '../logs');
    Iif (!fs.existsSync(logsDir)) {
      fs.mkdirSync(logsDir, { recursive: true });
    }
 
    this.logger = winston.createLogger({
      level: 'info',
      format: winston.format.combine(
        winston.format.timestamp(),
        winston.format.errors({ stack: true }),
        winston.format.json()
      ),
      defaultMeta: { service: 'logvista-agent' },
      transports: [
        // Write all logs with level 'error' and below to error.log
        new winston.transports.File({ 
          filename: path.join(logsDir, 'error.log'), 
          level: 'error' 
        }),
        // Write all logs with level 'info' and below to combined.log
        new winston.transports.File({ 
          filename: path.join(logsDir, 'combined.log') 
        })
      ]
    });
 
    // Add console transport for development
    Eif (process.env.NODE_ENV !== 'production') {
      this.logger.add(new winston.transports.Console({
        format: winston.format.combine(
          winston.format.colorize(),
          winston.format.simple()
        )
      }));
    }
  }
 
  info(message, meta = {}) {
    this.logger.info(message, meta);
  }
 
  error(message, meta = {}) {
    this.logger.error(message, meta);
  }
 
  warn(message, meta = {}) {
    this.logger.warn(message, meta);
  }
 
  debug(message, meta = {}) {
    this.logger.debug(message, meta);
  }
}
 
module.exports = new Logger();