Skip to content

Commit

Permalink
v2.0.2. Bugfix.
Browse files Browse the repository at this point in the history
- v2.0.2 May 2, 2013
	- Fixed colors
  • Loading branch information
balupton committed May 2, 2013
1 parent f6020be commit 688c368
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 54 deletions.
3 changes: 3 additions & 0 deletions History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## History

- v2.0.2 May 2, 2013
- Fixed colors

- v2.0.1 April 25, 2013
- Node 0.8 support

Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ var human = new (require('caterpillar-human').Human)();
logger.pipe(human).pipe(process.stdout);

// Log
logger.log('info', 'this is the first log entry');
// info: this is the first log entry
logger.log('debug', 'this is the second log entry');
// debug: this is the second log entry
logger.log('warn', 'this is the first log entry');
// warn: this is the first log entry
logger.log('info', 'this is the second log entry');
// info: this is the second log entry

// Wait
setTimeout(function(){
// Set debug mode
logger.setConfig({level:7});

// Log
logger.log('info', 'this is the first log entry');
// info: this is the first log entry
logger.log('warn', 'this is the first log entry');
// warn: this is the first log entry
// → [2013-04-25 20:37:22.692] [/Users/balupton/Projects/caterpillar-human/example.js:20] [null._onTimeout]
logger.log('debug', 'this is the second log entry');
// debug: this is the second log entry
logger.log('info', 'this is the second log entry');
// info: this is the second log entry
// → [2013-04-25 20:37:22.693] [/Users/balupton/Projects/caterpillar-human/example.js:22] [null._onTimeout]
},0);
```
Expand Down
16 changes: 8 additions & 8 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@ var human = new (require('./').Human)();
logger.pipe(human).pipe(process.stdout);

// Log
logger.log('info', 'this is the first log entry');
// info: this is the first log entry
logger.log('debug', 'this is the second log entry');
// debug: this is the second log entry
logger.log('warn', 'this is the first log entry');
// warn: this is the first log entry
logger.log('info', 'this is the second log entry');
// info: this is the second log entry

// Wait
setTimeout(function(){
// Set debug mode
logger.setConfig({level:7});

// Log
logger.log('info', 'this is the first log entry');
// info: this is the first log entry
logger.log('warn', 'this is the first log entry');
// warn: this is the first log entry
// → [2013-04-25 20:37:22.692] [/Users/balupton/Projects/caterpillar-human/example.js:20] [null._onTimeout]
logger.log('debug', 'this is the second log entry');
// debug: this is the second log entry
logger.log('info', 'this is the second log entry');
// info: this is the second log entry
// → [2013-04-25 20:37:22.693] [/Users/balupton/Projects/caterpillar-human/example.js:22] [null._onTimeout]
},0);
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "caterpillar-human",
"version": "2.0.1",
"version": "2.0.2",
"description": "Turn your Caterpillar logger stream into a beautiful readable format with colors and optional debug information",
"homepage": "https://github.com/bevry/caterpillar-human",
"keywords": [
Expand Down
32 changes: 18 additions & 14 deletions src/lib/caterpillar-human.coffee
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
# Import
util = require('util')
try
cliColor = require('cli-color')
colorFormatters = require('cli-color')
catch err
cliColor = null
colorFormatters = null

# Human
class Human extends require('caterpillar').Transform
Expand All @@ -27,7 +27,7 @@ class Human extends require('caterpillar').Transform

getColor: (levelNumber) ->
# Determine
color = @config.color and @config.colors?[levelNumber] or false
color = @config.colors?[levelNumber] or false

# Return
return color
Expand Down Expand Up @@ -91,26 +91,30 @@ class Human extends require('caterpillar').Transform

format: (entry) =>
# Prepare
entry.color = @getColor(entry.levelCode)
config = @getConfig()
entry.color = @getColor(entry.levelNumber)
entry.timestamp = @formatDate(entry.date)
entry.text = @formatArguments(entry.args)
useColors = @config.color is true
debugMode = config.level is 7
result = null

# Check
if entry.text
# Formatters
colorFormatter = entry.color and cliColor?[entry.color]
debugFormatter = false #cliColor.white
messageFormatter = colorFormatter and cliColor?.bold
levelFormatter = useColors and colorFormatters?[entry.color]
textFormatter = debugMode and useColors and colorFormatters?.bold
debugFormatter = false # useColors and colorFormatters?.italic

# Message
levelName = entry.levelName+':'
levelName = colorFormatter(levelName) if colorFormatter
messageString = "#{levelName} #{entry.text}"
messageString = messageFormatter(messageString) if messageFormatter

# Level
if @config.level is 7
levelString = entry.levelName+':'
levelString = levelFormatter(levelString) if levelFormatter
entryString = entry.text
entryString = textFormatter(entryString) if textFormatter
messageString = "#{levelString} #{entryString}"

# Debugging
if debugMode
# Debug Information
seperator = '\n'
debugString = "[#{entry.timestamp}] [#{entry.file}:#{entry.line}] [#{entry.method}]"
Expand Down
92 changes: 69 additions & 23 deletions src/test/caterpillar-human-test.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ joe.describe 'human', (describe,it) ->
it 'should instantiate correctly', ->
transform = new Human()

describe 'logging', (describe,it) ->

describe 'logging without colors', (describe,it) ->
logger = new Logger()
transform = new Human(color:false)
output = new PassThrough()
actual = []
expected = [
'emergency: this is emergency and is level 0\n',
expected =
[ 'emergency: this is emergency and is level 0\n',
'alert: this is alert and is level 1\n',
'critical: this is critical and is level 2\n',
'error: this is error and is level 3\n',
Expand All @@ -40,8 +41,48 @@ joe.describe 'human', (describe,it) ->
'error: this is err and is level 3\n',
'warning: this is warn and is level 4\n',
'notice: this is note and is level 5\n',
'info: this is default and is level 6\n'
]
'info: this is default and is level 6\n' ]

output.on 'data', (chunk) ->
actual.push chunk.toString()

it 'should pipe correctly', ->
logger.pipe(transform).pipe(output)

it 'should log messages', ->
for own name,code of Logger::config.levels
message = "this is #{name} and is level #{code}"
logger.log(name, message)

it 'should provide the expected output', (done) ->
output.on 'end', ->
#console.log actual
expect(actual.length).to.equal(expected.length)
for result,index in actual
expect(result).to.equal(expected[index])
done()
logger.end()

describe 'logging with colors', (describe,it) ->
logger = new Logger()
transform = new Human()
output = new PassThrough()
actual = []
expected =
[ '\u001b[31memergency:\u001b[39m this is emergency and is level 0\n',
'\u001b[31malert:\u001b[39m this is alert and is level 1\n',
'\u001b[31mcritical:\u001b[39m this is critical and is level 2\n',
'\u001b[31merror:\u001b[39m this is error and is level 3\n',
'\u001b[33mwarning:\u001b[39m this is warning and is level 4\n',
'\u001b[33mnotice:\u001b[39m this is notice and is level 5\n',
'\u001b[32minfo:\u001b[39m this is info and is level 6\n',
'\u001b[32mdebug:\u001b[39m this is debug and is level 7\n',
'\u001b[31memergency:\u001b[39m this is emerg and is level 0\n',
'\u001b[31mcritical:\u001b[39m this is crit and is level 2\n',
'\u001b[31merror:\u001b[39m this is err and is level 3\n',
'\u001b[33mwarning:\u001b[39m this is warn and is level 4\n',
'\u001b[33mnotice:\u001b[39m this is note and is level 5\n',
'\u001b[32minfo:\u001b[39m this is default and is level 6\n' ]

output.on 'data', (chunk) ->
actual.push chunk.toString()
Expand All @@ -56,33 +97,37 @@ joe.describe 'human', (describe,it) ->

it 'should provide the expected output', (done) ->
output.on 'end', ->
#console.log actual
expect(actual.length).to.equal(expected.length)
for result,index in actual
expect(result).to.equal(expected[index])
done()
logger.end()

describe 'logging debug', (describe,it) ->

describe 'logging with colors in debug mode', (describe,it) ->
logger = new Logger()
transform = new Human(color:false,level:7)
transform = new Human(level:7)
output = new PassThrough()
actual = []
expected = [
'emergency: this is emergency and is level 0\n → [2013-04-25 20:07:40.449] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'alert: this is alert and is level 1\n → [2013-04-25 20:07:40.450] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'critical: this is critical and is level 2\n → [2013-04-25 20:07:40.450] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'error: this is error and is level 3\n → [2013-04-25 20:07:40.451] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'warning: this is warning and is level 4\n → [2013-04-25 20:07:40.451] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'notice: this is notice and is level 5\n → [2013-04-25 20:07:40.452] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'info: this is info and is level 6\n → [2013-04-25 20:07:40.453] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'debug: this is debug and is level 7\n → [2013-04-25 20:07:40.454] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'emergency: this is emerg and is level 0\n → [2013-04-25 20:07:40.455] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'critical: this is crit and is level 2\n → [2013-04-25 20:07:40.456] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'error: this is err and is level 3\n → [2013-04-25 20:07:40.457] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'warning: this is warn and is level 4\n → [2013-04-25 20:07:40.458] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'notice: this is note and is level 5\n → [2013-04-25 20:07:40.459] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n',
'info: this is default and is level 6\n → [2013-04-25 20:07:40.463] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:90] [Task.fn]\n'
].map(cleanChanging)
expected =
[ '\u001b[31memergency:\u001b[39m \u001b[1mthis is emergency and is level 0\u001b[22m\n → [2013-05-02 11:02:29.106] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[31malert:\u001b[39m \u001b[1mthis is alert and is level 1\u001b[22m\n → [2013-05-02 11:02:29.106] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[31mcritical:\u001b[39m \u001b[1mthis is critical and is level 2\u001b[22m\n → [2013-05-02 11:02:29.107] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[31merror:\u001b[39m \u001b[1mthis is error and is level 3\u001b[22m\n → [2013-05-02 11:02:29.108] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[33mwarning:\u001b[39m \u001b[1mthis is warning and is level 4\u001b[22m\n → [2013-05-02 11:02:29.108] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[33mnotice:\u001b[39m \u001b[1mthis is notice and is level 5\u001b[22m\n → [2013-05-02 11:02:29.109] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[32minfo:\u001b[39m \u001b[1mthis is info and is level 6\u001b[22m\n → [2013-05-02 11:02:29.110] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[32mdebug:\u001b[39m \u001b[1mthis is debug and is level 7\u001b[22m\n → [2013-05-02 11:02:29.112] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[31memergency:\u001b[39m \u001b[1mthis is emerg and is level 0\u001b[22m\n → [2013-05-02 11:02:29.113] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[31mcritical:\u001b[39m \u001b[1mthis is crit and is level 2\u001b[22m\n → [2013-05-02 11:02:29.114] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[31merror:\u001b[39m \u001b[1mthis is err and is level 3\u001b[22m\n → [2013-05-02 11:02:29.115] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[33mwarning:\u001b[39m \u001b[1mthis is warn and is level 4\u001b[22m\n → [2013-05-02 11:02:29.116] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[33mnotice:\u001b[39m \u001b[1mthis is note and is level 5\u001b[22m\n → [2013-05-02 11:02:29.120] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n',
'\u001b[32minfo:\u001b[39m \u001b[1mthis is default and is level 6\u001b[22m\n → [2013-05-02 11:02:29.121] [/Users/balupton/Projects/caterpillar-human/out/test/caterpillar-human-test.js:95] [Task.fn]\n' ]

# Clean up
expected = expected.map(cleanChanging)

output.on 'data', (chunk) ->
actual.push chunk.toString()
Expand All @@ -97,6 +142,7 @@ joe.describe 'human', (describe,it) ->

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
Expand Down

0 comments on commit 688c368

Please sign in to comment.