@deepkush97/logger-ts
Version:
A simple logger implementation with typescript containing two transport method for logging.
34 lines (33 loc) • 1.25 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.FileTransport = void 0;
const fs_1 = require("fs");
const os_1 = require("os");
const path_1 = __importDefault(require("path"));
const util_1 = require("util");
const LogLevels_1 = require("../utils/LogLevels");
class FileTransport {
constructor() {
this.log = (level, data) => {
this._fileStream.write(util_1.format(level, ...data) + os_1.EOL);
};
const logPath = path_1.default.join(process.cwd(), "logs");
if (!fs_1.existsSync(logPath)) {
fs_1.mkdirSync(logPath, {
recursive: true,
});
}
this._fileStream = fs_1.createWriteStream(path_1.default.join(logPath, `log-${new Date()
.toISOString()
.slice(0, 19)
.replace("T", "-")
.replace(/:/g, "-")}.log`), {
flags: "a",
});
this.log(LogLevels_1.LogLevels.INFO, `------------- ${new Date().toISOString()} -------------`);
}
}
exports.FileTransport = FileTransport;