Skip to content Skip to sidebar Skip to footer

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.add

Solution 1:

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


Solution 2:

I have check this issue with and followed the steps mentioned in Answer 1 and it works for me.

Earlier I was getting log output in Console only but now I am getting log in console and file also.

I corrected the file path passing and Set type: "file" in log4js configure in conf file.

Log4js in Conf file

Log appender in file

Please let me know if you face any issue again.

Thanks


Post a Comment for "I Am Finding Trouble Using Log4js-protractor-appender"