applicationinsights
Version:
Microsoft Application Insights module for Node.js
128 lines • 5.15 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileWriter = void 0;
const path = require("path");
const fs = require("fs");
const fileHelpers_1 = require("./fileHelpers");
const util_1 = require("../../../shared/util");
class FileWriter {
// leave at "keep at single file only", "write up to certain size limit", "clear old file on process startup"
constructor(_filepath, _filename, options) {
this._filepath = _filepath;
this._filename = _filename;
this.callback = (_err) => {
// no-op
};
this._ready = false;
this._options = Object.assign(Object.assign({}, FileWriter.DEFAULT_OPTIONS), options);
this._ready = FileWriter.isNodeVersionCompatible() && (0, fileHelpers_1.makeStatusDirs)(this._filepath);
if (this._options.deleteOnExit) {
FileWriter._addCloseHandler();
FileWriter._fullpathsToDelete.push(path.join(this._filepath, this._filename));
}
}
static isNodeVersionCompatible() {
const majVer = process.versions.node.split(".")[0];
return parseInt(majVer) >= 1;
}
log(message) {
if (this._ready) {
const data = typeof message === "object"
? util_1.Util.getInstance().stringify(message)
: message.toString();
// Check if existing file needs to be renamed
this._shouldRenameFile((err, shouldRename) => {
if (err) {
return;
}
if (shouldRename) {
if (this._options.renamePolicy === "rolling") {
(0, fileHelpers_1.renameCurrentFile)(this._filepath, this._filename, (renameErr, renamedFullpath) => {
if (renameErr) {
return;
}
FileWriter._fullpathsToDelete.push(renamedFullpath);
this._options.append
? this._appendFile(`${data}\n`)
: this._writeFile(data);
});
}
else if (this._options.renamePolicy === "overwrite") {
// Clear the current file
this._writeFile(data);
}
else if (this._options.renamePolicy === "stop") {
// Stop future logging
this._ready = false;
}
}
else {
this._options.append
? this._appendFile(`${data}\n`)
: this._writeFile(data);
}
});
}
}
_appendFile(message) {
const fullpath = path.join(this._filepath, this._filename);
fs.appendFile(fullpath, message, (err) => {
this.callback(err);
});
}
_writeFile(message) {
const fullpath = path.join(this._filepath, this._filename);
fs.writeFile(fullpath, message, { mode: this._options.chmod }, this.callback);
}
static _addCloseHandler() {
if (!FileWriter._listenerAttached) {
process.on("exit", () => {
FileWriter._fullpathsToDelete.forEach((filename) => {
try {
fs.unlinkSync(filename);
}
catch (err) { /** ignore errors */ }
});
});
FileWriter._listenerAttached = true;
}
}
_shouldRenameFile(callback) {
const fullpath = path.join(this._filepath, this._filename);
fs.stat(fullpath, (err, stats) => {
if (err) {
if (err.code === "ENOENT" && typeof callback === "function") {
callback(null, false);
}
else if (typeof callback === "function") {
callback(err);
}
return;
}
if (stats.size > this._options.sizeLimit) {
callback(null, true);
}
else {
const createDate = new Date(stats.birthtime);
const currentDate = new Date();
const result = (createDate.getUTCDate() !== currentDate.getUTCDate() ||
createDate.getUTCMonth() !== currentDate.getUTCMonth() ||
createDate.getUTCFullYear() !== currentDate.getUTCFullYear());
callback(null, result);
}
});
}
}
exports.FileWriter = FileWriter;
FileWriter._fullpathsToDelete = [];
FileWriter._listenerAttached = false;
FileWriter.DEFAULT_OPTIONS = {
append: false,
deleteOnExit: true,
sizeLimit: 10 * 1024,
renamePolicy: "stop",
chmod: 0o644 // rw/r/r
};
//# sourceMappingURL=fileWriter.js.map