From: Ben Beltran Date: Wed, 3 Apr 2013 22:49:03 +0000 (-0600) Subject: I don't know how git works (massive commit) X-Git-Url: https://git.r.bdr.sh/rbdr/cologne/commitdiff_plain/b69497efe78c60742e0bb7ab5fed2b94ce89682e I don't know how git works (massive commit) --- diff --git a/lib/cobalt.js b/lib/cobalt.js new file mode 100644 index 0000000..45f0e26 --- /dev/null +++ b/lib/cobalt.js @@ -0,0 +1,255 @@ +(function (global) { + var Module, Class; + + // 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')({}); + Module(Cobalt, 'Formatter')({}); + + // Load up loggers + formatters + if (typeof require === 'function') { + // Formatters + Cobalt.Formatter.Simple = require('./formatters/simple.js').Simple; + Cobalt.Formatter.Ansi = require('./formatters/ansi.js').Ansi; + 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; + } + + 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; + + if (!item._cobaltLog) { + if (typeof item !== "object") { + item = { _cobaltLog : true, message : item.toString() }; + } + + item._from = co.from; + item._level = item._level || level || 7; + item._levelString = co._levelString(item._level); + item._version = co.version; + item._timestamp = co.now(); + item._indentLevel = co.currentIndent; + item._color = co.currentColor; + } + + return item; + }, + + buildSeparator : function (type) { + var co = this; + return { + _cobaltLog : true, + _separator : true, + _version : co.version, + _timestamp : co.now(), + _separatorType : type, + _indentLevel : co.currentIndent + } + }, + + _log : function (severity) { + var co = this, + logString, + i, j; + + for (i = 1; i < arguments.length; i++) { + logObject = co.buildLog(arguments[i], severity); + + for (j = 0; j < co.loggers.length; j++) { + co.loggers[j].log(logObject); + } + } + }, + + log : function () { + this._log.apply(this, [7].concat([].splice.call(arguments, 0))); + }, + + info : function () { + this._log.apply(this, [6].concat([].splice.call(arguments, 0))); + }, + + notice : function () { + this._log.apply(this, [5].concat([].splice.call(arguments, 0))); + }, + + warn : function () { + this._log.apply(this, [4].concat([].splice.call(arguments, 0))); + }, + + error : function () { + this._log.apply(this, [3].concat([].splice.call(arguments, 0))); + }, + + separator : function (type) { + var co = this; + + co._log(7, co.buildSeparator(type)); + }, + + space : function (lines) { + var co = this, + i; + + if (typeof lines === "undefined") { + lines = 1; + } + + for (i = 0; i < lines; i++) { + co.log(' '); + } + + return co; + }, + + indent : 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; + } + + return co; + }, + + outdent : function (callback) { + var co = this; + + if (typeof callback === "function") { + co.currentIndent = co.currentIndent - co.indentSize; + if (co.currentIndent < 0) { + co.currentIndent = 0; + } + + callback(); + + co.currentIndent = co.currentIndent + co.indentSize; + } else { + co.currentIndent = co.currentIndent - co.indentSize; + if (co.currentIndent < 0) { + co.currentIndent = 0; + } + } + + return co; + }, + + 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; + } + + return co; + }, + + // Returns the current time in microseconds. + now : function () { + if (typeof performance !== 'undefined') { + return performance.timing.navigationStart + performance.now(); + } + + if (typeof process !== 'undefined') { + return process.hrtime(); + } + + 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; + } + } + } + }); + + if (typeof require === 'function') { + global.Formatter = Cobalt.Formatter; + global.Logger = Cobalt.Logger; + global.Console = Cobalt.Console; + } else { + global.Cobalt = Cobalt; + } + +}(typeof window !== 'undefined' ? window : (typeof module.exports !== 'undefined' ? module.exports : self))); diff --git a/lib/cobalt.rb b/lib/cobalt.rb deleted file mode 100644 index bcd683c..0000000 --- a/lib/cobalt.rb +++ /dev/null @@ -1,101 +0,0 @@ -require 'rubygems' -require 'isna' -require 'logger' - -module Cobalt - - class Console - - attr_accessor :separator_length - - def initialize( options = {} ) - @indent = 0 - @loggers = options[:loggers] || [::Logger.new(STDOUT)] - @separator_length = 120 - @color = :white - end - - def add_logger logger - @loggers << logger - end - - def remove_logger logger - @loggers = @loggers - [logger] - end - - def log(*objects) - objects.each do |object| - the_string = object.to_s - the_string = the_string.to_ansi.send(@color).to_s - the_string = the_string.gsub(/^/, ' ' * @indent) - @loggers.each { |logger| logger.info(the_string) } - end - self - end - - def pp(*objects) - dump = "" - if objects.size > 1 - PP.pp(objects, dump) - else - PP.pp(objects.first, dump) - end - log(dump) - end - - def info(*objects) - notice(*objects) - end - - def notice(*objects) - color(:cyan) { log(*objects) } - end - - def warn(*objects) - color(:yellow) { log(*objects) } - end - - def error(*objects) - color(:red) { log(*objects) } - end - - def separator(type = '-') - log((type * (@separator_length - @indent))) - end - - def space(lines = 1) - lines.times { self.log('') } - self - end - - def indent - if block_given? - @indent = @indent + 2 - yield - @indent = @indent - 2 - else - @indent = @indent + 2 - end - self - end - - def outdent - @indent = @indent - 2 - self - end - - def color(symbol) - if block_given? - old = @color - @color = symbol - yield - @color = old - else - @color = symbol - end - self - end - - end - -end diff --git a/lib/ext/socket_helper.js b/lib/ext/socket_helper.js new file mode 100644 index 0000000..661d435 --- /dev/null +++ b/lib/ext/socket_helper.js @@ -0,0 +1,7 @@ +var bindEvents = function (socket, logger) { + socket.on('log', function (logObject) { + logger.log(logObject); + }); +} + +exports.bindEvents = bindEvents; diff --git a/lib/formatters/ansi.js b/lib/formatters/ansi.js new file mode 100644 index 0000000..25ad256 --- /dev/null +++ b/lib/formatters/ansi.js @@ -0,0 +1,47 @@ +(function (global) { + var Module; + + // Load up dependencies + if (typeof require === 'function') { + require('colors'); + var Ne = require('neon'); + Module = Ne.Module; + } else { + Module = global.Module; + } + + var Cobalt = {}; + Module(Cobalt, 'Formatter')({}); + + Cobalt.Formatter.Ansi = Module(Cobalt.Formatter, 'Ansi')({ + format : function (logObject, opts){ + var indent, + message; + + indent = Array(logObject._indentLevel + 1).join(' '); + + 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; + } + } + }); + + 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/simple.js b/lib/formatters/simple.js new file mode 100644 index 0000000..24f65b3 --- /dev/null +++ b/lib/formatters/simple.js @@ -0,0 +1,30 @@ +(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')({ + format : function (logObject, opts){ + var indent; + + indent = Array(logObject._indentLevel + 1).join(' '); + + 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 new file mode 100644 index 0000000..40d4f59 --- /dev/null +++ b/lib/formatters/token.js @@ -0,0 +1,79 @@ +(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; + } + + 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; + } + } + }); + + if (typeof require === 'function') { + global.Token = Cobalt.Formatter.Token; + } else { + global.Cobalt.Formatter.Token = Cobalt.Formatter.Token; + } +}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); diff --git a/lib/loggers/console.js b/lib/loggers/console.js new file mode 100644 index 0000000..3dc4b1d --- /dev/null +++ b/lib/loggers/console.js @@ -0,0 +1,76 @@ +(function (global) { + var Module, Class; + + // 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 : console, + formatterOpts : {}, + + init : function (config) { + var logger = this, + property; + + if (config) { + for (property in config) { + logger[property] = config[property]; + } + } + }, + + log : function (logObject) { + var message; + + message = this.format(logObject); + + switch (logObject._level){ + case 0: + case 1: + case 2: + case 3: + this.console.error(message); + break; + case 4: + this.console.warn(message); + break; + case 5: + case 6: + this.console.info(message); + break; + case 7: + default: + this.console.log(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; + } + } + }); + + if (typeof require === 'function') { + global.JsConsole = Cobalt.Logger.JsConsole; + } else { + global.Cobalt.Logger.JsConsole = Cobalt.Logger.JsConsole; + } +}(typeof window !== 'undefined' ? window : (typeof exports !== 'undefined' ? exports : self))); diff --git a/lib/loggers/socket.js b/lib/loggers/socket.js new file mode 100644 index 0000000..8d0ff55 --- /dev/null +++ b/lib/loggers/socket.js @@ -0,0 +1,36 @@ +(function (global) { + var Module, Class; + + // 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.Socket = Class(Cobalt.Logger, 'Socket')({ + prototype : { + serverUrl : '/', + + init : function () { + this._socket = io.connect(this.serverUrl); + }, + + log : function (logObject) { + this._socket.emit('log', logObject); + } + } + }); + + 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 e69de29..3d9017b 100644 --- a/package.json +++ b/package.json @@ -0,0 +1,25 @@ +{ + "name" : "cobalt", + "description" : "Logger + multiplexer for JSON based logs", + "author" : { + "name" : "Ben Beltran", + "email" : "ben@nsovocal.com", + "url" : "http://nsovocal.com" + }, + "contributors" : [ + { + "name" : "Kazuyoshi Tlacaelel", + "email" : "kazu.dev@gmail.com", + "url" : "http://github.com/ktlacaelel" + } + ], + "version" : "0.1.0", + "dependencies" : { + "neon" : "1.0.x", + "socket.io" : "0.9.x", + "colors" : "0.x.x" + }, + "engines" : { "node" : ">= 0.8.0" }, + "main" : "./lib/cobalt.js", + "private" : true +}