Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var logdir = "./logs/";
- var print = console.log.bind(console);
- var fs = require('fs');
- var rawFile = logdir+"@raw.log";
- var EOL = require('os').EOL;
- var parseServerMsg = function (src, str) { // Goes under Logger['info']
- if (src.substr(0,2) !== "<-" && src.substr(0,2) !== "->") return console.log.call(console, src, str);
- var truestr = str, truesrc = src;
- if (src.length > 2) {
- truesrc = src.substr(0,2);
- truestr = src.substr(2);
- }
- var network = "TestIRC"; // Bot.ServerInfo["NETWORK"] contains the name
- var networkDir = logdir+network+"/"
- if (!fs.existsSync(networkDir)) fs.mkdirSync(networkDir);
- var data = truestr.split(' ');
- var timeStamp = "["+Date().split(' ')[4]+"] ";
- var rawLine = timeStamp+truesrc+" "+truestr+EOL;
- fs.appendFile(rawFile, rawLine); // Log Raw
- if (data[0] === "PING") return;
- var msgFrom;
- if (truesrc === "->") { // Outgoing message (we sent it)
- msgFrom = "(ME)"; // Bot nick goes here
- }
- var msgType = data[1];
- switch (msgType) {
- case 'NOTICE':
- var notice = true;
- case 'PRIVMSG':
- var msgTo = data[2];
- var isChannel = msgTo.match(/[~&@%\+]?(#.+)/); // Consider using Bot.ServerInfo["PREFIX"] for auth symbols
- var msg = data.slice(3).join(' ').substr(1); // substr removes colon -> :text here
- var logMsg = timeStamp + "<" + msgFrom.split('!')[0] + "> " + msg + EOL;
- var logFile = networkDir + ( isChannel ? isChannel[1] : msgTo ) + ".log"; // TODO: seperate log files by date.
- fs.appendFile(logFile, logMsg);
- break;
- }
- }
- var Logger = function () {
- return {
- /*
- debug: print,
- info: print,
- note: print,
- notice: print,
- warning: print,
- crit: print,
- alert: print,
- emerg: print
- */
- debug: function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "debug src: "+src+" str: "+str+EOL); },
- info: function (src, str) {
- print(src, str);
- fs.appendFile(logdir+"master.log", "info src: "+src+" str: "+str+EOL);
- fs.appendFile(rawFile, "src: "+src+" str: "+str+EOL);
- parseServerMsg(src, str);
- },
- note: function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "note src: "+src+" str: "+str+EOL); },
- notice: function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "notice src: "+src+" str: "+str+EOL); },
- warning:function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "warning src: "+src+" str: "+str+EOL); },
- crit: function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "crit src: "+src+" str: "+str+EOL); },
- alert: function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "alert src: "+src+" str: "+str+EOL); },
- emerg: function (src, str) { print(src, str); fs.appendFile(logdir+"master.log", "emerg src: "+src+" str: "+str+EOL); }
- }
- };
- module.exports = Logger;
Advertisement
Add Comment
Please, Sign In to add comment