Skip to content

Commit

Permalink
Logger Module
Browse files Browse the repository at this point in the history
add the following code at the top of the other js files in the directory
var poolLogging    = require('./burst-pool-logger');

usage is like this

poolLogging.writeDateLog('Test')

to turn off and on toggle var enabled = false;

do not leave on as can cause problems on large pools due to the time it
takes to open a large file and write to it
  • Loading branch information
SOELexicon authored and SOELexicon committed Dec 26, 2016
1 parent 0eccddc commit 1511d15
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
Empty file added Logs/test.txt
Empty file.
85 changes: 85 additions & 0 deletions burst-pool-logger.js
@@ -0,0 +1,85 @@
var fs = require('fs');

var enabled = false;
var toConsole = false;
var toFile = true;
var toGeneralFile = false;
var generalLogFilename = 'log_general.txt';

function TimeStamp() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + "-" + month + "-" + day + "_" + hour + "-" + min + "-" + sec;
}
function TimeStampFile() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return day+ "-" + month + "-" + year;
}

function write(name,text){
if (enabled){
if (toFile){
fs.appendFile('log_'+name+'.txt', TimeStamp()+' '+text+'\n', function (err) {
if (err) throw err;
});
}
if (toGeneralFile){
fs.appendFile(generalLogFilename, TimeStamp()+' ('+name+') '+text+'\n', function (err) {
if (err) throw err;
});
}
if (toConsole){
console.log('(logger.'+name+") "+text)
}
}
}
function writeDateLog(text){
if (enabled){

if (toFile){
fs.exists('./Logs/log_'+TimeStampFile()+'.txt', function(exists) {
if (exists) {
fs.appendFile('./Logs/log_'+TimeStampFile()+'.txt', TimeStamp()+' '+text+'\n', function (err) {
if (err) throw err;
});
} else {
fs.writeFile('./Logs/log_'+TimeStampFile()+'.txt', TimeStamp()+' '+text+'\n', function(err) {
if(err) {
return console.log(err);
}

// console.log("The file was saved!");
});
}
});

}

}
}
module.exports = {
write : write,
writeDateLog:writeDateLog,
TimeStamp : TimeStamp
};

0 comments on commit 1511d15

Please sign in to comment.