UNPKG

cl-print

Version:

In Version3 , we added color and only print feature

324 lines (257 loc) 11.7 kB
// const chalk = require('chalk'); // const error = new Error(); // console.log(error.stack); // const clc = require('cli-color'); const colors = require('colors'); const path = require('path'); function logLocation(...data) { if (!print_only_handler('cl')) { return; } const error = new Error(); const stackLine = error.stack.split('\n')[2]; // Get caller's frame const lines = path.basename(stackLine).split(':'); const file_path = path.resolve(stackLine); console.log(location_printable(file_path, lines[0], lines[1]).dim, ...data); } const clConfig = { printOnly: false, onlyList: [], silent:false, } function LogLoc(error) { const stackLine = error.stack.split('\n')[2]; // Get caller's frame const lines = path.basename(stackLine).split(':'); const file_path = path.resolve(stackLine); return [file_path, lines[0], lines[1]] } logLocation.silent=(status)=>{ clConfig.silent=status||false; console.log(status); return { exclude:(list)=>{ console.log(list) if (Array.isArray(list)) { clConfig.onlyList = list; } else { throw new Error('only pass array [] argument in .exclude([only array])'.bgRed) } } } } logLocation.silent.exclude=(list)=>{ console.log(list) if (Array.isArray(list)) { clConfig.onlyList = list; } else { throw new Error('only pass array [] argument in .exclude([only array])'.bgRed) } } // only logLocation.only = (list) => { clConfig.printOnly = true; if (Array.isArray(list)) { clConfig.onlyList = list; } else { throw new Error('only pass array [] argument in cl.only([only array])'.bgRed) } } logLocation.only.close = () => { clConfig.printOnly = false; } function print_only_handler (color) { if (clConfig.printOnly) { return clConfig.onlyList.includes(color); } else { return true; } } logLocation.id = (id) => { const error = new Error(); const locations = LogLoc(error); return (...data) => { if (!print_only_handler(id)) { return; } console.log(location_printable(locations[0], locations[1], locations[2]).dim + `[${id}]`.blue, ...data); } } logLocation.help = () => { console.log('\n' + ' CL Console Log Helper '.bgBlue.white.bold + '\n'); console.log('Purpose:'.cyan); console.log('cl is a feature-rich alternative to console.log designed for developers who want better control, colors, locations, and filtering logs.\n'.white); console.log('Basic Usage:'.yellow); console.log('cl("some data") // normal log with file location'.white); console.log('cl.info("info message") // info log in cyan color'.white); console.log('cl.warn("warning message") // warning log in yellow color'.white); console.log('cl.err("error message") // error log in red color'.white); console.log('cl.red("log in red color") // custom colored log'.white); console.log('cl.bgBlue("background blue log") // background colored log'.white); console.log('\n'); console.log('Advance Usage (Control logs):'.yellow); console.log('\n1) cl.only([...])'.cyan); console.log('• Purpose: Control which types or ids of logs will be printed.'.white); console.log('• Usage:'.white); console.log(' cl.only(["info", "warn", "red"]); // only these logs will print'.green); console.log('• This is useful when you want to suppress unwanted logs during development or debugging.'.white); console.log('• You can even pass custom ids defined via cl.id():'.white); console.log(' cl.only(["customID1", "warn", "bgRed"]); // print only logs with these types or ids.\n'.green); console.log('To reset and print all logs again:'.white); console.log(' cl.only.close();'.green); console.log('\n'); console.log('2) cl.id("customID")("your data")'.cyan); console.log('• Purpose: Assign an ID to a specific log.'.white); console.log('• Usage:'.white); console.log(' const logWithId = cl.id("fetchData");'.green); console.log(' logWithId("API call result"); // This log is marked as "fetchData"'.green); console.log('• Now you can control this log using cl.only():'.white); console.log(' cl.only(["fetchData"]); // Only print logs with id "fetchData"'.green); console.log('• This is very helpful when you want to enable/disable specific log points across large projects.\n'.white); console.log('3) Location Tracking:'.cyan); console.log('• All logs automatically print their file name and line number for easy debugging.\n'.white); console.log('4) Colors & Styling:'.cyan); console.log('• Available Text Colors: red, green, yellow, blue, magenta, cyan, white, black.'.white); console.log('• Available Background Colors: bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite, bgBlack.'.white); console.log('• Example: cl.red("Error in this function")'.green); console.log(' cl.bgBlue("Important Section")\n'.green); console.log('5) Highlight:'.cyan); console.log('• cl.highlight("text") adds a special highlight with background blue.'.white); console.log('\nFeature Request / Feedback:'.magenta); console.log('GitHub: @gitkrishnaa'.white); console.log('Email: krishna854192@gmail.com'.white); console.log('Insta: @gitkrishnaa'.white); console.log('X/Twitter: @gitkrishnaa\n'.white); } logLocation.info = function (...data) { if (!print_only_handler('info')) { return; } const error = new Error(); const locations = LogLoc(error); console.info(location_printable(locations[0], locations[1], locations[2]).cyan + 'ℹ️ ', ...data); } logLocation.warn = function (...data) { if (!print_only_handler('warn')) { return; } const error = new Error(); const locations = LogLoc(error); console.info(location_printable(locations[0], locations[1], locations[2]).yellow + '⚠️ ', ...data); } logLocation.err = function (...data) { if (!print_only_handler('err')) { return; } const error = new Error(); const locations = LogLoc(error); console.info(location_printable(locations[0], locations[1], locations[2]).red + '❌', ...data); } logLocation.error = function (...data) { if (!print_only_handler('error')) { return; } const error = new Error(); const locations = LogLoc(error); console.info(location_printable(locations[0], locations[1], locations[2]).red + '❌', ...data); } logLocation.debug = function (...data) { if (!print_only_handler('error')) { return; } const error = new Error(); const locations = LogLoc(error); console.info(location_printable(locations[0], locations[1], locations[2]).red + '🛠️ ', ...data); } // colors logLocation.red = function (...data) { if (!print_only_handler('red')) { return; } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).red, ...data); } logLocation.green = function (...data) { if (!print_only_handler('green')) { return; } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).green, ...data); } logLocation.yellow = function (...data) { if (!print_only_handler('yellow')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).yellow, ...data); } logLocation.blue = function (...data) { if (!print_only_handler('blue')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).blue, ...data); } logLocation.magenta = function (...data) { if (!print_only_handler('magenta')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).magenta, ...data); } logLocation.cyan = function (...data) { if (!print_only_handler('cyan')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).cyan, ...data); } logLocation.white = function (...data) { if (!print_only_handler('white')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).white, ...data); } logLocation.black = function (...data) { if (!print_only_handler('black')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).black, ...data); } logLocation.bgRed = function (...data) { if (!print_only_handler('bgRed')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgRed, ...data); } logLocation.bgGreen = function (...data) { if (!print_only_handler('bgGreen')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgGreen, ...data); } logLocation.bgYellow = function (...data) { if (!print_only_handler('bgYellow')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgYellow, ...data); } logLocation.bgBlue = function (...data) { if (!print_only_handler('bgBlue')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgBlue, ...data); } logLocation.bgMagenta = function (...data) { if (!print_only_handler('bgMagenta')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgMagenta, ...data); } logLocation.bgCyan = function (...data) { if (!print_only_handler('bgCyan')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgCyan, ...data); } logLocation.bgWhite = function (...data) { if (!print_only_handler('bgWhite')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgWhite, ...data); } logLocation.bgBlack = function (...data) { if (!print_only_handler('bgBlack')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2]).bgBlack, ...data); } logLocation.highlight = function (...data) { if (!print_only_handler('highlight')) { return } const error = new Error(); const locations = LogLoc(error); console.log(location_printable(locations[0], locations[1], locations[2], '>>>>>').bgBlue, ...data); } module.exports.cl = logLocation function location_printable(location, filePath, line = 1, extratext = '') { const absolutePath = path.resolve(location); // console.log() let arr = absolutePath.split(' ') const filename = path.basename(filePath) // console.log("absolutePath",absolutePath,"filename",filename) return `\u001b]8;;${arr[arr.length - 1]}\u0007[${filename}:${line}]\u001b]8;;\u0007` + extratext; }