Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

change object oriented-ness

  • Loading branch information...
commit e6e8c6c703b761708d7ceb8cd4c56835f4367704 1 parent 4874f1e
@lancejpollard authored
View
18 Cakefile
@@ -0,0 +1,18 @@
+{spawn, exec} = require 'child_process'
+fs = require 'fs'
+sys = require 'util'
+
+task 'coffee', ->
+ coffee = spawn './node_modules/coffee-script/bin/coffee', ['-o', 'lib', '-w', 'src']
+ coffee.stdout.on 'data', (data) -> console.log data.toString().trim()
+
+task 'spec', 'Run jasmine specs', ->
+ spec = spawn './node_modules/jasmine-node/bin/jasmine-node', ['--coffee', './spec']
+ spec.stdout.on 'data', (data) ->
+ data = data.toString().replace(/^\s*|\s*$/g, '')
+ if data.match(/\u001b\[3\dm[\.F]\u001b\[0m/)
+ sys.print data
+ else
+ data = "\n#{data}" if data.match(/Finished/)
+ console.log data
+ spec.stderr.on 'data', (data) -> console.log data.toString().trim()
View
52 README.md
@@ -1,11 +1,11 @@
-# CommonLogger.js
+# underscore.logger.js
> Cross-browser and Node.js empowered logging.
## Install
```
-npm install common-logger
+npm install underscore.logger
```
## Require
@@ -13,7 +13,19 @@ npm install common-logger
### Node.js
``` coffeescript
-CommonLogger = require('common-logger')
+_console = require('underscore.logger')
+```
+
+If you want to make it useable everywhere in node:
+
+``` coffeescript
+global._console ||= require('underscore.logger')
+```
+
+Or to make it an underscore mixin:
+
+``` coffeescript
+_.mixin require('underscore.logger').toMixin()
```
### Browser
@@ -25,32 +37,40 @@ CommonLogger = require('common-logger')
## Api
``` coffeescript
-logger = new CommonLogger
-
# set the log level so anything above this won't show up
-logger.level = CommonLogger.DEBUG
+_console.level = _console.constructor.DEBUG
+
+# you can access the constructor helpers like this as well:
+Logger = _console.constructor
+_console.level = Logger.DEBUG
# override default colors for any of the log levels
-logger.colors[CommonLogger.WARN] = CommonLogger.ANSI.RED
+_console.colors[Logger.WARN] = Logger.ANSI.RED
# the first parameter is the message, any following parameters are variables.
-logger.trace "I'm a trace"
-logger.debug "Debug message"
-logger.info "%s %s!", "Hello", "World" #=> "Hello World!"
-logger.error "ERROR!"
-logger.fatal "oh man..."
+_console.trace "I'm a trace"
+_console.debug "Debug message"
+_console.info "%s %s!", "Hello", "World" #=> "Hello World!"
+_console.error "ERROR!"
+_console.fatal "oh man..."
# set a custom `out` method, which defaults to `console.log`
-logger.out = (message) -> alert(message)
+_console.out = (message) -> alert(message)
# customize the format too if you'd like, which defaults to `[date] level message`
-logger.format = (date, level, message) -> message
+_console.format = (date, level, message) -> message
-# watch the fps to see how your app is performing (`this` is the `CommonLogger.Timer` object)
-logger.on "frame" ->
+# watch the fps to see how your app is performing (`this` is the `Logger.Timer` object)
+_console.on "frame" ->
$("#log-line-template").tmpl(@fps).appendTo("#log-panel")
```
+To create a new one, maybe because you want two separate loggers (the edge case), you can use the constructor:
+
+``` coffeescript
+myLogger = new _console.constructor
+```
+
## Resources
- http://en.wikipedia.org/wiki/Common_Log_Format
View
169 lib/common-logger.js → lib/underscore.logger.js
@@ -1,14 +1,23 @@
(function() {
- var CommonLogger;
- CommonLogger = (function() {
- CommonLogger.FATAL = 0;
- CommonLogger.ERROR = 1;
- CommonLogger.WARN = 2;
- CommonLogger.INFO = 3;
- CommonLogger.DEBUG = 4;
- CommonLogger.TRACE = 5;
- CommonLogger.levels = ["FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
- CommonLogger.ANSI = {
+ var Logger;
+
+ Logger = (function() {
+
+ Logger.FATAL = 0;
+
+ Logger.ERROR = 1;
+
+ Logger.WARN = 2;
+
+ Logger.INFO = 3;
+
+ Logger.DEBUG = 4;
+
+ Logger.TRACE = 5;
+
+ Logger.levels = ["FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE"];
+
+ Logger.ANSI = {
OFF: 0,
BOLD: 1,
ITALIC: 3,
@@ -33,22 +42,22 @@
CYAN_BG: 46,
WHITE_BG: 47
};
- CommonLogger.colors = [CommonLogger.ANSI.MAGENTA, CommonLogger.ANSI.RED, CommonLogger.ANSI.YELLOW, CommonLogger.ANSI.GREEN, CommonLogger.ANSI.CYAN, CommonLogger.ANSI.BLUE];
- function CommonLogger(options) {
- if (options == null) {
- options = {};
- }
- this.level = options.level || this.constructor.DEBUG;
- if (options.out) {
- this.out = options.out;
- }
+
+ Logger.colors = [Logger.ANSI.MAGENTA, Logger.ANSI.RED, Logger.ANSI.YELLOW, Logger.ANSI.GREEN, Logger.ANSI.CYAN, Logger.ANSI.BLUE];
+
+ function Logger(options) {
+ if (options == null) options = {};
+ this.level = options.level || Logger.DEBUG;
+ if (options.out) this.out = options.out;
this.colorized = options.hasOwnProperty("colorized") ? options.colorized : false;
- this.colors = this.constructor.colors.concat();
+ this.colors = Logger.colors.concat();
}
- CommonLogger.prototype.out = function(message) {
+
+ Logger.prototype.out = function(message) {
return console.log(message);
};
- CommonLogger.prototype.colorize = function() {
+
+ Logger.prototype.colorize = function() {
var color, colors, i, result, string;
colors = Array.prototype.slice.call(arguments);
string = colors.shift();
@@ -58,13 +67,15 @@
result += "\033[" + color + "m";
i++;
}
- result += "" + string + "\033[" + this.constructor.ANSI.OFF + "m";
+ result += "" + string + "\033[" + Logger.ANSI.OFF + "m";
return result;
};
- CommonLogger.prototype.format = function(date, level, message) {
- return "[" + (date.toUTCString()) + "] " + this.constructor.levels[level] + " " + message;
+
+ Logger.prototype.format = function(date, level, message) {
+ return "[" + (date.toUTCString()) + "] " + Logger.levels[level] + " " + message;
};
- CommonLogger.prototype.log = function(level, args) {
+
+ Logger.prototype._log = function(level, args) {
var i, message;
if (level <= this.level) {
i = 0;
@@ -72,32 +83,42 @@
return args[i++];
});
message = this.format(new Date(), level, message);
- if (this.colorized) {
- message = this.colorize(message, this.colors[level]);
- }
+ if (this.colorized) message = this.colorize(message, this.colors[level]);
return this.out(message);
}
};
- CommonLogger.prototype.fatal = function() {
- return this.log(this.constructor.FATAL, arguments);
+
+ Logger.prototype.log = function() {
+ return this.info.apply(this, arguments);
};
- CommonLogger.prototype.error = function() {
- return this.log(this.constructor.ERROR, arguments);
+
+ Logger.prototype.fatal = function() {
+ return this._log(Logger.FATAL, arguments);
};
- CommonLogger.prototype.warn = function() {
- return this.log(this.constructor.WARN, arguments);
+
+ Logger.prototype.error = function() {
+ return this._log(Logger.ERROR, arguments);
};
- CommonLogger.prototype.info = function() {
- return this.log(this.constructor.INFO, arguments);
+
+ Logger.prototype.warn = function() {
+ return this._log(Logger.WARN, arguments);
};
- CommonLogger.prototype.debug = function() {
- return this.log(this.constructor.DEBUG, arguments);
+
+ Logger.prototype.info = function() {
+ return this._log(Logger.INFO, arguments);
};
- CommonLogger.prototype.trace = function() {
- return this.log(this.constructor.TRACE, arguments);
+
+ Logger.prototype.debug = function() {
+ return this._log(Logger.DEBUG, arguments);
};
- CommonLogger.prototype.group = function() {};
- CommonLogger.prototype.on = function(event, callback) {
+
+ Logger.prototype.trace = function() {
+ return this._log(Logger.TRACE, arguments);
+ };
+
+ Logger.prototype.group = function() {};
+
+ Logger.prototype.on = function(event, callback) {
var _ref;
switch (event) {
case "message":
@@ -107,9 +128,7 @@
this;
break;
case "frame":
- if ((_ref = this.timer) == null) {
- this.timer = new CommonLogger.Timer();
- }
+ if ((_ref = this.timer) == null) this.timer = new Logger.Timer();
this.timer.on(event, callback);
break;
case "bench":
@@ -117,7 +136,37 @@
}
return this;
};
- CommonLogger.Timer = (function() {
+
+ Logger.prototype.toObject = function() {
+ var logger;
+ logger = this;
+ return {
+ log: function() {
+ return logger.log.apply(logger, arguments);
+ },
+ fatal: function() {
+ return logger.fatal.apply(logger, arguments);
+ },
+ error: function() {
+ return logger.error.apply(logger, arguments);
+ },
+ warn: function() {
+ return logger.warn.apply(logger, arguments);
+ },
+ info: function() {
+ return logger.info.apply(logger, arguments);
+ },
+ debug: function() {
+ return logger.debug.apply(logger, arguments);
+ },
+ trace: function() {
+ return logger.trace.apply(logger, arguments);
+ }
+ };
+ };
+
+ Logger.Timer = (function() {
+
function Timer() {
this.now = Date.now();
this.time_last_frame = this.now;
@@ -131,26 +180,28 @@
this.frames = 0;
this.handlers = [];
}
+
Timer.prototype.on = function(event, callback) {
this.handlers.push(callback);
return this.start();
};
+
Timer.prototype.start = function() {
var self;
- if (this.timer) {
- return this;
- }
+ if (this.timer) return this;
self = this;
this.timer = setInterval((function() {
return self.update();
}), 1000 / 60);
return this;
};
+
Timer.prototype.stop = function() {
clearInterval(this.timer);
this.timer = null;
return this;
};
+
Timer.prototype.update = function() {
var handler, _i, _len, _ref, _results;
this.now = Date.now();
@@ -174,13 +225,23 @@
return _results;
}
};
+
return Timer;
+
})();
- return CommonLogger;
+
+ return Logger;
+
})();
- if (typeof module === 'undefined') {
- this["CommonLogger"] = CommonLogger;
+
+ if (typeof module === 'undefined' || typeof window !== 'undefined') {
+ window["_console"] = new Logger({
+ colorized: false
+ });
} else {
- module.exports = CommonLogger;
+ module.exports = new Logger({
+ colorized: true
+ });
}
+
}).call(this);
View
14 package.json
@@ -1,9 +1,9 @@
{
- "name": "common-logger",
- "version": "0.2.0",
- "description": "Cross-browser and Node.js empowered logging",
- "homepage": "http://github.com/viatropos/common-logger.js",
- "main": "lib/common-logger.js",
+ "name": "underscore.logger",
+ "version": "0.3.0",
+ "description": "Cross-browser and Node.js empowered logging. Use as Underscore.js mixin if desired.",
+ "homepage": "http://github.com/viatropos/underscore.logger.js",
+ "main": "lib/underscore.logger.js",
"author": "Lance Pollard <lancejpollard@gmail.com>",
"keywords": [
"logging",
@@ -17,11 +17,11 @@
}
],
"bugs": {
- "url": "https://github.com/viatropos/common-logger.js/issues"
+ "url": "https://github.com/viatropos/underscore.logger.js/issues"
},
"repository": {
"type": "git",
- "url": "https://github.com/viatropos/common-logger.js.git"
+ "url": "https://github.com/viatropos/underscore.logger.js.git"
},
"directories": {
"test": "spec"
View
48 spec/common_logger_spec.coffee
@@ -1,48 +0,0 @@
-CommonLogger = require('../lib/common-logger')
-
-describe 'common-logger', ->
- describe 'levels', ->
- beforeEach ->
- @logger = new CommonLogger
-
- it "should default to debug", ->
- expect(@logger.level).toEqual CommonLogger.DEBUG
-
- it "should accept a new log level", ->
- @logger.level = CommonLogger.ERROR
- expect(@logger.level).toEqual CommonLogger.ERROR
-
- describe 'color', ->
- beforeEach ->
- @logger = new CommonLogger
-
- it "should have default colors", ->
- expect(@logger.colors[CommonLogger.TRACE]).toEqual CommonLogger.ANSI.BLUE
- expect(@logger.colors[CommonLogger.DEBUG]).toEqual CommonLogger.ANSI.CYAN
-
- describe 'format', ->
- beforeEach ->
- @logger = new CommonLogger(colorized: false, out: (m) -> m)
-
- it 'should default to a date format', ->
- now = new Date().toUTCString()
- expect(@logger.info('format')).toEqual "[#{now}] INFO format"
-
- describe 'file', ->
-
- describe 'stream', ->
-
- describe 'grouping', ->
-
- describe 'benchmarking', ->
-
- describe 'fps', ->
- beforeEach ->
- @logger = new CommonLogger(colorized: false)
-
- it "should clock it!", ->
- @logger.on "frame", ->
- expect(@fps).toEqual 60
- @stop()
-
- describe 'cross-browser', ->
View
85 spec/loggerSpec.coffee
@@ -0,0 +1,85 @@
+_console = require('../lib/underscore.logger')
+Logger = _console.constructor
+_ = require 'underscore'
+
+describe 'common-logger', ->
+ describe 'levels', ->
+ it "should default to debug", ->
+ expect(_console.level).toEqual Logger.DEBUG
+
+ it "should accept a new log level", ->
+ _console.level = Logger.ERROR
+ expect(_console.level).toEqual Logger.ERROR
+ _console.level = Logger.DEBUG
+
+ describe 'color', ->
+ it "should have default colors", ->
+ expect(_console.colors[Logger.TRACE]).toEqual Logger.ANSI.BLUE
+ expect(_console.colors[Logger.DEBUG]).toEqual Logger.ANSI.CYAN
+
+ describe 'format', ->
+ beforeEach ->
+ _console.colorized = false
+ _console.out = (m) -> m
+
+ afterEach ->
+ _console.colorized = true
+ _console.out = (m) -> console.log m
+
+ it 'should default to a date format', ->
+ now = new Date().toUTCString()
+ expect(_console.info('format')).toEqual "[#{now}] INFO format"
+
+ describe 'file', ->
+
+ describe 'stream', ->
+
+ describe 'grouping', ->
+
+ describe 'benchmarking', ->
+
+ describe 'fps', ->
+ beforeEach ->
+ _console.colorized = false
+
+ afterEach ->
+ _console.colorized = true
+
+ it "should clock it!", ->
+ _console.on "frame", ->
+ expect(@fps).toEqual 60
+ @stop()
+
+ describe 'cross-browser', ->
+
+ describe 'underscore mixin', ->
+ beforeEach ->
+ _console.colorized = false
+ _console.out = (m) -> m
+
+ afterEach ->
+ _console.colorized = true
+ _console.out = (m) -> console.log m
+
+ it 'should become a mixin!', ->
+ expect(_.log).toEqual undefined
+ expect(_.fatal).toEqual undefined
+ expect(_.error).toEqual undefined
+ expect(_.warn).toEqual undefined
+ expect(_.info).toEqual undefined
+ expect(_.debug).toEqual undefined
+ expect(_.trace).toEqual undefined
+
+ _.mixin _console.toObject()
+
+ expect(_.log).toNotEqual undefined
+ expect(_.fatal).toNotEqual undefined
+ expect(_.error).toNotEqual undefined
+ expect(_.warn).toNotEqual undefined
+ expect(_.info).toNotEqual undefined
+ expect(_.debug).toNotEqual undefined
+ expect(_.trace).toNotEqual undefined
+
+ now = new Date().toUTCString()
+
+ expect(_.info('format')).toEqual "[#{now}] INFO format"
View
46 src/common-logger.coffee → src/underscore.logger.coffee
@@ -1,4 +1,4 @@
-class CommonLogger
+class Logger
@FATAL: 0
@ERROR: 1
@WARN: 2
@@ -36,11 +36,11 @@ class CommonLogger
@colors: [@ANSI.MAGENTA, @ANSI.RED, @ANSI.YELLOW, @ANSI.GREEN, @ANSI.CYAN, @ANSI.BLUE]
constructor: (options = {}) ->
- @level = options.level || @constructor.DEBUG
+ @level = options.level || Logger.DEBUG
@out = options.out if options.out
# set to false if you're in the browser
@colorized = if options.hasOwnProperty("colorized") then options.colorized else false
- @colors = @constructor.colors.concat()
+ @colors = Logger.colors.concat()
out: (message) -> console.log(message)
@@ -52,38 +52,41 @@ class CommonLogger
while color = colors[i]
result += "\033[#{color}m"
i++
- result += "#{string}\033[#{@constructor.ANSI.OFF}m"
+ result += "#{string}\033[#{Logger.ANSI.OFF}m"
result
format: (date, level, message) ->
# Common Log Date Format: [10/Oct/2000:13:55:36 -0700]
- "[#{date.toUTCString()}] #{@constructor.levels[level]} #{message}"
+ "[#{date.toUTCString()}] #{Logger.levels[level]} #{message}"
- log: (level, args) ->
+ _log: (level, args) ->
if level <= @level
i = 0
message = args[0].replace /%s/g, -> args[i++]
message = @format(new Date(), level, message)
message = @colorize(message, @colors[level]) if @colorized
@out message
+
+ log: ->
+ @info(arguments...)
fatal: ->
- @log(@constructor.FATAL, arguments)
+ @_log(Logger.FATAL, arguments)
error: ->
- @log(@constructor.ERROR, arguments)
+ @_log(Logger.ERROR, arguments)
warn: ->
- @log(@constructor.WARN, arguments)
+ @_log(Logger.WARN, arguments)
info: ->
- @log(@constructor.INFO, arguments)
+ @_log(Logger.INFO, arguments)
debug: ->
- @log(@constructor.DEBUG, arguments)
+ @_log(Logger.DEBUG, arguments)
trace: ->
- @log(@constructor.TRACE, arguments)
+ @_log(Logger.TRACE, arguments)
group: ->
@@ -94,12 +97,23 @@ class CommonLogger
when "line"
@
when "frame"
- @timer ?= new CommonLogger.Timer()
+ @timer ?= new Logger.Timer()
@timer.on(event, callback)
when "bench"
@
@
+ toObject: ->
+ logger = @
+
+ log: -> logger.log(arguments...)
+ fatal: -> logger.fatal(arguments...)
+ error: -> logger.error(arguments...)
+ warn: -> logger.warn(arguments...)
+ info: -> logger.info(arguments...)
+ debug: -> logger.debug(arguments...)
+ trace: -> logger.trace(arguments...)
+
class @Timer
constructor: ->
@now = Date.now()
@@ -147,7 +161,7 @@ class CommonLogger
for handler in @handlers
handler.apply(@)
-if typeof module == 'undefined'
- this["CommonLogger"] = CommonLogger
+if typeof module == 'undefined' || typeof window != 'undefined'
+ window["_console"] = new Logger(colorized: false)
else
- module.exports = CommonLogger
+ module.exports = new Logger(colorized: true)
Please sign in to comment.
Something went wrong with that request. Please try again.