Comparing version 0.3.1 to 0.4.0
@@ -1,32 +0,8 @@ | ||
exports.parse = function(line) { | ||
exports.parse = require('./lib/logfmt_parser').parse; | ||
var pairs = line.match(/([a-zA-Z0-9\%\_\-\.\^]+)=?(([a-zA-Z0-9\.\-\_\.\/\@]+)|("([^\"]+)"))?/g) | ||
var attrs = {} | ||
exports.stream = process.stdout; | ||
if(!pairs) { return attrs } | ||
exports.log = function(data, stream) { | ||
if(stream == undefined) stream = exports.stream; | ||
pairs.forEach(function(pair) { | ||
parts = pair.split("=") | ||
key = parts.shift() | ||
value = parts.join("=") | ||
//strip quotes | ||
if(value[0] == '"'){ | ||
value = value.substring(1, value.length-1) | ||
} | ||
//casts | ||
if(value == '') value = true; | ||
else if(value == 'true') value = true; | ||
else if(value == 'false') value = false; | ||
else if(/^\d+$/.test(value)) value = parseInt(value); | ||
attrs[key] = value | ||
}) | ||
return attrs; | ||
} | ||
exports.sink = process.stdout; | ||
exports.log = function(data, sink) { | ||
if(sink == undefined) sink = exports.sink; | ||
var line = ''; | ||
@@ -40,3 +16,3 @@ Object.keys(data).forEach(function(key){ | ||
//trim traling space and print w. newline | ||
sink.write(line.substring(0,line.length-1) + "\n"); | ||
stream.write(line.substring(0,line.length-1) + "\n"); | ||
} |
{ | ||
"name": "logfmt", | ||
"version": "0.3.1", | ||
"version": "0.4.0", | ||
"description": "Key-Value log line parser", | ||
@@ -5,0 +5,0 @@ "main": "logfmt.js", |
@@ -34,6 +34,6 @@ # node-logfmt | ||
Overwrite the default location by setting `.sink` | ||
Overwrite the default location by setting `.stream` | ||
```javascript | ||
var logfmt = require('logfmt'); | ||
logfmt.sink = process.stderr | ||
logfmt.stream = process.stderr | ||
@@ -40,0 +40,0 @@ logfmt.log({ "foo": "bar", "a": 14, baz: 'hello kitty'}) |
@@ -32,6 +32,6 @@ var logfmt = require('../logfmt'), | ||
var sink = logfmt.sink; | ||
logfmt.sink = mock_sink; | ||
logfmt.stream = mock_sink; | ||
logfmt.log(data); | ||
assert.equal("foo=\"hello kitty\"\n", mock_sink.logline) | ||
logfmt.sink = sink; | ||
logfmt.stream = sink; | ||
}) |
var logfmt = require('../logfmt'), | ||
assert = require('assert'); | ||
test("simple flag parses", function(){ | ||
assert.deepEqual({'hello':true}, logfmt.parse('hello')); | ||
}) | ||
test("simple key/value parses", function(){ | ||
assert.deepEqual({'hello':'kitty'}, logfmt.parse('hello=kitty')); | ||
}) | ||
test("simple boolean parses", function(){ | ||
assert.deepEqual({'foo':true, 'bar':false}, logfmt.parse('foo=true bar=false')); | ||
}) | ||
test("simple number parses", function(){ | ||
assert.deepEqual({'foo':123, 'bar':456.789}, logfmt.parse('foo=123 bar=456.789')); | ||
}) | ||
test("string with escapes", function(){ | ||
assert.deepEqual({'hello':"\'kitty\'"}, logfmt.parse('hello="\'kitty\'"')); | ||
assert.deepEqual({'hello':"\'kitty\'"}, logfmt.parse('hello=\'kitty\'')); | ||
}) | ||
test("readme string parses", function(){ | ||
@@ -18,1 +38,2 @@ var test_string = "foo=bar a=14 baz=\"hello kitty\" cool%story=bro f %^asdf "; | ||
}) | ||
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14373
19
228