All files / emp_audit_system/utils logger.js

85.71% Statements 6/7
100% Branches 2/2
50% Functions 1/2
85.71% Lines 6/7

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  6x 6x   6x                             3x                                                 6x       6x
// utils/logger.js
const { createLogger, format, transports } = require('winston');
const path = require('path');
 
const logger = createLogger({
  level: 'info',
  format: format.combine(
    format.timestamp({ format: 'YYYY-MM-DD HH:mm:ss' }),
    format.errors({ stack: true }),
    format.splat(),
    format.json()
  ),
  transports: [
    // Console logging
    new transports.Console({
      format: format.combine(
        format.colorize(),
        format.printf(
          ({ level, message, timestamp, stack }) => 
            `${timestamp} ${level}: ${stack || message}`
        )
      )
    }),
    // File logging (errors only)
    new transports.File({
      filename: path.join(__dirname, '../logs/error.log'),
      level: 'error',
      format: format.combine(
        format.timestamp(),
        format.json()
      )
    }),
    // File logging (all levels)
    new transports.File({
      filename: path.join(__dirname, '../logs/combined.log'),
      format: format.combine(
        format.timestamp(),
        format.json()
      )
    })
  ]
});
 
// Add stream for morgan HTTP logging
logger.stream = {
  write: (message) => logger.info(message.trim())
};
 
module.exports = logger;