1

I am novice to nodejs and wanted my node app to log to a file as well as console.

Here is my code structure:

fpa_log4j.json:

{
  "appenders": {
    "fpa_file": {
      "type": "file",
      "filename": "fpa.log",
      "maxLogSize": 10485760,
      "backups": 10,
      "compress": true
    },
    "screen": {
      "type": "stdout",
      "layout": {
        "type": "coloured"
      }
    }
  },
  "categories": {
    "default": {
      "appenders": [
        "fpa_file",
        "screen"
      ],
      "level": "trace"
    }
  }
}

config.js

var LOG4J_CFG = 'config/fpa_log4j.json';
var fs = require('fs');
var log4js = require('log4js');
var path = require('path');

LOG4J_CFG = path.resolve(LOG4J_CFG);
log4js.configure(JSON.parse(fs.readFileSync(LOG4J_CFG, 'utf8')));
....
....
module.exports = {
    log4js,
    ....
    ....
}

app.js

var cfg = require('./config');
var log = cfg.log4js.getLogger("appMain");
log.info('FPA program STARTED!');

OUTPUT

[2017-12-04T03:20:17.791] [INFO] appMain - FPA program STARTED!

However the log file seems to be empty:

dir fpa.log
 Volume in drive C is XXXXXXXXX
 Volume Serial Number is XXXXXXXXXXXX

 Directory of c:\fpaMain

12/04/2017  12:13 AM                 0 fpa.log
               1 File(s)              0 bytes
               0 Dir(s)  12,242,776,064 bytes free

To be frank, I was able to make this work few days back. But then something got changed (alas, not able to recollect what it is!) I guess, and then logging into the file stopped.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Dec 04 '17 at 10:42
  • thank you @halfer for the edit. I was not aware about the potential problem. thanks for guiding me correctly. Will surely keep it in mind in future. – Dilip Muthukurussimana Dec 04 '17 at 10:54

1 Answers1

3

Ok. So I figured it out finally. Sharing it here for the sake of others. Here it comes:

There were few more lines in the app.js (which were removed for clarity) and the app was in fact crashing immediately after the log.info() statement. And I was expecting the statements to appear in the log file as it appeared on the console. But then, I failed to realize that, unlike other programming languages, nodejs has got the powerful feature of parallel processing by birth itself (which in fact was one of the core reasons for my love towards it :)).

So in my case, due to its unparallel feature of parallel processing, nodejs just fired the log statement and went on processing other statements. And, when it crashed, it took down the whole program with it. Since writing to the console was way much faster than IO (for the obvious reasons!!), it showed up on the screen faster. However before it could write to the file system the app got crashed, ending up nothing being written to the file!

Lesson Learned:

  1. Always remember that nodejs === parallel processing
  2. For any IO related issues like the above, ensure that the program runs to complete or at least for some more time before it crashes, thus providing enough time for the IO to complete.