diff --git a/src/index.js b/src/index.js index 7262b66..d884df6 100644 --- a/src/index.js +++ b/src/index.js @@ -5,7 +5,8 @@ var util = require('util') , moment = require('moment') , fs = require('fs'); -var APP_NAME = "AppName"; +var APP_NAME = "APP_NAME"; +var LOG_PATH = __dirname + "/log"; // Logger information which will be read by the 'overload' function when // assigning the roles of each I/O scribes. @@ -18,34 +19,13 @@ var loggers = { // Prinout to console? console: true, }, - info: { - file: true, - console: true - }, - error: { - file: true, - console: true - }, - warn: { - file: true, - console: true, - }, - realtime: { - file: true, - console: true - }, - high: { - file: true, - console: true - }, - normal: { - file: true, - console: true - }, - low: { - file: true, - console: true - } + info: { file: true, console: true }, + error: { file: true, console: true }, + warn: { file: true, console: true, }, + realtime: { file: true, console: true }, + high: { file: true, console: true }, + normal: { file: true, console: true }, + low: { file: true, console: true } }, colors : { // Console tag colors @@ -60,27 +40,22 @@ var loggers = { }, settings : { maxTagLength : 30, - maxLineWidth : 500, preIndent : 5, - divider : ' : ', + divider : 'W5JZ6WS4GY', defaultTag : '[' + APP_NAME + ']', userName : 'unkown' } }; -var overload = function() { +var logpath = LOG_PATH; - // Additional transports - console.realtime = console.info; - console.high = console.info; - console.normal = console.info; - console.low = console.info; +var createdir = function(){ var userName = process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'].toLowerCase(); loggers.settings.userName = userName.slice(userName.lastIndexOf('\\') + 1); // Create daily logger directory - var fpath = path.join(__dirname, "log".toLowerCase()); + var fpath = path.join(LOG_PATH, "log".toLowerCase()); // Create daily logger directory var dailyPath = path.join(fpath, moment().format('MMM_D_YY').toLowerCase()); @@ -103,8 +78,20 @@ var overload = function() { throw error; } + return userPath; +} + +var overload = function() { + + // Additional transports + console.realtime = console.info; + console.high = console.info; + console.normal = console.info; + console.low = console.info; + // Assign this variable as write directory - fpath = userPath; + var dayiso = moment().isoWeekday(); // FIXME Remove this + var fpath = logpath = createdir(); var _sps = function(sp){ var s = ''; @@ -119,6 +106,14 @@ var overload = function() { // that returns a new function that is used instead of the existing // function. console[m] = (function (i) { + + // Check if dir up to date + var diso = moment().isoWeekday(); + if(dayiso != diso){ + fpath = logpath = createdir(); + dayiso = diso; + } + // Important to overload instead of override. This way we can // actually reuse the old function. var _backup = console[i]; @@ -126,13 +121,9 @@ var overload = function() { // We need to apply any formatting from the variables. // Notice we don't know the number of arguments, so we // use the 'arguments' variable instead. - var utfs = util.format.apply(util, arguments) + var utfs = arguments.length == 1 && typeof arguments[0] === 'object' ? JSON.stringify(arguments[0], null, 4) : util.format.apply(util, arguments).trim() // Moment.js is a great time library for Node.js , mtime = moment().format('MMMM Do YYYY, h:mm:ss A') - // Replace any linebreaks and leading/trailing white - // space. But we attach a line break at the end to show - // each line in the scribe outputs. - , ltfs = i == 'log' ? utfs : utfs.replace(/\n/g, '').trim() // Used to show where the tags are to be placed. // I.e. [Memache] Message goes here. // Coldex holds the index of the first ']' @@ -140,37 +131,19 @@ var overload = function() { // The file we are appending to changes everyday since // its directory changes by date. , file = path.join(fpath, 'app.' + i ) - // Stripped and cleaned string for output to file. - // This is different from the console output which has - // color formatting. This formatting can cause a lot of - // gibberish in textfiles which make it hard to read. - // So that is why we strip it out strings that are going - // to be saved into the file. Read below for more info - // regarding the async/sync formats. - , foutstrAsync = ltfs.stripColors + '\n' - , foutstrSync = utfs.stripColors.replace(/\n/g, '\n' + _sps(mtime.length + loggers.settings.divider.length)) + '\n' // Default file type , options = { encoding : 'utf8' } , fullIndent = _sps(loggers.settings.preIndent + loggers.settings.maxTagLength) , defaultTagPostIndent = _sps(loggers.settings.maxTagLength - loggers.settings.defaultTag.length) , temp = '' , preIndent = _sps(loggers.settings.preIndent); + // Check with logger settings to see if the message should // be outputed to file. Note: Don't append empty lines! - if(loggers.types[i].file && ltfs != '') - // Asynchrounous ouput which should not block existing - // code. Basically, a form of 'process.fork(...)' - if(utfs.length < loggers.settings.maxLineWidth) - // For small data, don't block current process. - // NOTE: Fix to Async and Sync - // 03/16/2013 - // FIXED : Problem was the EACH function which wasn't - // chaining. - // 03/16/2013 - fs.appendFileSync(file, mtime + loggers.settings.divider + foutstrAsync, options, function(err){}); - else - // For large data, block the process when outputting. - fs.appendFileSync(file, mtime + loggers.settings.divider + foutstrSync, options, function(err){}); + if(loggers.types[i].file && utfs != '') + // For large data, block the process when outputting. + fs.appendFileSync(file, mtime + loggers.settings.divider + + utfs.stripColors.replace(/\n/g, '\n' + mtime + loggers.settings.divider) + "\n", options, function(err){}); // Check whether the message should be outputed to console. // Usually, long strings are marked as output ONLY to file // and NOT to console. Short and important messages are @@ -200,11 +173,7 @@ var overload = function() { console.info('\n'); for(var i in loggers.types) { - console[i]('========================================================='[i]); - console[i]((APP_NAME + ' Server 1 Application.%s | Newline.%s')[i], i.toUpperCase() - , [i] == 'log' ? 'DISABLED' : 'ENABLED'); - console[i](moment().format('LLL')[i]); - console[i]('========================================================='[i]); + console[i]('[' + APP_NAME + ']' + 'Started - console.%s', i.toUpperCase()); } console.info('\n'); @@ -215,17 +184,70 @@ var overload = function() { var logger = function(req, res, next) { // Important to see who is accessing what and when // Should be a printout of every access and its respective data. - console.info('[%s]%s %s %s' , req.ip - , req.method.red - , req.url + console.info('[%s]%s | %s %s %s' , "Express" + , req.ip.red + , req.method.green + , req.url.grey , (/mobile/i.test(req.headers['user-agent']) ? 'MOBILE' : 'DESKTOP').yellow); // Continue with the next process in the Express framework next(); } -exports.overload = overload; -exports.logger = logger; +var htmltemplate = ""; +var css = {}; +css['html,body'] = 'margin:0; border:none;float:none;display:block;padding:0;font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif;background:#1c262f;overflow:hidden;'; +css['div'] = 'font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif;font-size:11px;'; +css['html'] = 'background:#2A3946;padding:0;padding-left:50px;' +css['body'] = 'border-left: 1px solid rgba(255, 255, 255, 0.12);'; +css['.log-time'] = 'background: none;color: rgba(255, 255, 255, 0.24);padding:2px 5px;margin-right:2px;margin-left:-1px;font-size:11px;'; +css['.log-type'] = 'background: #633E3E;color: #C20404;padding:2px 5px;margin-right:2px;font-size:11px;'; +css['.log-line'] = 'height:auto;line-height:15px;color:#BEBEBE;font-size:11px;' +css['.button, .selected'] = "font-family:Consolas,Menlo,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New,monospace,serif;background:#2C3338;text-align:center;width:100%;line-height:35px;height:35px;margin-bottom:5px;box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.11);" +css['.selected'] = "background:#4D5961;" +css['a'] = "background:none;text-decoration:none;font-size: 11px;color: #C2C2C2;padding: 10px;" -// Initialize -overload(); \ No newline at end of file +htmltemplate+= "TITLE"; +var buttons = '
' + + '
I
' + + '
L
' + + '
E
' + + '
W
' + + '
Re
' + + '
Hi
' + + '
No
' + + '
Lo
' + + '
' +htmltemplate += '
CONTENT
' + buttons + ''; +htmltemplate = "" + htmltemplate + ""; + +var divRegex = loggers.settings.divider.replace(/([\||\$|\^|\?|\[|\]|\)|\(|\{|\}])/g, '\\$1'); +var regA = new RegExp('^(.*?)(' + divRegex + ")", 'mg'); +var regB = new RegExp('^(.*?)' + divRegex + '\\s{0,}\\[(.*?)\\](.*?)$', 'gm') +var regC = new RegExp('^(.*?)' + divRegex,'mg'); + +var getlog = function(req, res) { + var type = req.param('type'); + type = type ? type : "log"; + var contents = "No log found"; + fs.readFile(logpath + "/app." + type, 'utf8', function(err, data) { + if (!err) contents = data; + // contents = contents.replace(/^(.*?)#\|#/mg, '$1#\|#') + contents = contents.replace(/ /g, ' ') + contents = contents.replace(regA, '$1$2'); + contents = contents.replace(regB, '$1$2$3'); + contents = contents.replace(regC, '$1') + contents = contents.replace(/^(.*?)$/mg, '
$1
') + var response = htmltemplate.replace(type + '_', '').replace("CONTENT", contents).replace("TITLE", APP_NAME + " - " + type); + res.send(response); + }); +} + +exports.init = overload; + +exports.express = { + 'logger' : logger, + 'getlog' : getlog +}