Permalink
Browse files

Logger Module

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...
1 parent 0eccddc commit 1511d15a3fd363c33aadba8807f268a6e47a634e SOELexicon committed Dec 26, 2016
Showing with 85 additions and 0 deletions.
  1. 0 Logs/test.txt
  2. +85 −0 burst-pool-logger.js
View
No changes.
View
@@ -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.