Disable console printing in Log4js

I only allowed logging using Log4js in my Node.js application. I used the configuration option from https://github.com/nomiddlename/log4js-node and it works fine.

It writes logs to the log file as well as to the console. I do not want this printed on the console. Unable to figure out how to configure this. terribly sorry to ask this stupid question.

+6
source share
5 answers

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

+10
source

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

Here is the code I wrote:

 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()); 
+1
source

I had this problem, as well as creating a console utility. I wanted the utility to write its output to the console, but save the logs in a file. No matter how I tried, I could not separate the two output buffers.

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

 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'); 
0
source

If someone has to stumble upon this rather old thread, note (meanwhile?) There is also the option to disable console logging by configuration.

Just set replaceConsole: true when setting up @see Example for github

I don’t know if it really decides what you plan to achieve, but for sure you will delete the logs from the console by doing this

0
source

Here's how I got it to work:

The problem is that the default category for the appender configuration is "[all]". Set the category to [[default] , and it only applies to registrars who β€œreceived” without a category: log4js.getLogger()

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

More explanation:

You probably have / have something similar to the appender config example

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

And then you get a registrar with or without a category name:

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

Exit:

 [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 
0
source

Source: https://habr.com/ru/post/909920/


All Articles