From: Ben Beltran Date: Thu, 20 Mar 2014 16:27:45 +0000 (-0600) Subject: Cobalt 2.0 -> Now with neon 2.0 X-Git-Tag: v1.1.0~15 X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/commitdiff_plain/db6bc3cb0117030b2d31d55850d91f58248a8e81 Cobalt 2.0 -> Now with neon 2.0 --- diff --git a/CHANGELOG b/CHANGELOG new file mode 100644 index 0000000..0a2c8a5 --- /dev/null +++ b/CHANGELOG @@ -0,0 +1,12 @@ +2014-03-20 Ben Beltran + + Version 1.0.0 + + * lib/cobalt.js (buildLog): logging a logObject will now use the calling + method's severity instead of the logObject severity. so calling `#error` + will always provide severity level 3. + + * package.json: Updates neon dependency to 2.0, adds tellurium as + dev-dependency. + + * /test: Adds VERY basic tests before tellurium is integrated. diff --git a/lib/cobalt.js b/lib/cobalt.js index fa4f623..9519bed 100644 --- a/lib/cobalt.js +++ b/lib/cobalt.js @@ -1,330 +1,315 @@ -(function (global) { - var Module, Class; - - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - Class = Ne.Class; - var Microtime = require('microtime'); - } else { - Module = global.Module; - Class = global.Class; +// Load up dependencies +if (typeof require === 'function') { + require('neon'); + var Microtime = require('microtime'); +} + +Cobalt = Module("Cobalt"); +Module(Cobalt, 'Logger')({}); +Module(Cobalt, 'Formatter')({}); + +// Load up loggers + formatters +if (typeof require === 'function') { + // Formatters + require('./formatters/token.js'); + + // Loggers + require('./loggers/console.js'); + require('./loggers/socket.js'); +} + +Cobalt.now = function () { + if (typeof performance !== 'undefined' && performance.timing) { + return performance.timing.navigationStart + performance.now(); } - var Cobalt = {}; - Module(Cobalt, 'Logger')({}); - Module(Cobalt, 'Formatter')({}); - - // Load up loggers + formatters - if (typeof require === 'function') { - // Formatters - Cobalt.Formatter.Token = require('./formatters/token.js').Token; - - // Loggers - Cobalt.Logger.JsConsole = require('./loggers/console.js').JsConsole; - Cobalt.Logger.Socket = require('./loggers/socket.js').Socket; + if (typeof Microtime !== 'undefined') { + return Microtime.nowDouble() * 1000; } - Cobalt.now = function () { - if (typeof performance !== 'undefined' && performance.timing) { - return performance.timing.navigationStart + performance.now(); - } + return Date.now(); +} - if (typeof Microtime !== 'undefined') { - return Microtime.nowDouble() * 1000; +// Stringify with circular dereference. +Cobalt.stringify = function (object) { + var cache = [], stringified; + stringified = JSON.stringify(object, function (key, value) { + if (typeof value === 'object' && value !== null) { + if (cache.indexOf(value) !== -1) { + return "[Circular]"; + } + cache.push(value); } - - return Date.now(); - } - - // Stringify with circular dereference. - Cobalt.stringify = function (object) { - var cache = [], stringified; - stringified = JSON.stringify(object, function (key, value) { - if (typeof value === 'object' && value !== null) { - if (cache.indexOf(value) !== -1) { - return "[Circular]"; + return value; + }); + cache = null; + + return stringified; +} + +Class(Cobalt, 'Console')({ + prototype : { + from : "Generic Cobalt Logger", + version : "0.1.0", + currentIndent : 0, + indentSize : 2, + loggers : [], + separatorLength : 120, + currentColor : "black", + + // Initialize instance of cobalt console + // and extend configuration. + init : function (config) { + var co = this, + property; + + if (config) { + for (property in config) { + co[property] = config[property]; } - cache.push(value); } - return value; - }); - cache = null; - - return stringified; - } + }, + + addLogger : function (logger) { + this.loggers.push(logger); + }, + + removeLogger : function (logger) { + var index; + + index = this.loggers.indexOf(logger); + this.loggers.splice(index, 1); + }, + + // Builds a Cobalt Log Object + buildLog : function (item, level) { + var co = this, oldItem, logObject = {}; + + if (typeof item === "undefined" || item === null || !item._cobaltLog) { + logObject.message = item; + logObject._cobaltLog = true; + logObject._from = co.from; + logObject._level = level || 6; + logObject._levelString = co._levelString(logObject._level); + logObject._version = co.version; + logObject._timestamp = co.now(); + logObject._indentLevel = co.currentIndent; + logObject._color = co.currentColor; + logObject._separator = false; + return logObject; + } - Cobalt.Console = Class(Cobalt, 'Console')({ - prototype : { - from : "Generic Cobalt Logger", - version : "0.1.0", - currentIndent : 0, - indentSize : 2, - loggers : [], - separatorLength : 120, - currentColor : "black", - - // Initialize instance of cobalt console - // and extend configuration. - init : function (config) { - var co = this, - property; - - if (config) { - for (property in config) { - co[property] = config[property]; - } - } - }, - - addLogger : function (logger) { - this.loggers.push(logger); - }, - - removeLogger : function (logger) { - var index; - - index = this.loggers.indexOf(logger); - this.loggers.splice(index, 1); - }, - - // Builds a Cobalt Log Object - buildLog : function (item, level) { - var co = this, oldItem, logObject = {}; - - if (typeof item === "undefined" || item === null || !item._cobaltLog) { - logObject.message = item; - logObject._cobaltLog = true; - logObject._from = co.from; - logObject._level = level || 6; - logObject._levelString = co._levelString(logObject._level); - logObject._version = co.version; - logObject._timestamp = co.now(); - logObject._indentLevel = co.currentIndent; - logObject._color = co.currentColor; - logObject._separator = false; - return logObject; - } + if (item._cobaltLog) { + item._level = level || item._level || 6; + item._levelString = co._levelString(item._level); + } - return item; - }, + return item; + }, - extendLog : function (extendingObject) { - var co = this, logObject, - property; + extendLog : function (extendingObject) { + var co = this, logObject, + property; - logObject = co.buildLog(undefined, 6); - extendingObject = extendingObject || {}; + logObject = co.buildLog(undefined, 6); + extendingObject = extendingObject || {}; - for (property in extendingObject) { - if (extendingObject.hasOwnProperty(property)) { - logObject[property] = extendingObject[property]; - } + for (property in extendingObject) { + if (extendingObject.hasOwnProperty(property)) { + logObject[property] = extendingObject[property]; } + } - return logObject; - }, - - buildSeparator : function (type) { - var co = this; - return { - _cobaltLog : true, - _separator : true, - _version : co.version, - _timestamp : co.now(), - _separatorType : type, - _indentLevel : co.currentIndent, - _color : co.currentColor - } - }, - - _log : function (severity) { - var co = this, - logString, - logObjectArray = [], - i, j; - - for (i = 1; i < arguments.length; i++) { - if (typeof arguments[i] === 'undefined') { - logObjectArray.push(co.buildLog("undefined", severity)); - } else { - logObjectArray.push(co.buildLog(arguments[i], severity)); - } - } + return logObject; + }, + + buildSeparator : function (type) { + var co = this; + return { + _cobaltLog : true, + _separator : true, + _version : co.version, + _timestamp : co.now(), + _separatorType : type, + _indentLevel : co.currentIndent, + _color : co.currentColor + } + }, - for (j = 0; j < co.loggers.length; j++) { - co.loggers[j].log.apply(co.loggers[j], logObjectArray); - } - }, + _log : function (severity) { + var co = this, + logString, + logObjectArray = [], + i, j; - log : function () { - this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); - }, + for (i = 1; i < arguments.length; i++) { + if (typeof arguments[i] === 'undefined') { + logObjectArray.push(co.buildLog("undefined", severity)); + } else { + logObjectArray.push(co.buildLog(arguments[i], severity)); + } + } - debug : function () { - this._log.apply(this, [7].concat(Array.prototype.slice.call(arguments))); - }, + for (j = 0; j < co.loggers.length; j++) { + co.loggers[j].log.apply(co.loggers[j], logObjectArray); + } + }, - info : function () { - this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); - }, + log : function () { + this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); + }, - notice : function () { - this._log.apply(this, [5].concat(Array.prototype.slice.call(arguments))); - }, + debug : function () { + this._log.apply(this, [7].concat(Array.prototype.slice.call(arguments))); + }, - warn : function () { - this._log.apply(this, [4].concat(Array.prototype.slice.call(arguments))); - }, + info : function () { + this._log.apply(this, [6].concat(Array.prototype.slice.call(arguments))); + }, - error : function () { - this._log.apply(this, [3].concat(Array.prototype.slice.call(arguments))); - }, + notice : function () { + this._log.apply(this, [5].concat(Array.prototype.slice.call(arguments))); + }, - dir : function () { - }, + warn : function () { + this._log.apply(this, [4].concat(Array.prototype.slice.call(arguments))); + }, - time : function () { - }, + error : function () { + this._log.apply(this, [3].concat(Array.prototype.slice.call(arguments))); + }, - timeEnd : function () { - }, + dir : function () { + }, - groupCollapsed : function () { - }, + time : function () { + }, - groupEnd : function () { - }, + timeEnd : function () { + }, - separator : function (type) { - var co = this; + groupCollapsed : function () { + }, - co._log(7, co.buildSeparator(type)); - }, + groupEnd : function () { + }, - space : function (lines) { - var co = this, - i; + separator : function (type) { + var co = this; - if (typeof lines === "undefined") { - lines = 1; - } + co._log(7, co.buildSeparator(type)); + }, - for (i = 0; i < lines; i++) { - co.log(' '); - } + space : function (lines) { + var co = this, + i; - return co; - }, + if (typeof lines === "undefined") { + lines = 1; + } - indent : function (callback) { - var co = this; + for (i = 0; i < lines; i++) { + co.log(' '); + } - if (typeof callback === "function") { - co.currentIndent = co.currentIndent + co.indentSize; - callback(); - co.currentIndent = co.currentIndent - co.indentSize; - } else { - co.currentIndent = co.currentIndent + co.indentSize; - } + return co; + }, - return co; - }, + indent : function (callback) { + var co = this; - outdent : function (callback) { - var co = this; + if (typeof callback === "function") { + co.currentIndent = co.currentIndent + co.indentSize; + callback(); + co.currentIndent = co.currentIndent - co.indentSize; + } else { + co.currentIndent = co.currentIndent + co.indentSize; + } - if (typeof callback === "function") { - co.currentIndent = co.currentIndent - co.indentSize; - if (co.currentIndent < 0) { - co.currentIndent = 0; - } + return co; + }, - callback(); + outdent : function (callback) { + var co = this; - co.currentIndent = co.currentIndent + co.indentSize; - } else { - co.currentIndent = co.currentIndent - co.indentSize; - if (co.currentIndent < 0) { - co.currentIndent = 0; - } + if (typeof callback === "function") { + co.currentIndent = co.currentIndent - co.indentSize; + if (co.currentIndent < 0) { + co.currentIndent = 0; } - return co; - }, + callback(); - color : function (color, callback) { - var co = this, - oldColor = co.currentColor; - - if (typeof callback === "function") { - co.currentColor = color; - callback(); - co.currentColor = oldColor; - } else { - co.currentColor = color; + co.currentIndent = co.currentIndent + co.indentSize; + } else { + co.currentIndent = co.currentIndent - co.indentSize; + if (co.currentIndent < 0) { + co.currentIndent = 0; } + } - return co; - }, + return co; + }, - // Returns the current time in microseconds. - now : function () { - if (typeof performance !== 'undefined' && performance.timing) { - return performance.timing.navigationStart + performance.now(); - } + color : function (color, callback) { + var co = this, + oldColor = co.currentColor; - if (typeof Microtime !== 'undefined') { - return Microtime.nowDouble() * 1000; - } + if (typeof callback === "function") { + co.currentColor = color; + callback(); + co.currentColor = oldColor; + } else { + co.currentColor = color; + } - return Date.now(); - }, - - _levelString : function (level) { - switch(level) { - case 0: - return "PANIC"; - break; - case 1: - return "ALERT" - break; - case 2: - return "CRIT" - break; - case 3: - return "ERROR" - break; - case 4: - return "WARN" - break; - case 5: - return "NOTICE" - break; - case 6: - return "INFO" - break; - case 7: - return "DEBUG" - break; - } + return co; + }, + + // Returns the current time in microseconds. + now : function () { + if (typeof performance !== 'undefined' && performance.timing) { + return performance.timing.navigationStart + performance.now(); } - } - }); - if (Cobalt.Console.__objectSpy) { - Cobalt.Console.__objectSpy.destroy(); - } + if (typeof Microtime !== 'undefined') { + return Microtime.nowDouble() * 1000; + } - if (typeof require === 'function') { - global.Formatter = Cobalt.Formatter; - global.Logger = Cobalt.Logger; - global.Console = Cobalt.Console; - global.now = Cobalt.now; - global.stringify = Cobalt.stringify; - } else { - global.Cobalt = Cobalt; + return Date.now(); + }, + + _levelString : function (level) { + switch(level) { + case 0: + return "PANIC"; + break; + case 1: + return "ALERT" + break; + case 2: + return "CRIT" + break; + case 3: + return "ERROR" + break; + case 4: + return "WARN" + break; + case 5: + return "NOTICE" + break; + case 6: + return "INFO" + break; + case 7: + return "DEBUG" + break; + } + } } +}); -}(typeof window !== 'undefined' ? window : (typeof module.exports !== 'undefined' ? module.exports : self))); +if (Cobalt.Console.__objectSpy) { + Cobalt.Console.__objectSpy.destroy(); +} diff --git a/lib/formatters/ansi.js b/lib/formatters/ansi.js index 25ad256..806869a 100644 --- a/lib/formatters/ansi.js +++ b/lib/formatters/ansi.js @@ -1,47 +1,29 @@ -(function (global) { - var Module; - - // Load up dependencies - if (typeof require === 'function') { +if (typeof require === 'function') { require('colors'); - var Ne = require('neon'); - Module = Ne.Module; - } else { - Module = global.Module; - } +} - var Cobalt = {}; - Module(Cobalt, 'Formatter')({}); +Module(Cobalt.Formatter, 'Ansi')({ + format : function (logObject, opts){ + var indent, + message; - Cobalt.Formatter.Ansi = Module(Cobalt.Formatter, 'Ansi')({ - format : function (logObject, opts){ - var indent, - message; + indent = Array(logObject._indentLevel + 1).join(' '); - indent = Array(logObject._indentLevel + 1).join(' '); + message = indent + logObject.message; - message = indent + logObject.message; - - switch(logObject._level) { - case 0: - case 1: - case 2: - case 3: - return message.red; - case 4: - return message.yellow; - case 5: - case 6: - return message.blue; - default: - return message; - } + switch(logObject._level) { + case 0: + case 1: + case 2: + case 3: + return message.red; + case 4: + return message.yellow; + case 5: + case 6: + return message.blue; + default: + return message; } - }); - - if (typeof require === 'function') { - global.Ansi = Cobalt.Formatter.Ansi; - } else { - global.Cobalt.Formatter.Ansi = Cobalt.Formatter.Ansi; } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +}); diff --git a/lib/formatters/event_tracker.js b/lib/formatters/event_tracker.js deleted file mode 100644 index 53cef95..0000000 --- a/lib/formatters/event_tracker.js +++ /dev/null @@ -1,270 +0,0 @@ -(function (global) { - var Module; - - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - } else { - Module = global.Module; - } - - var Cobalt = {}; - Module(Cobalt, 'Formatter')({}); - - Cobalt.Formatter.EventTracker = Module(Cobalt.Formatter, 'EventTracker')({ - - // Main formatter entry point, validate type and send to proper format - // method - format : function (logObject) { - switch(this._validateFormat(logObject)){ - case 'error': - return this._formatError(logObject); - break; - case 'event': - logObject._type = 'event'; - return this._formatEvent(logObject); - break; - } - return undefined; - }, - - // Format error - _formatError : function (logObject) { - var errorObject; - - errorObject = logObject.error; - errorObject._type = 'error'; - - if (typeof Client !== 'undefined') { - errorObject.site_id = Client.pageInfo.site.id; - }else if (global.data && global.data.site && global.data.site.id) { - errorObject.site_id = global.data.site.id; - } - errorObject.scoped_class_name = errorObject.meta.scopedClassName; - errorObject.user_agent = global.navigator.userAgent; - - return errorObject; - }, - - // Format the event - _formatEvent : function (logObject) { - var eventToSend; - - eventToSend = this._createStructure(logObject); - - return eventToSend; - }, - - // Check for the fingerprint of an event or an error - _validateFormat : function (logObject) { - if (typeof logObject === 'object'){ - // Check for error object (must have meta) - if (logObject.hasOwnProperty('error')) { - if (logObject.error.hasOwnProperty('meta')) { - return 'error'; - } - // Check for event object (must have type) - } else if (logObject.hasOwnProperty('event')) { - if (logObject.event.hasOwnProperty('type')) { - return 'event'; - } - } - } - return undefined; - }, - - // This was ported as-is, but It's weird that we're doing lithium logic in - // here. TODO: Please move this elsewhere. - ben - _pushToLithium : function (object) { - var lithiumEvent; - - if (typeof Li !== 'undefined') { - if (Li.stack) { - lithiumEvent = { - type : object.event.type, - instanceName : (object.event.data) ? (object.event.data.name || object.event.data.id || '---') : '---', - time : global.Cobalt.now() - }; - - Li.events.push(lithiumEvent); - Li.stack.push(lithiumEvent); - - // Another note: There was another push to Li.stack that checked if it - // was typeof === array. Which will never happen since array is typeof - // Object. But I could be wrong. So... yeah. That. - - if (Li.events.length > 20) { - Li.stack.shift(); - } - } - } - }, - - // Create the main structure, also push to lithium - _createStructure : function (logObject) { - var event = {}; - - this._pushToLithium(logObject); - - event.site_id = global.Client ? Client.pageInfo.site.id : 'client not found'; - event.event_type = logObject.event.type; - - event.data = logObject.event.data || {}; - event.data = this._process(event.data); - event.data.user_time_stamp = global.Cobalt.now(); - - event = this._addSiteData(event); - event = this._addTrackingData(event); - - event._type = 'event'; - - return event; - }, - - // Add required data from siteInfo - _addSiteData : function (event) { - if (typeof siteInfo !== 'undefined') { - event.data.trial_period_days_left = siteInfo.trialPeriodDaysLeft; - event.data.billing_status = siteInfo.billingStatus; - event.data.is_admin_on_trial = siteInfo.isAdminOnTrial; - event.data.is_new_site = siteInfo.isNewSite; - event.data.is_for_play = siteInfo.forPlay; - event.data.page_path = siteInfo.currentPagePath; - } - - return event; - }, - - // Add required tracking variables for ad campaigns and such - _addTrackingData : function (event) { - if (typeof siteInfo !== 'undefined' && siteInfo.trackingVariables) { - event.data.initial_referring_domain = siteInfo.trackingVariables.initial_referring_domain; - event.data.referrer = siteInfo.trackingVariables.referrer; - event.data.search_keyword = siteInfo.trackingVariables.search_keyword; - event.data.utm_campaign = siteInfo.trackingVariables.utm_campaign; - event.data.utm_content = siteInfo.trackingVariables.utm_content; - event.data.utm_medium = siteInfo.trackingVariables.utm_medium; - event.data.utm_source = siteInfo.trackingVariables.utm_source; - event.data.utm_term = siteInfo.trackingVariables.utm_term; - } - - return event; - }, - - // Send data through processors to extract valuable metadata - _process : function (data) { - if( typeof data !== 'undefined' && typeof data.constructor !== 'undefined' && typeof data.constructor.className !== 'undefined' ){ - switch( data.constructor.className ){ - case 'App': - return { meta: this._processApp(data) }; - - case 'Entry': - return { meta: this._processEntry(data) }; - - case 'Dot': - return { meta: this._processDot(data) }; - - case 'SiteSkin': - return { meta: this._processSiteSkin(data) }; - - case 'RepositorySkin': - return { meta: this._processRepositorySkin(data) }; - - default : - //do nothing and create an snapshot for the object - var snapshot = {}; - - $.each(data, function(i, el){ - if(typeof el === 'string' || typeof el === 'number' || typeof el === 'boolean'){ - snapshot[i] = el; - } - }); - - return { meta: { status: 'No processor for '+data.constructor.className, snapshot: snapshot } }; - } - - }else{ - // No process for you - return { meta: data }; - } - }, - - _processApp : function (data) { - var eventData = { - displayName : data.record.displayName, - repositoryAppId : data.record.repositoryApp().id, - repositoryAppName : data.record.repositoryApp().name - }; - - return { app: eventData }; - }, - - _processEntry : function (data) { - //get app data - var app = this.processApp( Client.page['app_' + data.appId] ); - - var entry = { - id : data.id, - fieldValues : {} - }; - - //parse and save fieldvalues - $.each(data.fieldValues, function(i, field){ - entry.fieldValues[field.fieldName] = field.value; - }); - - return {app: app, entry : entry}; - }, - - _processDot : function (data) { - var target = { type: data.ruleTree.type }; - - switch (target.type){ - case 'app' : - target.app = this.processApp( Client.page['app_'+data.ruleTree.id.replace('app_style_','')] ).app; - break; - } - - var dot = { - type: data.type, - name: data.name - }; - - return {target: target, dot : dot}; - }, - - _processSiteSkin : function (data) { - return { siteSkin : { - skinName: skinData.name, - skinId: skinData.id - } - }; - }, - - _processRepositorySkin : function (data) { - var skin = { - id : data.id, - name : data.name, - userName : data.userName, - acquireMode : data.acquireMode, - amountInCents : data.amountInCents, - featured : data.featured, - installCount : data.installCount, - popular : data.popular - }; - - return { repositorySkin: skin }; - } - }); - - if (Cobalt.Formatter.EventTracker.__objectSpy) { - Cobalt.Formatter.EventTracker.__objectSpy.destroy(); - } - - if (typeof require === 'function') { - global.EventTracker = Cobalt.Formatter.EventTracker; - } else { - global.Cobalt.Formatter.EventTracker = Cobalt.Formatter.EventTracker; - } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); diff --git a/lib/formatters/simple.js b/lib/formatters/simple.js index 24f65b3..6d2ad7d 100644 --- a/lib/formatters/simple.js +++ b/lib/formatters/simple.js @@ -1,30 +1,9 @@ -(function (global) { - var Module; - - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - } else { - Module = global.Module; - } - - var Cobalt = {}; - Module(Cobalt, 'Formatter')({}); - - Cobalt.Formatter.Simple = Module(Cobalt.Formatter, 'Simple')({ +Module(Cobalt.Formatter, 'Simple')({ format : function (logObject, opts){ - var indent; + var indent; - indent = Array(logObject._indentLevel + 1).join(' '); + indent = Array(logObject._indentLevel + 1).join(' '); - return indent + logObject.message; + return indent + logObject.message; } - }); - - if (typeof require === 'function') { - global.Simple = Cobalt.Formatter.Simple; - } else { - global.Cobalt.Formatter.Simple = Cobalt.Formatter.Simple; - } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +}); diff --git a/lib/formatters/token.js b/lib/formatters/token.js index f562f1c..4fa7d57 100644 --- a/lib/formatters/token.js +++ b/lib/formatters/token.js @@ -1,83 +1,58 @@ -(function (global) { - var Module; - - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - } else { - Module = global.Module; - } - - var Cobalt = {}; - Module(Cobalt, 'Formatter')({}); - - Cobalt.Formatter.Token = Module(Cobalt.Formatter, 'Token')({ - formatString : "{{message}}", - replaceRule : /{{(.*?)}}/g, - separatorLength : 60, - separatorType : "-", - format : function (logObject, opts){ - var indent, indentSize, - separatorLength, separatorType, - output; - indentSize = logObject._indentLevel || 0; - - indent = Array(indentSize + 1).join(' '); - - if (logObject._separator) { - separatorLength = logObject._separatorLength || this.separatorLength; - separatorType = logObject._separatorType || this.separatorType; - output = indent + Array(separatorLength - indentSize + 1).join(separatorType); - } else { - output = indent + this.parseFormatString(logObject, opts.formatString); - } - - if (opts.ansiColor) { - output = this.colorize(logObject._level, output); - } - - return output; - }, - - parseFormatString : function (logObject, formatString) { - var resultString = ''; - if (typeof formatString === 'undefined') { - formatString = this.formatString; - } +Module(Cobalt.Formatter, 'Token')({ + formatString : "{{message}}", + replaceRule : /{{(.*?)}}/g, + separatorLength : 60, + separatorType : "-", + format : function (logObject, opts){ + var indent, indentSize, + separatorLength, separatorType, + output; + indentSize = logObject._indentLevel || 0; + + indent = Array(indentSize + 1).join(' '); + + if (logObject._separator) { + separatorLength = logObject._separatorLength || this.separatorLength; + separatorType = logObject._separatorType || this.separatorType; + output = indent + Array(separatorLength - indentSize + 1).join(separatorType); + } else { + output = indent + this.parseFormatString(logObject, opts.formatString); + } - resultString = formatString.replace(this.replaceRule, function(match, paren){ - return logObject[paren] || "-"; - }); + if (opts.ansiColor) { + output = this.colorize(logObject._level, output); + } - return resultString; - }, + return output; + }, - colorize : function (level, message) { - switch(level) { - case 0: - case 1: - case 2: - case 3: - return message.red; - case 4: - return message.yellow; - case 5: - case 6: - return message.blue; - default: - return message; - } + parseFormatString : function (logObject, formatString) { + var resultString = ''; + if (typeof formatString === 'undefined') { + formatString = this.formatString; } - }); - if (Cobalt.Formatter.Token.__objectSpy) { - Cobalt.Formatter.Token.__objectSpy.destroy(); - } - - if (typeof require === 'function') { - global.Token = Cobalt.Formatter.Token; - } else { - global.Cobalt.Formatter.Token = Cobalt.Formatter.Token; + resultString = formatString.replace(this.replaceRule, function(match, paren){ + return logObject[paren] || "-"; + }); + + return resultString; + }, + + colorize : function (level, message) { + switch(level) { + case 0: + case 1: + case 2: + case 3: + return message.red; + case 4: + return message.yellow; + case 5: + case 6: + return message.blue; + default: + return message; + } } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +}); diff --git a/lib/loggers/console.js b/lib/loggers/console.js index c8e3036..c4031eb 100644 --- a/lib/loggers/console.js +++ b/lib/loggers/console.js @@ -1,97 +1,70 @@ -(function (global) { - var Module, Class; +Class(Cobalt.Logger, 'JsConsole')({ + prototype : { + console : null, + formatterOpts : {}, - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - Module = Ne.Module; - Class = Ne.Class; - } else { - Module = global.Module; - Class = global.Class; - } - - var Cobalt = {}; - Module(Cobalt, 'Logger')({}); - - Cobalt.Logger.JsConsole = Class(Cobalt.Logger, 'JsConsole')({ - prototype : { - console : null, - formatterOpts : {}, + init : function (config) { + var logger = this, + property; - init : function (config) { - var logger = this, - property; - - if (config) { - for (property in config) { - logger[property] = config[property]; - } + if (config) { + for (property in config) { + logger[property] = config[property]; } + } - if (!logger.console) { - logger.console = console; - } - }, + if (!logger.console) { + logger.console = console; + } + }, - log : function () { - var i, message = [], severity; + log : function () { + var i, message = [], severity; - for (i = 0; i < arguments.length; i++) { - // We're not formatting objects for now. + for (i = 0; i < arguments.length; i++) { + // We're not formatting objects for now. - if (!arguments[i].__skipConsole && !arguments[i].message.__skipConsole) { - if (typeof arguments[i].message === 'object') { - message.push(arguments[i].message); - } else { - message.push(this.format(arguments[i])); - } - if (!severity) { - severity = arguments[i]._level - } + if (!arguments[i].__skipConsole && !arguments[i].message.__skipConsole) { + if (typeof arguments[i].message === 'object') { + message.push(arguments[i].message); + } else { + message.push(this.format(arguments[i])); + } + if (!severity) { + severity = arguments[i]._level } } + } - switch (severity){ - case 0: - case 1: - case 2: - case 3: - this.console.error.apply(this.console, message); - break; - case 4: - this.console.warn.apply(this.console, message); - break; - case 5: - case 6: - this.console.info.apply(this.console, message); - break; - case 7: - default: - this.console.log.apply(this.console, message); - break; - } - }, - - format : function (logObject) { - // Usually what you want to do here is format. Preferably using - // someone inside Cobalt.Formatter - if (this.formatter) { - return this.formatter.format(logObject, this.formatterOpts); - } - - return logObject.message; + switch (severity){ + case 0: + case 1: + case 2: + case 3: + this.console.error.apply(this.console, message); + break; + case 4: + this.console.warn.apply(this.console, message); + break; + case 5: + case 6: + this.console.info.apply(this.console, message); + break; + case 7: + default: + this.console.log.apply(this.console, message); + break; } - } - }); + }, - if (Cobalt.Logger.JsConsole.__objectSpy) { - Cobalt.Logger.JsConsole.__objectSpy.destroy(); - } + format : function (logObject) { + // Usually what you want to do here is format. Preferably using + // someone inside Cobalt.Formatter + if (this.formatter) { + return this.formatter.format(logObject, this.formatterOpts); + } - if (typeof require === 'function') { - global.JsConsole = Cobalt.Logger.JsConsole; - } else { - global.Cobalt.Logger.JsConsole = Cobalt.Logger.JsConsole; + return logObject.message; + } } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +}); diff --git a/lib/loggers/socket.js b/lib/loggers/socket.js index b057d77..7066274 100644 --- a/lib/loggers/socket.js +++ b/lib/loggers/socket.js @@ -1,61 +1,37 @@ -(function (global) { - var Module, Class; - - // Load up dependencies - if (typeof require === 'function') { - var Ne = require('neon'); - var ioClient = require('socket.io-client'); - Module = Ne.Module; - Class = Ne.Class; - } else { - Module = global.Module; - Class = global.Class; - } - - var Cobalt = {}; - Module(Cobalt, 'Logger')({}); +if (typeof require === 'function') { + var ioClient = require('socket.io-client'); +} - Cobalt.Logger.Socket = Class(Cobalt.Logger, 'Socket')({ - prototype : { - serverUrl : '/', +Class(Cobalt.Logger, 'Socket')({ + prototype : { + serverUrl : '/', - init : function (config) { - var logger = this; + init : function (config) { + var logger = this; - if (config) { - for (property in config) { - logger[property] = config[property]; - } + if (config) { + for (property in config) { + logger[property] = config[property]; } + } - if (!logger.socketIo) { - logger.socketIo = ioClient; - } + if (!logger.socketIo) { + logger.socketIo = ioClient; + } - logger._socket = logger.socketIo.connect(logger.serverUrl); - }, + logger._socket = logger.socketIo.connect(logger.serverUrl); + }, - log : function () { - var i, messageArray = []; + log : function () { + var i, messageArray = []; - for (i = 0; i < arguments.length; i++) { - messageArray.push(arguments[i]); - } + for (i = 0; i < arguments.length; i++) { + messageArray.push(arguments[i]); + } - if (this._socket) { - this._socket.emit('log', messageArray); - } + if (this._socket) { + this._socket.emit('log', messageArray); } } - }); - - if (Cobalt.Logger.Socket.__objectSpy) { - Cobalt.Logger.Socket.__objectSpy.destroy(); - } - - if (typeof require === 'function') { - global.Socket = Cobalt.Logger.Socket; - } else { - global.Cobalt.Logger.Socket = Cobalt.Logger.Socket; } -}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); +}); diff --git a/package.json b/package.json index 07dcac9..0362d50 100644 --- a/package.json +++ b/package.json @@ -17,14 +17,17 @@ "url" : "http://github.com/ktlacaelel" } ], - "version" : "0.2.3", + "version" : "1.0.0", "dependencies" : { - "neon" : "1.0.x", + "neon" : "2.0.x", "socket.io-client" : "0.9.x", "colors" : "0.x.x", "emailjs" : "0.3.x", "microtime" : "0.3.x" }, + "devDependencies" : { + "tellurium" : "2.0.x" + }, "engines" : { "node" : ">= 0.8.0" }, "main" : "./lib/cobalt.js" } diff --git a/test/basic.js b/test/basic.js new file mode 100644 index 0000000..90aadd6 --- /dev/null +++ b/test/basic.js @@ -0,0 +1,35 @@ +if (typeof require === "function") { + require("cobalt-log"); +} + +var co = new Cobalt.Console({ + loggers : [ + new Cobalt.Logger.JsConsole({ + formatter : Cobalt.Formatter.Token, + formatterOpts : { + formatString : "[{{_level}}] {{message}} {{customParam}}" + } + }) + ] +}) + +// TODO: Do this whole thing with tellurium. + +co.log("Log - Normal"); +co.debug("Warn - Normal"); +co.info("Info - Normal"); +co.notice("Notice - Normal"); +co.warn("Warn - Normal"); +co.error("Error - Normal"); + +var logObject = co.extendLog({ + message : "Extended Log Object", + customParam : "<3" +}); + +co.log(logObject); +co.debug(logObject); +co.info(logObject); +co.notice(logObject); +co.warn(logObject); +co.error(logObject); diff --git a/test/browser.html b/test/browser.html new file mode 100644 index 0000000..ec58b6f --- /dev/null +++ b/test/browser.html @@ -0,0 +1,20 @@ + + + + Tellurium Test Runner + + + + + + + + + + + + + + + +