Why is log4js creating an empty file but never putting messages into it?

2.3k Views Asked by At

log4js creates an empty file. I've looked at the answers to the (many) other times this happens to people on SO, but none seem relevant to me. Here is my code:

// Configure logging.
require('log4js').configure('log4js.json');
var logger = require('log4js').getLogger();
logger.info('Starting Engine');

And my log4js.json file is pretty standard:

{
"appenders": [
    {
        "type": "file",
        "filename": "main.log",
        "maxLogSize": 20480,
        "backups": 3,
        "category": "relative-logger"
    }
]
}

Is there any actual documentation for this project? It feels like its a poor cousin to the real log4j, and they are just freeloading off log4j's good reputation.

Edit:

Here is an almost word for word copy of an example which (surprise surprise) doesn't work:

var log4js = require('log4js');
log4js.configure({
appenders: [
    { type: 'console' },
    { type: 'file', filename: "test.log", category: 'my_project' }
]
});
var logger  = log4js.getLogger('my_project');
//logger.setLevel('DEBUG');
logger.log('Why does node.js not have any decent logging frameworks??');

It returns the following:

return this.level <= otherLevel.level;
                                ^
TypeError: Cannot read property 'level' of undefined
at Level.isLessThanOrEqualTo (xxxx\node_modules\log4js\lib\levels.js:41:34)
at Logger.isLevelEnabled (xxxx\node_modules\log4js\lib\logger.js:63:21)
at Logger.log (xxxx\node_modules\log4js\lib\logger.js:56:12)
2

There are 2 best solutions below

0
On BEST ANSWER

So. the problem (and im still not sure why) is that this:

require('log4js').configure('log4js.json');
var logger = require('log4js').getLogger();

Needs to be written thus:

var log4js = require('log4js');
log4js.configure('log4js.json');
var logger = log4js.getLogger();

Being clever and doing a require() twice on log4js seems to cause chaos.

1
On

it appears you are using log4js incorrectly. there is no function called log in object logger. Use any of the following:

logger.trace('Entering cheese testing');

logger.debug('Got cheese.');

logger.info('Cheese is Gouda.');

logger.warn('Cheese is quite smelly.');

logger.error('Cheese %s is too ripe!', "gouda");

logger.fatal('Cheese was breeding ground for listeria.');