UNPKG

@visulima/pail

Version:

Highly configurable Logger for Node.js, Edge and Browser.

136 lines (129 loc) 3.73 kB
'use strict'; Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: 'Module' } }); const abstractJsonReporter = require('./abstract-json-reporter-okwLqT9k.cjs'); var __defProp$2 = Object.defineProperty; var __name$2 = (target, value) => __defProp$2(target, "name", { value, configurable: true }); class SafeStreamHandler { static { __name$2(this, "SafeStreamHandler"); } #ready = true; #stream; #name; constructor(stream, name) { this.#stream = stream; this.#name = name; } /** * Writes `message` to the instance's internal stream * @param message Message to write */ write(message) { this.writeStream(message); } /** * Calls `end` on this instance's internal stream */ // eslint-disable-next-line @typescript-eslint/no-explicit-any end(...arguments_) { this.#stream.end(...arguments_); } get isReady() { return this.#ready; } writeStream(message) { if (!this.#ready) { console.warn("Stream busy: " + this.#name + '. Write will be dropped: "' + message + '"'); return; } this.#ready = false; this.#stream.on("error", (error) => { throw error; }); this.#stream.on("drain", () => { this.#ready = true; }); this.#stream.on("finish", () => { this.#ready = true; }); this.#ready = this.#stream.write(message, () => { }); } } var __defProp$1 = Object.defineProperty; var __name$1 = (target, value) => __defProp$1(target, "name", { value, configurable: true }); class RotatingFileStream { static { __name$1(this, "RotatingFileStream"); } #filePath; #immediate; #stream; #options; // eslint-disable-next-line @typescript-eslint/no-redundant-type-constituents #createRfsStream; constructor(filePath, writeImmediately = false, options = {}) { this.#filePath = filePath; this.#immediate = writeImmediately; this.#options = options; if (!this.#immediate) { try { this.#createRfsStream = require("rotating-file-stream").createStream; } catch { throw new Error("The 'rotating-file-stream' package is missing. Make sure to install the 'rotating-file-stream' package."); } this.#stream = this.#createRfsStream(this.#filePath, options); } } /** * Writes `message` to the instance's internal #stream * @param message Message to write */ write(message) { let fileStream = this.#stream; if (this.#immediate) { fileStream = this.#createRfsStream(this.#filePath, this.#options); } const stream = new SafeStreamHandler(fileStream, this.#filePath); stream.write(message); if (this.#immediate) { stream.end(); } } /** * Ends the instance's internal #stream * * When `immediate` is not `true`, a call to `write` after calling this method * will throw an error. */ end() { if (this.#stream !== void 0) { this.#stream.end(); } } } var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); class JsonFileReporter extends abstractJsonReporter.AbstractJsonReporter { static { __name(this, "JsonFileReporter"); } stream; constructor(options) { super(); const { filePath, writeImmediately = false, ...rfsOptions } = options; this.stream = new RotatingFileStream(filePath, writeImmediately, { compress: "gzip", // compress rotated files interval: "1d", // rotate daily size: "10M", // rotate every 10 MegaBytes written, ...rfsOptions }); } _log(message) { this.stream.write(message + "\n"); } } exports.JsonFileReporter = JsonFileReporter;