balakrishna
balakrishna

Reputation: 299

log4js appenders with different levels of logging levels

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

Upvotes: 5

Views: 5238

Answers (2)

Leo
Leo

Reputation: 409

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

Upvotes: 11

techie_questie
techie_questie

Reputation: 1522

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.

Upvotes: -1

Related Questions