mirror of
https://github.com/zulip/zulip-desktop.git
synced 2025-11-14 02:47:49 +00:00
logs: Limit the number of lines in log files.
Limits the numbers of lines in log files to 500. Fixes a part of #727.
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
const NodeConsole = require('console').Console;
|
const NodeConsole = require('console').Console;
|
||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
const os = require('os');
|
||||||
const isDev = require('electron-is-dev');
|
const isDev = require('electron-is-dev');
|
||||||
const { initSetUp } = require('./default-util');
|
const { initSetUp } = require('./default-util');
|
||||||
const { sentryInit, captureException } = require('./sentry-util');
|
const { sentryInit, captureException } = require('./sentry-util');
|
||||||
@@ -30,6 +31,13 @@ class Logger {
|
|||||||
timestamp = this.getTimestamp;
|
timestamp = this.getTimestamp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Trim log according to type of process
|
||||||
|
if (process.type === 'renderer') {
|
||||||
|
requestIdleCallback(() => this.trimLog(file));
|
||||||
|
} else {
|
||||||
|
process.nextTick(() => this.trimLog(file));
|
||||||
|
}
|
||||||
|
|
||||||
const fileStream = fs.createWriteStream(file, { flags: 'a' });
|
const fileStream = fs.createWriteStream(file, { flags: 'a' });
|
||||||
const nodeConsole = new NodeConsole(fileStream);
|
const nodeConsole = new NodeConsole(fileStream);
|
||||||
|
|
||||||
@@ -88,6 +96,24 @@ class Logger {
|
|||||||
reportSentry(err) {
|
reportSentry(err) {
|
||||||
captureException(err);
|
captureException(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trimLog(file) {
|
||||||
|
fs.readFile(file, 'utf8', (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
const MAX_LOG_FILE_LINES = 500;
|
||||||
|
const logs = data.split(os.EOL);
|
||||||
|
const logLength = logs.length - 1;
|
||||||
|
|
||||||
|
// Keep bottom MAX_LOG_FILE_LINES of each log instance
|
||||||
|
if (logLength > MAX_LOG_FILE_LINES) {
|
||||||
|
const trimmedLogs = logs.slice(logLength - MAX_LOG_FILE_LINES);
|
||||||
|
const toWrite = trimmedLogs.join(os.EOL);
|
||||||
|
fs.writeFileSync(file, toWrite);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Logger;
|
module.exports = Logger;
|
||||||
|
|||||||
Reference in New Issue
Block a user