9

I just enabled logging using Log4js in my Node.js application. I have used the config option from https://github.com/nomiddlename/log4js-node and it's working fine.

It writes the logs in the log file, as well as in the console. I do not want this to be printed in the console. Not able to figure out how to configure this. awfully sorry to ask this silly question.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
user644745
  • 5,673
  • 9
  • 54
  • 80

5 Answers5

10

Use log4js.clearAppenders() before adding the appenders you want to use.

Matt
  • 68,711
  • 7
  • 155
  • 158
1

In socket.io, you can set a custom logger. I tried assigning a log4js logger, but that caused an error. I suspect you (and I) will have to write a wrapper, which passes the logging call along to log4js.

Here is the code I ended up writing:

var LogWrapper = function() {
    this.logger = log4js.getLogger('socket.io');
};
LogWrapper.prototype.error = function() {
    this.logger.error.apply(this.logger, arguments);
};
LogWrapper.prototype.warn = function() {
    this.logger.warn.apply(this.logger, arguments);
};
LogWrapper.prototype.info = function() {
    this.logger.info.apply(this.logger, arguments);
};
LogWrapper.prototype.debug = function() {
    this.logger.debug.apply(this.logger, arguments);
};
io.set('logger', new LogWrapper());
abendigo
  • 954
  • 1
  • 8
  • 31
0

I had this issue as well building a console-based utility. I wanted the utility to write its output to the console, but keep logs in a file. No matter how I tried, I couldn't seem to separate the two output buffers.

The solution was to add a category to the console appender in the log4js.configure call, like so:

var log4js = require('log4js'); 

log4js.configure({
  appenders: [
    { type: 'console', category: 'thiswillgotoconsole' },
    { type: 'file', filename: 'myLog.log', category: 'thiswillgotofile' }
  ]
});

var logger = log4js.getLogger('thiswillgotofile');
logger.setLevel('debug');

logger.debug('thiswillgotofile');
console.log('thiswillgotoconsole');
o-o
  • 8,158
  • 2
  • 20
  • 21
0

if anybody should stumble upon this rather old thread, please note (meanwhile ?) there is also the possibility to disable logging the the console by configuration.

Simply set replaceConsole: true while configuring @see Example on github

Don't know if it really solves what you planned to achieve but for sure you will make the logs disappear from console by doing so

0

Here's how I got this to work:

The issue is that the default category for the appender config is '[all]'. Set the category to '[default]' and it will only apply to loggers that are 'gotten' with out a category: log4js.getLogger()

{
   appenders: [
     { type: 'console', category: '[default]' },
     { type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
   ]
}

More explanation:

You probably had/have something that looks like the example appender config

{
  appenders: [
    { type: 'console' },
    { type: 'file', filename: 'logs/cheese.log', category: 'cheese' }
  ]
}

And then you get the logger by with or without a category name:

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

logger.info(1)
cheeseLogger(2)

Output:

[2016-10-25 15:43:06.225] [INFO] [default] - 1
[2016-10-25 15:43:06.225] [INFO] cheese - 2

logs/cheese.log:

[2016-10-25 15:43:06.225] [INFO] cheese - 2
Gabriel Littman
  • 552
  • 3
  • 13