Search code examples
testingprotractorcucumberlog4j

How to fix Protractor, log4js configuration error?


I am trying to run a protractor tests. But facing issue with log4js. I did npm install log4js. Does the before launch configuration looks right? Is there anything I should change in the format for current version of log4js?

Below is the error:

Error: Problem with log4js configuration: ({
  appenders: {
    out: {
      type: 'log4js-protractor-appender',
      category: 'protractorLog4js'
    },
    app: {
      type: 'file',
      filename: './logs/ExecutionLog.log',
      category: 'protractorLog4js'
    }
  },
  categories: { default: { appenders: [ 'out', 'app' ], level: 'info' } }
}) - appender "out" is not valid (type "log4js-protractor-appender" could not be found)
    at C:\Project\PrjectName\node_modules\log4js\lib\configuration.js:31:13
    at Array.forEach (<anonymous>)
    at Object.throwExceptionIf (C:\Project\PrjectName\node_modules\log4js\lib\configuration.js:29:9)
    at createAppender (C:\Project\PrjectName\node_modules\log4js\lib\appenders\index.js:47:17)
    at C:\Project\PrjectName\node_modules\log4js\lib\appenders\index.js:77:25
    at Array.forEach (<anonymous>)
    at setup (C:\Project\PrjectName\node_modules\log4js\lib\appenders\index.js:75:33)
    at C:\Project\PrjectName\node_modules\log4js\lib\configuration.js:46:33
    at Array.forEach (<anonymous>)
    at Object.configure (C:\Project\PrjectName\node_modules\log4js\lib\configuration.js:46:13)
npm ERR! Test failed.  See above for more details.

This is my before launch configuration in config file

beforeLaunch:function(){
          log4js.configure({
            appenders: {
              out:{ type: 'log4js-protractor-appender'},
              app:{ type: "file",
                    filename: './logs/ExecutionLog.log'}
              },
            categories: {
                    default: { appenders: [ 'out', 'app' ], level: 'info' }
            }
            });
        },

Thanks


Solution

  • the type of appenders that log4js supports are:

    export type Appender = CategoryFilterAppender
        | ConsoleAppender
        | FileAppender
        | SyncfileAppender
        | DateFileAppender
      | LogLevelFilterAppender
      | NoLogFilterAppender
        | MultiFileAppender
        | MultiprocessAppender
        | RecordingAppender
        | StandardErrorAppender
        | StandardOutputAppender
        | CustomAppender;
    

    If you remove the "appender" from the name you will get the supported type ex: console, file, multifile... etc

    Here is an exmple configuration.

    const log4js_config: Configuration = {
            appenders: {
                consoleErrors: {
                    type: 'logLevelFilter',
                    appender: 'console',
                    level: 'error'
                },
                console: {
                    type: 'console'
                },
            },
            categories: {
                default: { appenders: [ 'console', 'consoleErrors' ], level: 'debug' }
            }
        };
    
    const Log4js = require('log4js');
    log4js.configure(LOG4JS_CONFIGURATION);
    

    Like this you have two types of appenders in the default category.

    • console appender will log everything in the console no matter the level.
    • consoleErrors overrides other rules for console and filter only error level messages or higher to console.

    You can read more about log4js configuration in: https://github.com/log4js-node/log4js-node

    Examples how the appenders work and how to configure them: https://github.com/log4js-node/log4js-node/tree/master/examples