17

my code is:

log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');
logger.setLevel('DEBUG');

logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');

it's creating a file named as 'cheese' but nothing is there inside that. How to write content in that?

1
  • Were you able to figure out this? Commented Mar 30, 2015 at 11:32

4 Answers 4

16

This solution helps u create logs folder and three files error.log,debug.log and info.log in ur folder log with the logs. Worked for me. Thanks.

var log4js = require('log4js'); // include log4js

log4js.configure({ // configure to use all types in different files.
    appenders: [
        {   type: 'file',
            filename: "/logs/error.log", // specify the path where u want logs folder error.log
            category: 'error',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: "file",
            filename: "/logs/info.log", // specify the path where u want logs folder info.log
            category: 'info',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: 'file',
            filename: "/logs/debug.log", // specify the path where u want logs folder debug.log
            category: 'debug',
            maxLogSize: 20480,
            backups: 10
        }
    ]
});

var loggerinfo = log4js.getLogger('info'); // initialize the var to use.
var loggererror = log4js.getLogger('error'); // initialize the var to use.
var loggerdebug = log4js.getLogger('debug'); // initialize the var to use.

loggerinfo.info('This is Information Logger');
loggererror.info('This is Error Logger');
loggerdebug.info('This is Debugger');
Sign up to request clarification or add additional context in comments.

Comments

12

In the above code you are missing logger declaration : Add the line var logger = log4js.getLogger('cheese');

The whole block of code will be as follows:

var log4js = require('log4js');

log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');

var logger = log4js.getLogger('cheese');

logger.setLevel('DEBUG');
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');

Reference

1 Comment

I used log4js v2.0.1 and it return TypeError: log4js.loadAppender is not a function
3

In version 2.x.

You'd need to do something like this:

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

var logger = log4js.getLogger('cheese');

logger.setLevel('DEBUG');
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');

If you want to dynamically create files based on properties in the log message (like filename above), then take a look at the multiFile appender.

2 Comments

TypeError: logger.setLevel is not a function. By removing the logger.setLevel('DEBUG'); line its working fine for me.
@slorenzo your link is dead. Can you update please ?
0

Log4js has a feature for multifile config. See here

log4js.configure({
  appenders: {
    out: { type: 'stdout' },
    afile: { type: 'multiFile', base: 'logs/', property: 'categoryName', extension: '.log' }
  },
  categories: {
    default: { appenders: ['out'], level: 'info' },
    tofile: { appenders: ['afile'], level: 'debug' }
  }
});

Then to start logging in a file logs/xyz.log:

const xyzlog = log4js.getLogger('tofile.xyz');
xyzlog.info('Hello!');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.