Skip to content

Commit

Permalink
chore(logger): add before log report callback
Browse files Browse the repository at this point in the history
  • Loading branch information
tarotlwei committed Jul 30, 2018
1 parent ceaa05e commit 89f958e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 18 deletions.
15 changes: 4 additions & 11 deletions bin/proxy/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ function wsFiller(ws, req) {
ws.logReportTimer = null;
ws.__tempSend = ws.send;
ws.reportIndex = 1;
ws.messageTriggerCount = 0;

ws.send = function(message) {
if (ws.readyState == WebSocket.OPEN) {
Expand All @@ -52,7 +51,6 @@ function wsFiller(ws, req) {

function emitReportLog(ws, type) {
ws.upgradeReq.emit(type);
ws.messageTriggerCount = 0;
}

function reportWebSocketLog(ws, isEnd) {
Expand All @@ -65,8 +63,6 @@ function reportWebSocketLog(ws, isEnd) {
} else if (logLength > 30) {
// 立即上报
emitReportLog(ws, 'reportLogStream');
} else if (ws.messageTriggerCount > 9) {
emitReportLog(ws, 'reportLogStream');
} else {
ws.logReportTimer = setTimeout(function() {
emitReportLog(ws, 'reportLogStream');
Expand All @@ -90,6 +86,10 @@ function bind_listen(server) {
d.currentContext.window.websocket = ws;
d.currentContext.window.response = {};
d.currentContext.isWebsocket = true;
d.currentContext.beforeLogClean = function() {
logReport.reportLog();
logger.clean();
};

if (config.enableWindow) {
d.currentContext.window.enable();
Expand Down Expand Up @@ -194,7 +194,6 @@ function bind_listen(server) {
};

ws.on('message', function(message) {
ws.messageTriggerCount++;
logger.debug('server get message : ${message}', {
message
});
Expand Down Expand Up @@ -232,7 +231,6 @@ function bind_listen(server) {

req.removeAllListeners('fail');
req.removeAllListeners('reportLog');
req.removeAllListeners('logFilled');

if (d.currentContext) {
d.currentContext.window.websocket = null;
Expand Down Expand Up @@ -265,11 +263,6 @@ function bind_listen(server) {

logReport();
reportWebSocketLog(ws);

req.on('logFilled', function() {
logReport.reportLog();
logger.clean();
});
});
});
}
Expand Down
14 changes: 7 additions & 7 deletions bin/tsw/util/logger/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,14 @@ Logger.prototype = {
}
}

if (log.arr.length > 1000 || (log.json && log.json.ajax && log.json.ajax.length > 30)) {
if (!contextMod.currentContext().emitLogFilled) {
contextMod.currentContext().emitLogFilled = true;
window && window.request && window.request.emit('logFilled');
setTimeout(function() {
contextMod.currentContext().emitLogFilled = false;
}, 1000);
if (log.arr.length % 512 === 0 || (log.json && log.json.ajax && log.json.ajax.length % 10 === 0)) {
const beforeLogClean = contextMod.currentContext().beforeLogClean;
if (typeof beforeLogClean === 'function') {
beforeLogClean();
}
} else if (log.arr.length % 1024 === 0 || (log.json && log.json.ajax && log.json.ajax.length % 20 === 0)) {
process.emit('warning', new Error('too many log'));
this.clean();
}
}
},
Expand Down

0 comments on commit 89f958e

Please sign in to comment.