3

There are a few third party libraries that are using log4js to output their logs. I know it sounds counter-intuitive but I would like to disable these logs temporarily to be able to investigate an issue that I have.

How do I temporarily disable log4js logs temporarily in code in javascript? Specifically nodejs.

2

2 Answers 2

1

In log4js 5.3.0 (not sure how many versions back it's the same) you can do this by turning everything off temporarily at the root logger, which affects every other logger:

log4js.getLogger().level = 'off';

A full example:

#!/bin/env node

const log4js = require('log4js');

log4js.configure({
    appenders: {
        out: { type: "stdout" }
    },
    categories: {
        default: {
            appenders: [ "out" ],
            level: "info"
        }
    }
});

const rootLogger = log4js.getLogger();

const log = log4js.getLogger('foo');

log.info('this is on - turning off');

rootLogger.level = 'off';

log.info('this is off - turning back on');

rootLogger.level = 'info';

log.info('this is back on');

Which gives you:

[2019-11-13T22:00:37.541] [INFO] foo - this is on - turning off
[2019-11-13T22:00:37.544] [INFO] foo - this is back on
Sign up to request clarification or add additional context in comments.

Comments

0

In the app.js file of my nodejs application I place the following code to temporarily disable log4js logs from thirdparty libraries.

const log4js = require('log4js')
log4js.configure({})

This works for disabling the logs.

2 Comments

Just for info, I think that must have worked with an earlier version of log4js, but it seems to demand a proper configuration now so {} throws an error.
/usr/local/bin/node_modules/log4js/lib/configuration.js:31 throw new Error(Problem with log4js configuration: (${util.inspect(config, { depth: 5 })}) ^ Error: Problem with log4js configuration: ({}) - must have a property "appenders" of type object.

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.