5

I'm trying to create a logger configuration which logs only "ERROR" level information to console and "DEBUG" level information to file appenders. Can anyone help on how to create a logger with above config?

Thanks in advance. Help is very much appreciated

balakrishna
  • 279
  • 2
  • 6
  • 12

2 Answers2

10
var log4js = require('log4js');
log4js.configure({
  appenders: {
    everything: { type: 'stdout' },
    file_log: { type: 'file', filename: 'anyPath/all-logs.log' },
    logLevelFilter: {
      type: 'logLevelFilter',
      level: 'debug',
      appender: 'file_log',
    },
  },
  categories: {
    default: {
      appenders: ['logLevelFilter', 'everything'],
      level: 'all',
    },
  },
});
const logger = log4js.getLogger();

in this example all logs will be shown in the console , but only debug level and above will be add to the anyPath/all-logs.log file

Leo
  • 359
  • 4
  • 6
0

For ERROR in console, you can directly have console.log statements. For DEBUG, you can add that in your config feel like below-

const log4js = require('log4js'); // include log4js
log4js.configure({
  appenders: { app: { type: 'file', filename: 'app.log' } },
  categories: { default: { appenders: ['app'], level: 'debug' } }
});

const logger = log4js.getLogger('app');
logger.debug('Your debug message');

You need to provide the level category inside categories in your config file.

Hope this should help.

techie_questie
  • 1,434
  • 4
  • 29
  • 53