Permalink
Browse files

v2.0.2. Bugfix.

- v2.0.2 May 7, 2013
	- Fixed defaulting the log level
		- Closes [issue #6](#6)
reported by [Erik Dasque](https://github.com/edasque)
  • Loading branch information...
1 parent a1ad687 commit ab7165b967372c5efa87a2e4f55d4355173d27de @balupton balupton committed May 7, 2013
Showing with 42 additions and 22 deletions.
  1. +4 −0 History.md
  2. +1 −1 package.json
  3. +16 −5 src/lib/caterpillar.coffee
  4. +21 −16 src/test/caterpillar-test.coffee
View
@@ -1,5 +1,9 @@
## History
+- v2.0.2 May 7, 2013
+ - Fixed defaulting the log level
+ - Closes [issue #6](https://github.com/bevry/caterpillar/issues/6) reported by [Erik Dasque](https://github.com/edasque)
+
- v2.0.1 April 25, 2013
- Node 0.8 support
View
@@ -1,6 +1,6 @@
{
"name": "caterpillar",
- "version": "2.0.1",
+ "version": "2.0.2",
"description": "Caterpillar is the ultimate logging system for Node.js, based on transform streams you can log to it and pipe the output off to different locations, including some pre-made ones. Caterpillar also supports log levels according to the RFC standard, as well as line, method, and file fetching for messages. You can even use it in web browsers with caterpillar-browser.",
"homepage": "https://github.com/bevry/caterpillar",
"keywords": [
@@ -73,6 +73,7 @@ class Logger extends Transform
result =
levelNumber: null
levelName: null
+ defaulted: false
# Log the entry
if typeof level is 'number'
@@ -85,6 +86,7 @@ class Logger extends Transform
unless levelNumber?
levelNumber = @getLevelNumber('default')
levelName = @getLevelName(levelNumber)
+ result.defaulted = true
# Apply
result.levelNumber = levelNumber
@@ -121,17 +123,26 @@ class Logger extends Transform
format: (level, args...) ->
# Prepare
entry = {}
- entry.args = args
entry.date = new Date().toISOString()
- entry[key] = value for own key,value of @getLevelInfo(level)
- entry[key] = value for own key,value of @getLineInfo(level)
+
+ # Prepare
+ levelInfo = @getLevelInfo(level)
+ lineInfo = @getLineInfo(level)
+
+ # Add the level to the message arguments if it was not a level
+ args.unshift(level) if levelInfo.defaulted and level isnt 'default'
+ delete levelInfo.defaulted
+
+ # Apply
+ entry.args = args
+ extendr.extend(entry, levelInfo, lineInfo)
# Return
return entry
- log: (level, args...) ->
+ log: (args...) ->
# Prepare
- entry = @format(level, args...)
+ entry = @format(args...)
entryString = JSON.stringify(entry)
# Write the entry
@@ -51,22 +51,25 @@ joe.describe 'caterpillar', (describe,it) ->
logger = new Logger()
output = new PassThrough()
actual = []
- expected = [
- '{"args":["this is emergency and is level 0"],"date":"2013-04-25T10:18:25.722Z","levelNumber":0,"levelName":"emergency","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is alert and is level 1"],"date":"2013-04-25T10:18:25.729Z","levelNumber":1,"levelName":"alert","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is critical and is level 2"],"date":"2013-04-25T10:18:25.730Z","levelNumber":2,"levelName":"critical","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is error and is level 3"],"date":"2013-04-25T10:18:25.731Z","levelNumber":3,"levelName":"error","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is warning and is level 4"],"date":"2013-04-25T10:18:25.731Z","levelNumber":4,"levelName":"warning","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is notice and is level 5"],"date":"2013-04-25T10:18:25.732Z","levelNumber":5,"levelName":"notice","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is info and is level 6"],"date":"2013-04-25T10:18:25.732Z","levelNumber":6,"levelName":"info","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is debug and is level 7"],"date":"2013-04-25T10:18:25.733Z","levelNumber":7,"levelName":"debug","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is emerg and is level 0"],"date":"2013-04-25T10:18:25.734Z","levelNumber":0,"levelName":"emergency","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is crit and is level 2"],"date":"2013-04-25T10:18:25.734Z","levelNumber":2,"levelName":"critical","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is err and is level 3"],"date":"2013-04-25T10:18:25.735Z","levelNumber":3,"levelName":"error","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is warn and is level 4"],"date":"2013-04-25T10:18:25.735Z","levelNumber":4,"levelName":"warning","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is note and is level 5"],"date":"2013-04-25T10:18:25.736Z","levelNumber":5,"levelName":"notice","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
- '{"args":["this is default and is level 6"],"date":"2013-04-25T10:18:25.736Z","levelNumber":6,"levelName":"info","line":"59","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}'
- ].map(cleanChanging)
+ expected =
+ [ '{"date":"2013-05-07T11:12:43.982Z","args":["this is emergency and is level 0"],"levelNumber":0,"levelName":"emergency","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.986Z","args":["this is alert and is level 1"],"levelNumber":1,"levelName":"alert","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.987Z","args":["this is critical and is level 2"],"levelNumber":2,"levelName":"critical","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.987Z","args":["this is error and is level 3"],"levelNumber":3,"levelName":"error","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.988Z","args":["this is warning and is level 4"],"levelNumber":4,"levelName":"warning","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.988Z","args":["this is notice and is level 5"],"levelNumber":5,"levelName":"notice","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.989Z","args":["this is info and is level 6"],"levelNumber":6,"levelName":"info","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.990Z","args":["this is debug and is level 7"],"levelNumber":7,"levelName":"debug","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.991Z","args":["this is emerg and is level 0"],"levelNumber":0,"levelName":"emergency","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.991Z","args":["this is crit and is level 2"],"levelNumber":2,"levelName":"critical","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.992Z","args":["this is err and is level 3"],"levelNumber":3,"levelName":"error","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.992Z","args":["this is warn and is level 4"],"levelNumber":4,"levelName":"warning","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.992Z","args":["this is note and is level 5"],"levelNumber":5,"levelName":"notice","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.993Z","args":["this is default and is level 6"],"levelNumber":6,"levelName":"info","line":"89","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}',
+ '{"date":"2013-05-07T11:12:43.995Z","args":["this is unknown and is level 6"],"levelNumber":6,"levelName":"info","line":"91","method":"Task.fn","file":"/Users/balupton/Projects/caterpillar/out/test/caterpillar-test.js"}' ]
+
+ # Clean
+ expected = expected.map(cleanChanging)
output.on 'data', (chunk) ->
actual.push chunk.toString()
@@ -78,9 +81,11 @@ joe.describe 'caterpillar', (describe,it) ->
for own name,code of Logger::config.levels
message = "this is #{name} and is level #{code}"
logger.log(name, message)
+ logger.log('this is unknown and is level 6')
it 'should provide the expected output', (done) ->
output.on 'end', ->
+ #console.log actual
actual = actual.map(cleanChanging)
expect(actual.length).to.equal(expected.length)
for result,index in actual

0 comments on commit ab7165b

Please sign in to comment.