applicationinsights
Version:
Microsoft Application Insights module for Node.js
151 lines • 6.23 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileWriter = void 0;
const path = __importStar(require("path"));
const fs = __importStar(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