2013-11-09 2 views
14

어떤 종류의 오류 메시지도 로깅하지 않고 지속적으로 충돌하는 Node.js 서버가 있습니다. 이것은 전형적인 시나리오입니까? 충돌을 일으키기 전에 오류를 잡아서 어떻게 기록 할 수 있습니까?Node.js 서버가 오류 메시지없이 충돌 함

+1

에서보고, 당신은 확인할 수 있습니다 프로세스에서 메모리가 부족한 경우 –

+1

서버의 메모리 사용량을 확인하는 방법을 권장 할 수 있습니까? –

+0

2 년 전 내가 쉘 서버에서 노드 서버를 실행하고 stderr를 캡처하는 방법을 사용했습니다. V8은 비정상적으로 종료되고 오류 메시지를 stderr에 기록합니다. 그러나 V8은 많이 바뀌었고 그 기술이 더 이상 효과가 있는지는 알 수 없습니다. 오랜 시간이 지난 후에 충돌이 발생하면 프로세스 메모리를 모니터링하면 도움이 될 수 있습니다. –

답변

28

좋은 시작은 서버, 세부 사항을 기록 예외에 대한 핸들러 리스너를 설정하기 전에, 특히 생산, 설치하는 것입니다. here 봐 : 당신이 Express.js를 사용하는 경우

process.on('uncaughtException', function (exception) { 
    console.log(exception); // to see your exception details in the console 
    // if you are on production, maybe you can send the exception details to your 
    // email as well ? 
}); 

은, 당신이 경우 이메일로 보내, 다시, 당신의 오류의 전체 스택을 참조 (결국 방법을 알고 here에서 살펴 생산). 이 경우, 리스너를 인스턴스화하기 전에 당신에게 자세한 내용을 제공하도록 지시 :

var express = require('express'); 
// ... 
var app = express(); 
// ... 
app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); 
// then, set the listener and do your stuff... 
+6

'errorHandler' 미들웨어는 더 이상 명시 적으로 번들로 제공되지 않습니다. 'npm install errorhandler'를 실행하여 설치할 수 있습니다 - 자세한 내용은 https://github.com/expressjs/errorhandler를 참조하십시오. – muchweb

4

이 @matteofigus 답을 완료하려면, 당신은 또한 listen for unhandled promise rejections 수 있습니다.

process.on('unhandledRejection', (reason, p) => { 
    console.log("Unhandled Rejection at: Promise ", p, " reason: ", reason); 
    // application specific logging, throwing an error, or other logic here 
}); 

somePromise.then((res) => { 
    return reportToUser(JSON.pasre(res)); // note the typo (`pasre`) 
}); // no `.catch` or `.then` 
0

당신은 당신이 모든 오류 예외 로그를 ​​유지할 수 있습니다 '로거'(log4js-NPM-패키지)와 'ErrorHandler를'&라는 미들웨어를 사용할 수 있습니다.

// 미들웨어 : 여기 ErrorHandler를위한 코드의 캐치 - 모든 오류 처리기를. 그래서 우리는 오류를 기록하지만 클라이언트에 내부 오류 세부 사항이 누설되지는 않습니다.

app.use(errorHandler); 

function errorHandler(err, req, res, next) { 

// XHR Request? 
if (req.xhr) { 
    logger.error(err); 
    res.status(500).send({ error: 'Internal Error Occured.' }); 
    return; 
} 

// Not a XHR Request. 
logger.error(err); 
res.status(500); 
res.render('framework/error', { error: "Internal Server Error." }); 

// Note: No need to call next() as the buck stops here. 
return; 
} 
0

노드 v6.11.0, 윈도우 (10)

는 아무 소용이 여기에 다른 제안을 시도 - 응용 프로그램은 중지 오류도 아래로

process.on('uncaughtException',...) 
process.on('unhandledRejection',....) 

마지막으로 추적 종료/충돌

를 사용하지 재귀 함수 호출. 다음 코드는 문제를 보여줍니다.

"use strict" ; 

process.on('uncaughtException', function (exception) { 
    console.log(exception); 
}); 

var count = 0 ; 
function recursiveFunction(){ 
    console.log(count++); 
    recursiveFunction(); 
} 
recursiveFunction() ; 

이것은 지금까지 멈출 것입니다. Try/Catch가 작동하지 않았다. 위와 같이 시도했다.

function recursiveFunction(){ 
    console.log(count++); 
    try{ 
     recursiveFunction(); 
    } 
    catch(e){ 
     console.log("recursion error"); 
    } 
} 

다시 한번 아무 것도 없습니다.

(코드를 재 설계 할 필요없이) 해결 방법 (재귀 과정을 피하기 위해) setImmediate를 사용하는 것;

function recursiveFunction(){ 
    console.log(count++); 
    setImmediate(recursiveFunction); 
} 

(I 결국이 그것을 중지-c'd CTRL.)

가 충돌의 원인을 찾기 위해 캐치되지 않는 예외를 기록하는 것 외에도 node github issues

관련 문제