我設法使用logger
插件開啓PhantomJS瀏覽器的prerender.io日誌記錄功能。但是,我只想記錄錯誤消息。 logger
插件記錄了一切。基於以下網頁:設置prerender.io只輸出來自PhantomJS的錯誤消息
http://phantomjs.org/api/webpage/handler/on-error.html http://phantomjs.org/api/phantom/handler/on-error.html
PhantomJS應有時發生錯誤,則觸發事件onError
。在嘗試創建插件之前,我暫時嘗試更新prerender.io的server.js
,以附加到onError
事件。我下面更新都server.createPage
功能,附加到page.onError
,甚至試圖page.set('onError')
,類似於logger.js
server.createPage = function(req, res) {
var _this = this;
if(!this.phantom) {
setTimeout(function(){
_this.createPage(req, res);
}, 50);
} else {
req.prerender.phantomId = this.phantom.id;
this.phantom.createPage(function(page){
req.prerender.page = page;
console.log("registering onError for page");
page.onError = function(msg, trace) {
var msgStack = ['PAGE PHANTOM ERROR OCCURRED: ' + msg];
msgStack.push('----------------------------------');
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
msgStack.push('==================================');
console.log(msgStack.join('\n'));
};
page.set('onError', function(msg, trace) {
var msgStack = ['PAGE2 PHANTOM ERROR OCCURRED: ' + msg];
msgStack.push('----------------------------------');
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
msgStack.push('==================================');
console.log(msgStack.join('\n'));
});
_this.onPhantomPageCreate(req, res);
});
}
};
發現代碼中,我還更新了server.onPhantomCreate()
,方法附加到一般phantom.onError
,按照如下:
server.onPhantomCreate = function(phantom) {
util.log('started phantom');
this.phantom = phantom;
this.phantom.id = Math.random().toString(36);
console.log("Registering phantom.onError");
phantom.onError = function(msg, trace) {
var msgStack = ['PHANTOM ERROR OCCURRED: ' + msg];
msgStack.push('----------------------------------');
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
msgStack.push('==================================');
console.log(msgStack.join('\n'));
};
phantom.set('onError', function(msg, trace) {
var msgStack = ['PHANTOM ERROR OCCURRED: ' + msg];
msgStack.push('----------------------------------');
if (trace && trace.length) {
msgStack.push('TRACE:');
trace.forEach(function(t) {
msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
});
}
msgStack.push('==================================');
console.log(msgStack.join('\n'));
});
};
但是,沒有什麼被記錄。任何想法爲什麼是這樣的?
這會拋出一個'dest.on不是函數'的錯誤從電話 –
對不起,這可能只適用於我們的代碼的舊版本。嘗試在我們最新的Prerender服務器上使用我們的新Chrome渲染器! –
我在AWS EC實例上託管Prerender。無論如何感謝偉大的產品! :) –