mirror of
https://github.com/nttgin/BGPalerter.git
synced 2024-05-19 06:50:08 +00:00
fixed file rotation issue where empty files were generated
This commit is contained in:
12
package-lock.json
generated
12
package-lock.json
generated
@@ -4785,10 +4785,9 @@
|
||||
}
|
||||
},
|
||||
"moment": {
|
||||
"version": "2.26.0",
|
||||
"resolved": "https://registry.npmjs.org/moment/-/moment-2.26.0.tgz",
|
||||
"integrity": "sha512-oIixUO+OamkUkwjhAVE18rAMfRJNsNe/Stid/gwHSOfHrOtw9EhAY2AHvdKZ/k/MggcYELFCJz/Sn2pL8b8JMw==",
|
||||
"optional": true
|
||||
"version": "2.29.0",
|
||||
"resolved": "https://registry.npmjs.org/moment/-/moment-2.29.0.tgz",
|
||||
"integrity": "sha512-z6IJ5HXYiuxvFTI6eiQ9dm77uE0gyy1yXNApVHqTcnIKfY9tIwEjlzsZ6u1LQXvVgKeTnv9Xm7NDvJ7lso3MtA=="
|
||||
},
|
||||
"ms": {
|
||||
"version": "2.1.2",
|
||||
@@ -5999,6 +5998,11 @@
|
||||
"glob": "^6.0.1"
|
||||
}
|
||||
},
|
||||
"rotating-file-stream": {
|
||||
"version": "2.1.3",
|
||||
"resolved": "https://registry.npmjs.org/rotating-file-stream/-/rotating-file-stream-2.1.3.tgz",
|
||||
"integrity": "sha512-zZ4Tkngxispo7DgiTqX0s4ChLtM3qET6iYsDA9tmgDEqJ3BFgRq/ZotsKEDAYQt9pAn9JwwqT27CSwQt3CTxNg=="
|
||||
},
|
||||
"rpki-validator": {
|
||||
"version": "2.2.2",
|
||||
"resolved": "https://registry.npmjs.org/rpki-validator/-/rpki-validator-2.2.2.tgz",
|
||||
|
@@ -50,9 +50,11 @@
|
||||
"ip-sub": "^1.0.11",
|
||||
"js-yaml": "^3.14.0",
|
||||
"kafka-node": "^5.0.0",
|
||||
"moment": "^2.29.0",
|
||||
"nodemailer": "^6.4.11",
|
||||
"path": "^0.12.7",
|
||||
"restify": "^8.5.1",
|
||||
"rotating-file-stream": "^2.1.3",
|
||||
"rpki-validator": "^2.2.2",
|
||||
"semver": "^7.3.2",
|
||||
"syslog-client": "^1.1.1",
|
||||
|
@@ -271,7 +271,7 @@ config.reports = (config.reports || [])
|
||||
});
|
||||
config.connectors = config.connectors || [];
|
||||
|
||||
config.connectors.push( {
|
||||
config.connectors.push({
|
||||
file: "connectorSwUpdates",
|
||||
name: "upd"
|
||||
});
|
||||
|
@@ -1,11 +1,10 @@
|
||||
const fs = require('fs');
|
||||
const moment = require('moment');
|
||||
const zlib = require('zlib');
|
||||
import fs from "fs";
|
||||
import moment from "moment";
|
||||
import { createStream } from "rotating-file-stream";
|
||||
|
||||
export default class FileLogger {
|
||||
|
||||
constructor(params) {
|
||||
|
||||
this.format = params.format || this.defaultFormat;
|
||||
this.logRotatePattern = params.logRotatePattern || "YYYY-MM-DD";
|
||||
this.filename = params.filename;
|
||||
@@ -17,87 +16,32 @@ export default class FileLogger {
|
||||
this.maxFileSizeMB = parseFloat(params.maxFileSizeMB || 20);
|
||||
this.maxRetainedFiles = parseFloat(params.maxRetainedFiles || 20);
|
||||
|
||||
this.backlog = [];
|
||||
this.staleTimer = null;
|
||||
this.backlogSize = parseFloat(params.backlogSize || 100);
|
||||
|
||||
this.wstream = null;
|
||||
|
||||
|
||||
if (!fs.existsSync(this.directory)){
|
||||
fs.mkdirSync(this.directory);
|
||||
}
|
||||
|
||||
this._currentFile = this.getCurrentFile();
|
||||
};
|
||||
|
||||
getRotatedFileName = (number) => {
|
||||
return this._currentFile + '.' + number + ((this.compressOnRotation) ? '.gz' : '');
|
||||
};
|
||||
|
||||
rotateOldFiles = () => {
|
||||
for (let n=this.maxRetainedFiles; n >= 0; n--) {
|
||||
const fileName = this.getRotatedFileName(n);
|
||||
|
||||
if (fs.existsSync(fileName)) {
|
||||
fs.renameSync(fileName, this.getRotatedFileName(n + 1));
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
applyFileNumberLimit = () => {
|
||||
|
||||
try {
|
||||
|
||||
let files = fs.readdirSync(this.directory)
|
||||
.filter(i => i.indexOf('.log') > 0)
|
||||
.sort((file1, file2) => {
|
||||
const v1 = file1.replace('.gz', '').split('.').pop();
|
||||
const v2 = file2.replace('.gz', '').split('.').pop();
|
||||
return parseInt(v1) - parseInt(v2);
|
||||
});
|
||||
|
||||
if (files.length >= this.maxRetainedFiles - 1) {
|
||||
files = files.slice(this.maxRetainedFiles);
|
||||
files
|
||||
.forEach(file => {
|
||||
fs.unlinkSync(this.directory + '/' + file);
|
||||
});
|
||||
}
|
||||
} catch {
|
||||
// Nothing
|
||||
}
|
||||
};
|
||||
|
||||
hasToBeRotated = () => {
|
||||
const stat = fs.statSync(this._currentFile);
|
||||
const fileSizeInMegabytes = stat.size / 1000000.0;
|
||||
return fileSizeInMegabytes > this.maxFileSizeMB;
|
||||
};
|
||||
|
||||
rotate = () => {
|
||||
this.close();
|
||||
const currentRotatedFile = this.getRotatedFileName(0);
|
||||
const firstRotatedFile = this.getRotatedFileName(1);
|
||||
fs.renameSync(this._currentFile, currentRotatedFile);
|
||||
|
||||
this.rotateOldFiles();
|
||||
const streamOptions = {
|
||||
size: `${this.maxFileSizeMB}M`,
|
||||
interval: "1d"
|
||||
};
|
||||
if (this.compressOnRotation) {
|
||||
fs.writeFileSync(firstRotatedFile, zlib.gzipSync(fs.readFileSync(firstRotatedFile, 'utf8')));
|
||||
streamOptions.compress = "gzip";
|
||||
}
|
||||
this.stream = createStream(this.getCurrentFile, streamOptions);
|
||||
};
|
||||
|
||||
getCurrentFile = (time, index) => {
|
||||
let suffix = "";
|
||||
if (index >= 1) {
|
||||
suffix = `.${index}`;
|
||||
if (this.compressOnRotation) {
|
||||
suffix += ".gz";
|
||||
}
|
||||
}
|
||||
|
||||
this.applyFileNumberLimit();
|
||||
this.open();
|
||||
};
|
||||
|
||||
getCurrentFile = () => {
|
||||
return this.directory + '/' + this.filename.replace("%DATE%", moment().format(this.logRotatePattern));
|
||||
};
|
||||
|
||||
currentFileChanged = () => {
|
||||
const file = this.getCurrentFile();
|
||||
return this._currentFile && this._currentFile !== file;
|
||||
return `${this.directory}/${this.filename.replace("%DATE%", moment().format(this.logRotatePattern))}${suffix}`;
|
||||
};
|
||||
|
||||
defaultFormat = (json) => {
|
||||
@@ -111,59 +55,6 @@ export default class FileLogger {
|
||||
data
|
||||
});
|
||||
|
||||
|
||||
if (this.staleTimer) {
|
||||
clearTimeout(this.staleTimer);
|
||||
delete this.staleTimer;
|
||||
}
|
||||
|
||||
if (this.currentFileChanged()) {
|
||||
this.flush();
|
||||
this.rotate();
|
||||
|
||||
this.backlog.push(item);
|
||||
|
||||
} else {
|
||||
|
||||
this.backlog.push(item);
|
||||
|
||||
if (this.backlog.length >= this.backlogSize) {
|
||||
this.flush();
|
||||
if (this.hasToBeRotated()){
|
||||
this.rotate();
|
||||
}
|
||||
} else {
|
||||
this.staleTimer = setTimeout(this.flushAndClose, 1000);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
this.stream.write(item + "\n");
|
||||
};
|
||||
|
||||
flushAndClose = () => {
|
||||
this.flush();
|
||||
this.close();
|
||||
};
|
||||
|
||||
flush = () => {
|
||||
const string = this.backlog.join('\n') + '\n';
|
||||
this.backlog = [];
|
||||
if (this.wstream === null) {
|
||||
this.open();
|
||||
}
|
||||
fs.appendFileSync(this.wstream, string, 'utf8');
|
||||
};
|
||||
|
||||
open = () => {
|
||||
this._currentFile = this.getCurrentFile();
|
||||
this.wstream = fs.openSync(this._currentFile, 'a');
|
||||
};
|
||||
|
||||
close = () => {
|
||||
if (this.wstream !== null)
|
||||
fs.closeSync(this.wstream);
|
||||
this.wstream = null;
|
||||
}
|
||||
|
||||
};
|
Reference in New Issue
Block a user