How to use log4js

Comprehensive log4js code examples:

How to use log4js.fatal:

91
92
93
94
95
96
97
98
99
100
            global.app.garbageSeller = new GarbageSeller(global.app.settings, global.app.client);

            /** On initialization complete callback */
            onInit(global.app);
        } catch (error) {
            logger.fatal(`error on application initialization: ${error}`);
            process.exit(0);
        }
    });
});

How to use log4js.warn:

90
91
92
93
94
95
96
97
98
99

/** Get current market quote coin to check if shit coin is enough for selling to grand quote minNotional volume */
const [, quote] = possibleMarket.split('/');
const quoteConfig = this.settings.getQuoteBySymbol(quote);
if (typeof quoteConfig === 'undefined') {
    logger.warn(`min notional config not found for ${quote}`);
    continue;
}

/** @var float Quote volume be obtained after shit coin sell */

How to use log4js.json:

107
108
109
110
111
112
113
114
115
116
    }
};

/**
 * Configure the logger.
 * Configure file just like log4js.json. And support ${scope:arg-name} format property setting.
 * It can replace the placeholder in runtime.
 * scope can be:
 *     env: environment letiables, such as: env:PATH
 *     args: command line arguments, such as: args:1

How to use log4js.debug:

76
77
78
79
80
81
82
83
84
85
const possibleMarkets = this.client.getPossibleMarkets([symbol]);

/** Check if there any markets where shit coin can be sold to obtain minNotional amount of quote */
for (const possibleMarket of possibleMarkets) {
    try {
        logger.debug(`${possibleMarket}`);
        const orderBook = await this.client.fetchOrderBook(possibleMarket);
        if (!orderBook['bids'] || !orderBook['bids'][0]) {
            logger.debug(`order book  ${possibleMarket} is empty`);
            continue;

How to use log4js.info:

451
452
453
454
455
456
457
458
459
460
    delay: 1,
    refillQuote: initialQuote,
})
.then((status) => {
    if (status !== -1) {
        logger.info(`equalizer completed for - ${initialQuote}`);
        this.getClient().getEqualizer().releaseChannel(initialQuote);
    }
})
.catch((e) => {

How to use log4js.replaceConsole:

55
56
57
58
59
60
61
62
63
64
const initLogging = (logLevel, config) => {
  // log4js.configure() modifies exports.logconfig so check for equality first.
  const logConfigIsDefault = deepEqual(config, defaultLogConfig());
  log4js.configure(config);
  log4js.setGlobalLogLevel(logLevel);
  log4js.replaceConsole();
  // Log the warning after configuring log4js to increase the chances the user will see it.
  if (!logConfigIsDefault) logger.warn('The logconfig setting is deprecated.');
};

How to use log4js.addLayout:

30
31
32
33
34
35
36
37
38
39

let backupCount = 15;
if (hasAttr(mo.config.logger, 'backupCount') && hasAttr(mo.config.logger.backupCount, 'max')) {
    backupCount = mo.config.logger.backupCount.max;
}
log4js.addLayout('baetyl', () => logEvent => {
    const asctime = moment(logEvent.startTime).format('YYYY-MM-DD HH:mm:ss');
    const name = logEvent.categoryName;
    const levelname = logEvent.level.levelStr;
    const message = logEvent.data;

How to use log4js.setGlobalLogLevel:

54
55
56
57
58
59
60
61
62
63
64


const initLogging = (logLevel, config) => {
  // log4js.configure() modifies exports.logconfig so check for equality first.
  const logConfigIsDefault = deepEqual(config, defaultLogConfig());
  log4js.configure(config);
  log4js.setGlobalLogLevel(logLevel);
  log4js.replaceConsole();
  // Log the warning after configuring log4js to increase the chances the user will see it.
  if (!logConfigIsDefault) logger.warn('The logconfig setting is deprecated.');
};

How to use log4js.shutdown:

92
93
94
95
96
97
98
};
exports.fatal = function (){
        return logger.fatal.apply(logger, Array.prototype.slice.call(arguments));
};
exports.shutdown = function (callback) {
        return log4js.shutdown(callback);
};

How to use log4js.error:

456
457
458
459
460
461
462
463
464
465
                logger.info(`equalizer completed for - ${initialQuote}`);
                this.getClient().getEqualizer().releaseChannel(initialQuote);
            }
        })
        .catch((e) => {
            logger.error(`equalizer error - ${e.message}`);
            this.getClient().getEqualizer().releaseChannel(initialQuote);
        });
    }
}

How to use log4js.configure:

37
38
39
40
41
42
43
44
45
46
    const name = logEvent.categoryName;
    const levelname = logEvent.level.levelStr;
    const message = logEvent.data;
    return `${asctime} - ${name} - ${levelname} - ${message}`;
});
log4js.configure({
    appenders: {
        file: {
            type: 'file',
            filename: mo.config.logger.path,

How to use log4js.levels:

166
167
168
169
170
171
172
173
174
// starts listening to requests as reported in issue #158. Not installing the log4js connect
// logger when the log level has a higher severity than INFO since it would not log at that level
// anyway.
if (!(settings.loglevel === 'WARN' && settings.loglevel === 'ERROR')) {
  app.use(log4js.connectLogger(logger, {
    level: log4js.levels.DEBUG,
    format: ':status, :method :url',
  }));
}

How to use log4js.getLogger:

17
18
19
20
21
22
23
24
25
26
27
28
configuration.parseOptions();


// log4js
const log4js = require('log4js');
const { url } = require('inspector');
var logger = log4js.getLogger();


// The queryCache.
const queryCache = new Map();