logifys
Version:
Let's make logs easier!
94 lines (81 loc) • 2.64 kB
JavaScript
const fs = require('fs');
const https = require('https');
const log = (message, options = {}) => {
const {
font = 'normal',
color = 'white',
backgroundColor,
size = 'normal',
json = false,
file
} = options;
https.get('https://raw.githubusercontent.com/tyler-Github/logify/main/colorCodes.json', (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
const colorCodes = JSON.parse(data);
let colorCode = colorCodes[color];
let backgroundColorCode;
if (!colorCode) {
colorCode = colorCodes['white'];
}
if (backgroundColor) {
backgroundColorCode = colorCodes[backgroundColor];
if (!backgroundColorCode) {
backgroundColorCode = colorCodes['black'];
}
}
let fontCode;
switch (font) {
case 'bold':
fontCode = '\x1b[1m';
break;
case 'italic':
fontCode = '\x1b[3m';
break;
case 'underline':
fontCode = '\x1b[4m';
break;
default:
fontCode = '\x1b[0m';
}
let sizeCode = typeof size === 'number' ? `\x1b[${size};m` : `\x1b[${size}m`;
setTimeout(() => {
let logMessage = `${fontCode}\x1b[38;2;${colorCode[0]};${colorCode[1]};${colorCode[2]}m[LOGIFYS]: ${message}\x1b[0m${sizeCode}`;
if (backgroundColorCode) {
logMessage = `${fontCode}\x1b[48;2;${backgroundColorCode[0]};${backgroundColorCode[1]};${backgroundColorCode[2]}m${convertRGBtoANSI(colorCode)}[LOGIFYS]: ${message}\x1b[0m${sizeCode}`;
}
console.log(logMessage);
if (file) {
if (json) {
const logEntry = {
timestamp: new Date().toLocaleString(),
message,
font,
color,
backgroundColor,
size
};
fs.appendFile(file, `${JSON.stringify(logEntry)}\n`, (error) => {
if (error) {
console.error(`Error logging to file: ${error}`);
}
});
} else {
fs.appendFile(file, `${ new Date().toLocaleString() }[LOGIFYS]: ${ message }\n`, (error) => {
if (error) {
console.error(`Error logging to file: ${error}`);
}
});
}
}
}, 100);
});
});
};
function convertRGBtoANSI(rgb) {
return `\x1b[38;2;${rgb[0]};${rgb[1]};${rgb[2]}m`;
}
module.exports = log;