我的目标:了解使用Winston 3.0是否可以将Puppeteer控制台错误/警告输出记录到文件中以及如何实现。
到目前为止我所知道的:我现在可以运行Puppeteer和Winston成功捕获一组URL的屏幕截图。但是,即使Winston确实创建了日志文件,它也不会向其写入任何内容。
我不知道的事情:我找不到解释如何的文档。Winston知道将Puppeteer控制台信息写入该文件。我的代码如下:
'use strict';
const winston = require('winston');
const puppeteer = require('puppeteer');
// As of winston@3, the default logging format is JSON.
/* Adding Winston code here */
const logger = winston.createLogger({
transports: [
new winston.transports.File({
filename: 'mycombined.log',
level: 'info'
}),
new winston.transports.File({
filename: 'myerrors.log',
level: 'error'
})
]
});
/* End primary Winston code */
/* Pupetteer loop code begins here */
const qaurls = ["https://www.urlnumber1.com/",
"https://urlnumber2.com/",
"https://www.urlnumber3.com/",
"https://urlnumber4.org/"
];
var parseurl = require('url');
puppeteer.launch({headless: false}).then(async browser => {
let page = await browser.newPage();
await page.setViewport({width: 1103, height: 1440});
for (var i = 0; i < qaurls.length; i++) {
var url = qaurls[i];
console.log('SCANNING ' + url);
var url_parts = parseurl.parse(url);
await page.goto(url);
var screenshot_name = i + '_screenshot_' + url_parts.hostname + '.png';
await page.screenshot({path: screenshot_name});
console.log('Screenshot = ' + screenshot_name);
logger.log('silly', 'Hello, this is a raw logging event');
}
await browser.close();
});