Last active
July 11, 2022 12:39
-
-
Save itzarty/3d2c9653e74d0de1975ce3ed88e53227 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Requires prototype found at https://gist.github.com/ItzArty/96e7439a63ac3800335ecb72a63d255c | |
console.clear( ); | |
if( !fs.existsSync('./logs') ) fs.mkdirSync('./logs'); | |
var d = new Date( ); | |
var logName = d.getDate( ).placeFormat( 2 ) + '-' + ( d.getMonth( ) + 1 ).placeFormat( 2 ) + '-' + d.getFullYear( ); | |
var logNameFound = true; | |
var logNameInc = 1; | |
var logFileName = logName + '.log'; | |
if( fs.existsSync( './logs/' + logName + '.log' ) ) logNameFound = false; | |
while( logNameFound == false ) { | |
if( !fs.existsSync( './logs/' + logName + '-' + logNameInc + '.log' ) ) { | |
logFileName = logName + '-' + logNameInc + '.log'; | |
logNameFound = true; | |
}else{ | |
logNameInc++; | |
} | |
} | |
fs.writeFile( './logs/' + logFileName, 'New session started...', ( err ) => { | |
if( err ) { | |
console.error('Unable to create log file, exitting...'); | |
console.log( err ); | |
process.exit( ); | |
} | |
}); | |
function log( message, type ) { | |
if( !type ) type = "info"; | |
var prefix, suffix; | |
var d = new Date( ); | |
const date = d.getDate( ).placeFormat( 2 ) + "/" + ( d.getMonth( ) + 1 ).placeFormat( 2 ) + "/" + d.getFullYear( ) + " " + d.getHours( ).placeFormat( 2 ) + ":" + d.getMinutes( ).placeFormat( 2 ) + ":" + d.getSeconds( ).placeFormat( 2 ); | |
const prefixes = { | |
default: "Info", | |
info: "Info", | |
warning: "Warn", | |
error: "Error" | |
} | |
console.log( date + " [ " + type.switch( prefixes ).toUpperCase( ) + "\x1b[0m ]: " + message + "\x1b[0m" ); | |
fs.appendFile( './logs/' + logFileName, '\n' + date + " [ " + type.switch( prefixes ).toUpperCase( ) + " ]: " + message, ( err ) => { | |
if( err ) console.error('Unable to append to the log file!'); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment