量角器中的 Winston 记录器实现测试失败

Test fails with Winston logger implemention in protractor

背景:我正在使用 Jasmine2 作为 Protractor 的测试框架,并尝试在框架中使用 winston 包实现记录器机制以实现更好的日志记录。

问题: 测试失败,cmd 出现以下非零错误,在脚本中包含 winston 之前工作正常。你能帮我正确实施吗?

非零错误:

Report destination:   target\e2e\screenshots\my-report.html
[20:28:52] I/launcher - Running 1 instances of WebDriver
[20:28:52] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub

[20:28:56] I/launcher - 0 instance(s) of WebDriver still running
[20:28:56] I/launcher - chrome #01 failed 1 test(s)
[20:28:56] I/launcher - overall: 1 failed spec(s)
[20:28:56] E/launcher - Process exited with error code 1

下面是相应的文件供参考:

Scenario_01.js:

describe('Scenario_01', function() {

 var Logging = require('./scripts/LoggingMech.js');
 var common = require('./scripts/CloseBrowsers.js');    
 var Login = require('./scripts/Login.js'); 


 it('Login', function() {
         browser.waitForAngularEnabled(false); 
         Login.login('admin','Adminpwd')
          .catch(error => Logging.Logger.log(error));
     });

afterAll(function(){
    common.closeBrowsers();
});
});

Login.js:

var Login = function() {

     this.login = async function(username, passwordKey){
     await browser.get('http://testwebsite.com/showCust');
     await element(by.name('USER')).sendKeys(username);
     await element(by.name('PASSWORD')).sendKeys(passwordKey);
     await element(by.xpath('/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td/form/input[9]')).click();
     await element(by.name('YES')).click();
     //browser.sleep(10000);

};
}
module.exports = new Login();

LoggingMech.js

const winston = require('winston');
var Logging = function() {

     this.Logger = function(){
      const logger = winston.createLogger({

           level: 'info',
           format: format.simple(),
            transports: [
                new winston.transports.Console(),
                new winston.transports.File({ filename: 'TodaysLog.log' })
                        ]
});

};
}
module.exports = new Logging();

问题

const logger = winston.createLogger...您正在创建一个 winston 引用,但它从未传递到任何地方。您只是将其存储在常量中。您必须使它成为 Logger 属性的 属性,然后创建 Logging.Logger 的新实例并在其上调用 log()

修复

You would have to make it a property of Logger attribute.

LoggingMech.js 中你做 this.logger = winston.createLogger 而不是 const logger = winston.createLogger

and then create a new instance of Logging.Logger and call log() on it.

var Logging = require('./scripts/LoggingMech.js');
// logs foo
(new Logging.Logger()).log('foo');

但我认为您正在根据需要将其做得更复杂。要实现记录器,您只需将 LoggingMech.js 文件更改为以下内容:

const winston = require('winston');
module.exports = winston.createLogger({
    level: 'info',
    format: format.simple(),
    transports: [
        new winston.transports.Console(),
        new winston.transports.File({
            filename: 'TodaysLog.log'
        })
    ]
});

并这样称呼它:

var Logging = require('./scripts/LoggingMech.js');
Logging.log('foo');

记录器的配置仍然封装在LoggingMech.js中,调用起来更简单。代码也更容易维护。

更新 1:使用 winston 登录

winston.log() 方法只接受日志对象,不接受字符串。

// logs "I am a log message."
winston.log({
  level: 'info',
  message: 'I am a log message.'
}

要记录纯字符串,您可以使用 logger.info()

更新 2:完整的 winston 日志记录示例

一个工作日志示例。以下代码适用于我的机器:

winston.js

var winston = require('winston');
module.exports = winston.createLogger({
    level: 'info',
    transports: [
        new winston.transports.Console(),
        new winston.transports.File({
            filename: 'TodaysLog.log'
        })
    ]
});

index.js

var Logging = require('./winston');
var message = {
    level: 'info',
    message: 'Hello distributed log files!'
  };

  Logging.log(message);
  Logging.info('sdf');
  Logging.error(message);
  Logging.warn(message);

在TodaysLog.log

中记录的内容
{"level":"info","message":"Hello distributed log files!"}
{"message":"sdf","level":"info"}
{"level":"error","message":"Hello distributed log files!"}
{"level":"warn","message":"Hello distributed log files!"}

@sylvanBregy:以下是建议修改的文件,供参考。

我可以见证测试完成并成功登录,但不确定为什么会在控制台中触发错误。

此外,日志文件中也没有写入任何内容:(

Scenario_01

describe('Scenario_01', function() {
console.log("Into Scenario1");

 var Logging = require('./scripts/LoggingMech.js');
 var common = require('./scripts/CloseBrowsers.js');    
 var Login = require('./scripts/Login.js'); 


 it('Login', function() {
         browser.waitForAngularEnabled(false); 
         Login.login('admin','Adminpwd')
           Logging.info("foo"); //Neither this worked nor the below
           Logging.info();
     });

afterAll(function(){
    common.closeBrowsers();
});
});

Login.js

var Login = function() {
  console.log("Into Login Function");

     this.login = async function(username, passwordKey){
     await browser.get('http://testwebsite.com/showCust');
     await element(by.name('USER')).sendKeys(username);
     await element(by.name('PASSWORD')).sendKeys(passwordKey);
     await element(by.xpath('/html/body/table/tbody/tr[2]/td/table/tbody/tr/td/table/tbody/tr[3]/td/form/input[9]')).click();
     await element(by.name('YES')).click();
     //browser.sleep(10000);

};
}
module.exports = new Login();

LoggingMech.js

const winston = require('C:\Program Files\nodejs\node_modules\npm\node_modules\winston\lib\winston');
//const winston = require('winston');

module.exports = winston.createLogger({
                     level: 'info',
                     format: 'simple',
                     transports: [
                            new winston.transports.Console(),
                            new winston.transports.File({filename:'./logs/TodaysLog.log',level: 'info'})
                            ],
});

控制台日志

Report destination:   target\e2e\screenshots\my-report.html
[20:22:58] I/launcher - Running 1 instances of WebDriver
[20:22:58] I/hosted - Using the selenium server at http://127.0.0.1:4444/wd/hub
Into Scenario1
Into Login Function

[20:23:03] I/launcher - 0 instance(s) of WebDriver still running
[20:23:03] I/launcher - chrome #01 failed 1 test(s)
[20:23:03] I/launcher - overall: 1 failed spec(s)
[20:23:03] E/launcher - Process exited with error code 1