Search code examples
javascriptautomationjasmineprotractorjasmine-node

I am finding trouble using log4js-protractor-appender


My log4js.js file code

'use strict';
var log4js = require('log4js');
var log4jsGen = {
    getLogger: function getLogger() {
        log4js.loadAppender('file');
        log4js.addAppender(log4js.appenders.file('./ApplicationLogs.log'), 'logs');

        var logger = log4js.getLogger('logs');
        return logger;
    }
};

module.exports = log4jsGen;

My conf.js file(specific to appender section only)

"appenders": [{
    "type": "log4js-protractor-appender",
    "append": 'false',
    "maxLogSize": 20480,
    "backups": 3,
    "category": "relative-logger"
}],

Problem:

1) IS there a way that the logs will get overwritten in each run. 2) Why log4js-protractor-appender is not working, instead log4js is working, the merit of the previous is that it resolves the promises which is passed as an argument.


Solution

  • Thats a great question. Yes log4js-protractor-appender is awesome. It is built specially for Protractor based environments and it places all logger command in Protractor Control flow and resolves Protractor promises before logging.

    You were using it incorrectly. The appender options are not part of Protractor config options but can be integrated. The approach you have is a little old one and I have updated by blog post

    These are the steps as an answer to your question-2

    Step 1: Install log4js npm module

    Step 2: Install log4js-protractor-appender module

    Step 3: Add the logger object creation logic in protractor beforeLaunch() and assign it onto ​​browser protractor global object

    'use strict';
    var log4js = require('log4js');
    beforeLaunch:function(){
        if (fs.existsSync('./logs/ExecutionLog.log')) {
            fs.unlink('./logs/ExecutionLog.log')
        }
        log4js.configure({
            appenders: [
                { type: 'log4js-protractor-appender', category: 'protractorLog4js' },
                {
                    type: "file",
                    filename: './logs/ExecutionLog.log',
                    category: 'protractorLog4js'
                }
            ]
        });
    },
    onPrepare: function() {
        browser.logger = log4js.getLogger('protractorLog4js');
    },
    

    Step 4: Use logger object in your tests by accessing through browser.logger

    describe('sample test', function(){
        it('Sample Check', function(){
            browser.get("http://www.protractortest.org/#/");
            browser.logger.info("Testing Log4js");
            browser.sleep(5000);
            browser.logger.info('Displayed text is:', browser.getCurrentUrl());
            var elm = element(by.css('.lead'))
            browser.logger.info('Displayed text is:', elm.getText());
        });
    });
    

    But one thing to note is - This appender is just an console appender and will not be able to write to file. The file will still contain unresolved promises

    Sample Output:

    [21:54:23] I/local - Starting selenium standalone server...
    [21:54:23] I/launcher - Running 1 instances of WebDriver
    [21:54:25] I/local - Selenium standalone server started at http://192.168.1.5:60454/wd/hub
    Started
    [2017-02-03 21:54:30.905] [INFO] protractorLog4js - Testing Log4js
    [2017-02-03 21:54:35.991] [INFO] protractorLog4js - Displayed text is: http://www.protractortest.org/#/
    [2017-02-03 21:54:36.143] [INFO] protractorLog4js - Displayed text is: Protractor is an end-to-end test framework for Angular and AngularJS applications. Protractor runs tests against your application running in a real browser, interacting with it as a user would.
    .
    

    Answer to your Question 1: How to overwrite logs each run. I added a simple logic in beforeLaunch() to delete old logs if they exist and its part of the code snippet I pasted above