Search code examples
javascriptnode.jslog4js-node

Java script is not writing the logs into file but showing the logs on console


I am trying to create a logfile where I want to write everything info/error/warn etc.

Below is my code where I can see the logs on console but the script is not writing into a file:

var log4js = require('log4js');
log4js.configure({
  appenders: {
    cleanupLogs: {
      type: 'file',
      filename: 'QuoteCleanup.log'
    },
    console: {
      type: 'console'
    }
  },
  categories: {
    default: {
      appenders: ['console'],
      level: 'trace'
    }
  }
});

var log = log4js.getLogger();


Solution

  • You can use this devtools snippet shown below to create a console.save method. It creates a FileBlob from the input, and then automatically downloads it.

    Edited I just edited my answer try this one and tell if this works for you

    log4js.configure({
      appenders: {
        everything: {
          type: 'file',
          filename: 'QuoteCleanup.log'
        }
      },
      categories: {
        default: {
          appenders: ['everything'],
          level: 'debug'
        }
      }
    });
    
    const logger = log4js.getLogger();
    logger.debug('I will be logged in QuoteCleanup.log');