KHD

Logger.js

KHD
Nov 7th, 2014
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var logdir = "./logs/";
  2. var print = console.log.bind(console);
  3. var fs = require('fs');
  4. var rawFile = logdir+"@raw.log";
  5. var EOL = require('os').EOL;
  6.  
  7. var parseServerMsg = function (src, str) { // Goes under Logger['info']
  8.     if (src.substr(0,2) !== "<-" && src.substr(0,2) !== "->") return console.log.call(console, src, str);
  9.     var truestr = str, truesrc = src;
  10.     if (src.length > 2) {
  11.         truesrc = src.substr(0,2);
  12.         truestr = src.substr(2);
  13.     }
  14.     var network = "TestIRC"; // Bot.ServerInfo["NETWORK"] contains the name
  15.     var networkDir = logdir+network+"/"
  16.     if (!fs.existsSync(networkDir)) fs.mkdirSync(networkDir);
  17.     var data = truestr.split(' ');
  18.     var timeStamp = "["+Date().split(' ')[4]+"] ";
  19.     var rawLine = timeStamp+truesrc+" "+truestr+EOL;
  20.     fs.appendFile(rawFile, rawLine); // Log Raw
  21.     if (data[0] === "PING") return;
  22.     var msgFrom;
  23.     if (truesrc === "->") { // Outgoing message (we sent it)
  24.         msgFrom = "(ME)"; // Bot nick goes here
  25.     }
  26.     else msgFrom = data[0].substr(1); // :[email protected]
  27.     var msgType = data[1];
  28.     switch (msgType) {
  29.         case 'NOTICE':
  30.             var notice = true;
  31.         case 'PRIVMSG':
  32.             var msgTo = data[2];
  33.             var isChannel = msgTo.match(/[~&@%\+]?(#.+)/); // Consider using Bot.ServerInfo["PREFIX"] for auth symbols
  34.             var msg = data.slice(3).join(' ').substr(1); // substr removes colon -> :text here
  35.             var logMsg = timeStamp + "<" + msgFrom.split('!')[0] + "> " + msg + EOL;
  36.             var logFile = networkDir + ( isChannel ? isChannel[1] : msgTo ) + ".log"; // TODO: seperate log files by date.
  37.             fs.appendFile(logFile, logMsg);
  38.             break;
  39.     }
  40. }
  41.  
  42. var Logger = function () {
  43.     return {
  44. /*
  45.         debug: print,
  46.         info: print,
  47.         note: print,
  48.         notice: print,
  49.         warning: print,
  50.         crit: print,
  51.         alert: print,
  52.         emerg: print
  53. */
  54.         debug:  function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "debug src: "+src+" str: "+str+EOL); },
  55.         info:   function (src, str) {
  56.             print(src, str);
  57.             fs.appendFile(logdir+"master.log", "info src: "+src+" str: "+str+EOL);
  58.             fs.appendFile(rawFile, "src: "+src+" str: "+str+EOL);
  59.             parseServerMsg(src, str);
  60.         },
  61.         note:   function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "note src: "+src+" str: "+str+EOL); },
  62.         notice: function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "notice src: "+src+" str: "+str+EOL); },
  63.         warning:function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "warning src: "+src+" str: "+str+EOL); },
  64.         crit:   function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "crit src: "+src+" str: "+str+EOL); },
  65.         alert:  function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "alert src: "+src+" str: "+str+EOL); },
  66.         emerg:  function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "emerg src: "+src+" str: "+str+EOL); }
  67.     }
  68. };
  69.  
  70. module.exports = Logger;
Advertisement
Add Comment
Please, Sign In to add comment