设置prerender.io只输出PhantomJS的错误消息

时间:2016-02-04 10:06:09

标签: javascript phantomjs prerender

我已经设法使用logger插件为PhantomJS浏览器打开prerender.io的日志记录。但是,我想只记录错误消息。 logger插件只记录所有内容。基于以下页面:

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'));
                                                            });


};

但是,没有记录任何内容。任何想法为什么会这样?

1 个答案:

答案 0 :(得分:0)

您可以使用prerender选项对象传递onStdoutonStderr函数:

var server = prerender({
    workers: 1,
    iterations: 50,
    onStdout: function(data) {
        console.log(data);
    },
    onStderr: function(data) {
        console.log(data);
    }
});

你不能直接在幻像上放置onError,因为它不是一个真正的幻像实例,它是真正的phantomjs实例的节点管道。