Comparing version 0.2.0 to 0.3.0
exports.parse = function(line) { | ||
var pairs = line.match(/([a-zA-Z0-9\%\_\-\.\^]+)=?(([a-zA-Z0-9\.\-\_\.\/]+)|("([^\"]+)"))?/g) | ||
var pairs = line.match(/([a-zA-Z0-9\%\_\-\.\^]+)=?(([a-zA-Z0-9\.\-\_\.\/\@]+)|("([^\"]+)"))?/g) | ||
var attrs = {} | ||
@@ -12,7 +12,11 @@ | ||
value = parts.join("=") | ||
//strip quotes | ||
if(value[0] == '"'){ | ||
value = value.substring(1, value.length-1) | ||
} | ||
//casts | ||
if(value == '') value = true; | ||
if(/\A\d+\Z/.test(value)) value = parseInt(value); | ||
else if(value == 'true') value = true; | ||
else if(value == 'false') value = false; | ||
else if(/^\d+$/.test(value)) value = parseInt(value); | ||
attrs[key] = value | ||
@@ -24,2 +28,18 @@ }) | ||
exports.sink = process.stdout; | ||
exports.log = function(data, sink) { | ||
if(sink == undefined) sink = exports.sink; | ||
var line = ''; | ||
Object.keys(data).forEach(function(key){ | ||
var value = data[key].toString(); | ||
if(value.indexOf(' ') > -1) value = '"' + value + '"'; | ||
line += key + '=' + value + ' '; | ||
}) | ||
//trim traling space and print w. newline | ||
sink.write(line.substring(0,line.length-1) + "\n"); | ||
} | ||
exports.body_parser = require('./logfmt-body-parser.js') |
{ | ||
"name": "logfmt", | ||
"version": "0.2.0", | ||
"version": "0.3.0", | ||
"description": "Key-Value log line parser", | ||
"main": "logfmt.js", | ||
"scripts": { | ||
"test": "bin/test" | ||
"test": "./test/test" | ||
}, | ||
"bin": { "logfmt": "./bin/logfmt" }, | ||
"devDependencies" : { | ||
"express" : "*" | ||
"express" : "3.*", | ||
"mocha" : "*" | ||
}, | ||
@@ -19,3 +20,4 @@ "repository": { | ||
"Log", | ||
"Parser" | ||
"Parser", | ||
"logfmt" | ||
], | ||
@@ -22,0 +24,0 @@ "author": "csquared", |
@@ -7,9 +7,46 @@ # node-logfmt | ||
## use | ||
# use | ||
### straight up | ||
```javascript | ||
var logfmt = require('logfmt'); | ||
``` | ||
## logging | ||
### `logfmt.log()` | ||
Defaults to logging to `process.stdout` | ||
```javascript | ||
var logfmt = require('logfmt'); | ||
logfmt.log({ "foo": "bar", "a": 14, baz: 'hello kitty'}) | ||
//=> foo=bar a=14 baz="hello kitty" | ||
``` | ||
### customizing logging location | ||
`log()` Accepts as 2nd arg anything that responds to `write(string)` | ||
```javascript | ||
var logfmt = require('logfmt'); | ||
logfmt.log({ "foo": "bar", "a": 14, baz: 'hello kitty'}, process.stderr) | ||
//=> foo=bar a=14 baz="hello kitty" | ||
``` | ||
Overwrite the default location by setting `.sink` | ||
```javascript | ||
var logfmt = require('logfmt'); | ||
logfmt.sink = process.stderr | ||
logfmt.log({ "foo": "bar", "a": 14, baz: 'hello kitty'}) | ||
//=> foo=bar a=14 baz="hello kitty" | ||
``` | ||
## parser | ||
### `logfmt.parse()` | ||
```javascript | ||
var logfmt = require('logfmt'); | ||
logfmt.parse("foo=bar a=14 baz=\"hello kitty\" cool%story=bro f %^asdf code=H12") | ||
@@ -21,2 +58,4 @@ //=>{ "foo": "bar", "a": 14, "baz": "hello kitty", "cool%story": "bro", "f": true, "%^asdf": true, "code" : "H12" } | ||
logfmt.body_parser(); | ||
```javascript | ||
@@ -48,11 +87,22 @@ var logfmt = require('logfmt').body_parser; | ||
### command line | ||
accepts lines on STDIN and converts them to json | ||
echo "foo=bar a=14 baz=\"hello kitty\" cool%story=bro f %^asdf" | logfmt | ||
{ "foo": "bar", "a": 14, "baz": "hello kitty", "cool%story": "bro", "f": true, "%^asdf": true } | ||
#### reverse | ||
accepts JSON on STDIN and converts them to logfmt | ||
echo '{ "foo": "bar", "a": 14, "baz": "hello kitty", "cool%story": "bro", "f": true, "%^asdf": true }' | logfmt -r | ||
foo=bar a=14 baz="hello kitty" cool%story=bro f=true %^asdf=true | ||
echo "foo=bar a=14 baz=\"hello kitty\" cool%story=bro f %^asdf" | logfmt | logfmt -r | logfmt | ||
{ "foo": "bar", "a": 14, "baz": "hello kitty", "cool%story": "bro", "f": true, "%^asdf": true } | ||
## caveats | ||
I use a regex and haven't optimized much for performance yet. |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
11134
16
171
106
2