Why log4js keeps splitting the log files?

141 Views Asked by At

I'm using log4js to log the test results to files. I have four test files and each file will create its own log file. So that should be 4 log files total, but I'm getting 6 log files total. Somehow it only logs the first two lines into the log file, but the rest of the lines are copied to new log files. Can someone help thanks?

file: conf.js

exports.config = {

    directConnect: true,
  
    //capabilities to be passed to the webdriver instance.
    capabilities: {
      'browserName': 'chrome',
  
      chromeOptions: {
        useAutomationExtension: false
      },
    },
  
    //framework to use. Jasmine is recommended.
    framework: 'jasmine',
  
    specs: [
      '../tests/testGoogle.js',
      '../tests/testCalculator.js'
    ],
  
    //options to be passed to Jasmine.
    jasmineNodeOpts: {
      defaultTimeoutInterval: 30000
    }
}

file: log4js.json

{
    "appenders": {
        "test": {
            "type": "multiFile",
            "base": "testOutput/",
            "property": "categoryName",

            "flags": "w",
            "pattern": "yyyy-MM-dd-hh-mm-ss", 
            "compress": false,
            "alwaysIncludePattern": true,
            "extension": ".log",
            "keepFileExt": true
        }
    },
    "categories": {
        "default": {
            "appenders": ["test"],
            "level": "info"
        }
    }
}

file: log4jsConfig.js

const log4jsLogger = require('log4js');
log4jsLogger.configure("./helpers/logger/log4js.json");

let getLoggerHelper = function(){
    this.createLogFile = function(filename){
        return log4jsLogger.getLogger(filename);
    }
}

module.exports = new getLoggerHelper();

file: testCalculator.js

let calculator = require("../pages/superCal/calculator");
const log4jsConfig = require("../helpers/logger/log4jsConfig.js");

describe("Test Suite- Calculator", function(){
    var testLogger = log4jsConfig.createLogFile("logTestCalculator");
    testLogger.info("logTestCalculator is created");
    testLogger.info("testCalculator.js, describe() log to logTestCalculator");

    it("addition test", function(){
      testLogger.info("testCalculator.js, describe(), it(), launch URL");        
      calculator.get("http://juliemr.github.io/protractor-demo/");

      testLogger.info("testCalculator.js, describe(), it(), sending first value 2");       
      calculator.enterFirstNumber('2');

      browser.sleep(2000);

      testLogger.info("testCalculator.js, describe(), it(), sending second value 3");    
      calculator.enterSecondNumber('3');

      testLogger.info("testCalculator.js, describe(), it(), sending operand MULTIPLICATION");
      calculator.enterOperator("MULTIPLICATION");

      browser.sleep(2000);

      testLogger.info("testCalculator.js, describe(), it(), sending click on Go! button");    
      calculator.clickingGo();
  
    
      testLogger.info("testCalculator.js, describe(), it(), verify result");    
      expect("6").toEqual(calculator.getResult("6").getText());
      browser.sleep(2000);
  });
});

file: calculator.js

const log4jsConfig = require("../../helpers/logger/log4jsConfig.js");
    
let homepage = function(){
    var testLogger = log4jsConfig.createLogFile("logCalculator");
    testLogger.info("logCalculator is created");
    testLogger.info("calculator.js, describe() log to logCalculator");

    this.myConsoleLog = function(){
        console.log("at myConsoleLog()");
    };

    this.get = function(url){
        testLogger.info("calculator.js, get");
        browser.get(url);
        browser.sleep(3000);
    };

    this.enterFirstNumber = function(value1){
        testLogger.info("calculator.js, enterFirstNumber");
        element(by.model("first")).sendKeys(value1);
    };

    this.enterSecondNumber = function(value2){
        testLogger.info("calculator.js, enterSecondNumber");
         element(by.model("second")).sendKeys(value2);
    };

    this.enterOperator = function(oper){
        testLogger.info("calculator.js, enterOperator");
        element(by.model("operator")).$('[value='+ oper).click();
    };

    this.clickingGo = function(){
       testLogger.info("calculator.js, clickingGo");
        element(by.css('[ng-click="doAddition()"]')).click();
    };

    this.getResult = function(result){
        testLogger.info("calculator.js, getResult");
        var retVal = element(by.cssContainingText(".ng-binding", result));
        return retVal;
    };    
};

module.exports = new homepage();

file: testGoogle.js

let googleSite = require("../pages/googleSite/google");
const log4jsConfig = require("../helpers/logger/log4jsConfig.js");
    
describe("Test Suite- Google", function(){
    var testLogger = log4jsConfig.createLogFile("logTestGoogle");
    testLogger.info("logTestGoogle is created");
    testLogger.info("testGoogle.js, describe() log to logTestGoogle");

    it("Launch Google site", function(){
        testLogger.info("testGoogle.js, describe(), launch URL");
        googleSite.get("http://www.google.com");

        expect("Google").toEqual(googleSite.getGoogleText("Google"));
        browser.sleep(2000);
    });
});

file: google.js

const log4jsConfig = require("../../helpers/logger/log4jsConfig.js");
    
let google = function(){
    var testLogger = log4jsConfig.createLogFile("logGoogle");
    testLogger.info("logGoogle is created");
    testLogger.info("google.js, describe() log to logGoogle");

    this.get = function(url){
        testLogger.info("google.js, get");
        browser.driver.get(url);
        browser.sleep(3000);
    };

    this.getGoogleText = function(text){
        testLogger.info("google.js, getGoogleText");
        return text;
        browser.sleep(3000);
    };
};

module.exports = new google();

And the output log files:

enter image description here

For an example, these two log files that got split:

"logCalculator.2020-07-29-12-34-19.log"

"logCalculator.2020-07-29-12-34-25.log"

this log file "logCalculator.2020-07-29-12-34-19.log", has:

[2020-07-29T12:34:19.687] [INFO] logCalculator - logCalculator is created
[2020-07-29T12:34:19.688] [INFO] logCalculator - calculator.js, describe() log to logCalculator

this log file "logCalculator.2020-07-29-12-34-25.log", has:

[2020-07-29T12:34:25.843] [INFO] logCalculator - calculator.js, get
[2020-07-29T12:34:25.846] [INFO] logCalculator - calculator.js, enterFirstNumber
[2020-07-29T12:34:25.848] [INFO] logCalculator - calculator.js, enterSecondNumber
[2020-07-29T12:34:25.849] [INFO] logCalculator - calculator.js, enterOperator
[2020-07-29T12:34:25.850] [INFO] logCalculator - calculator.js, clickingGo
[2020-07-29T12:34:25.850] [INFO] logCalculator - calculator.js, getResult
0

There are 0 best solutions below