UNPKG

@lvksh/logger

Version:

Zero dependency, light-weight, blazing fast customizable logging library.

32 lines (31 loc) 1.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.FileLogger = void 0; const node_fs_1 = require("node:fs"); const node_path_1 = require("node:path"); const ansi_1 = require("./ansi"); const FileLogger = (config) => { let { path } = config; const { mode } = config; // If we are creating a new file generate the file name if (mode == 'NEW_FILE') { const { namePattern } = config; const calculatedPattern = namePattern; path = (0, node_path_1.join)(path, calculatedPattern); } // Ensure the parent folder exists const folder = (0, node_path_1.resolve)(path, '..'); if (!(0, node_fs_1.existsSync)(folder)) { (0, node_fs_1.mkdirSync)(folder, { recursive: true }); } // Create the stream to write out to const stream = (0, node_fs_1.createWriteStream)(path, { encoding: 'utf-8', autoClose: true, }); // Return the executable logging function return (input) => { stream.write((0, ansi_1.stripAnsi)(input) + '\n'); }; }; exports.FileLogger = FileLogger;