2016-02-04 18 views
1

我設法使用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')); 
                  }); 


}; 

但是,沒有什麼被記錄。任何想法爲什麼是這樣的?

回答

0

您可以通過與預渲染選項通過onStdoutonStderr功能對象:

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

你不能把onError的幻象直接,因爲它不是一個真正的幻影的實例,這是一個節點管道輸送到真正的幻影實例。

+0

這會拋出一個'dest.on不是函數'的錯誤從電話 –

+0

對不起,這可能只適用於我們的代碼的舊版本。嘗試在我們最新的Prerender服務器上使用我們的新Chrome渲染器! –

+0

我在AWS EC實例上託管Prerender。無論如何感謝偉大的產品! :) –