Socket
Socket
Sign inDemoInstall

logfmt

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

logfmt - npm Package Compare versions

Comparing version 0.11.0 to 0.11.1

1

lib/logfmt_parser.js

@@ -25,3 +25,2 @@ exports.debug = false;

else if(value == 'false') value = false;
else if(/^\d+$/.test(value)) value = parseFloat(value);
object[key] = value;

@@ -28,0 +27,0 @@ value = '';

22

package.json
{
"name": "logfmt",
"version": "0.11.0",
"version": "0.11.1",
"description": "Key-Value log line parser",

@@ -9,13 +9,15 @@ "main": "logfmt.js",

},
"bin": { "logfmt": "./bin/logfmt" },
"dependencies" : {
"split" : "*",
"through" : "*",
"readable-stream" : "*"
"bin": {
"logfmt": "./bin/logfmt"
},
"devDependencies" : {
"express" : "3.*",
"mocha" : "*",
"restify" : "*"
"dependencies": {
"split": "*",
"through": "*",
"readable-stream": "*"
},
"devDependencies": {
"express": "3.*",
"mocha": "*",
"restify": "*"
},
"repository": {

@@ -22,0 +24,0 @@ "type": "git",

@@ -31,5 +31,8 @@ # node-logfmt

logfmt.parse("foo=bar a=14 baz=\"hello kitty\" cool%story=bro f %^asdf code=H12")
//=>{ "foo": "bar", "a": 14, "baz": "hello kitty", "cool%story": "bro", "f": true, "%^asdf": true, "code" : "H12" }
//=>{ "foo": "bar", "a": '14', "baz": "hello kitty", "cool%story": "bro", "f": true, "%^asdf": true, "code" : "H12" }
```
The only conversions are from the strings `true` and `false` to their proper boolean counterparts.
We cannot arbitrarily convert numbers because we will drop precision for numbers that require more than 32 bits to represent them.
## logging

@@ -125,4 +128,11 @@

## express middleware
## express/restify middleware
```javascript
// streaming
app.use(logfmt.bodyParserStream());
// buffering
app.use(logfmt.bodyParser());
```
### Streaming

@@ -129,0 +139,0 @@

@@ -5,102 +5,106 @@ var logfmt = require('../logfmt'),

test("stream body parser skips parsing when req._body is true", function(){
suite('logfmt.bodyParserStream', function() {
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
test("skips parsing when req._body is true", function(){
mockReq._body = true;
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(err){
assert.equal(mockReq.body, undefined)
};
mockReq._body = true;
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
})
var next = function(err){
assert.equal(mockReq.body, undefined)
};
test("stream body parser skips parsing when contentType does not match", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
})
var next = function(err){
assert.equal(mockReq.body, undefined)
};
test("skips parsing when contentType does not match", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
var next = function(err){
assert.equal(mockReq.body, undefined)
};
})
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
test("stream body parser converts body lines to object read stream", function(done){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(){};
})
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
test("converts body lines to object read stream", function(done){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(){};
mockReq.body.on('readable', function(){
var data = mockReq.body.read();
assert.deepEqual(data, {hello: 'kitty'})
done();
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
mockReq.body.on('readable', function(){
var data = mockReq.body.read();
assert.deepEqual(data, {hello: 'kitty'})
done();
})
})
})
test("stream body parser accepts contentType option", function(done){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(){};
test("accepts contentType option", function(done){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(){};
var parser = logfmt.bodyParserStream({contentType: 'foo'});
parser(mockReq, null, next)
var parser = logfmt.bodyParserStream({contentType: 'foo'});
parser(mockReq, null, next)
mockReq.body.on('readable', function(){
var data = mockReq.body.read();
assert.deepEqual(data, {hello: 'kitty'})
done();
mockReq.body.on('readable', function(){
var data = mockReq.body.read();
assert.deepEqual(data, {hello: 'kitty'})
done();
})
})
})
test("body parser parses all the lines", function(done){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty\n');
mockReq.push('foo=bar\n');
mockReq.push('path=/');
mockReq.push(null);
var next = function(){};
test("parses all the lines", function(done){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty\n');
mockReq.push('foo=bar\n');
mockReq.push('path=/\n');
mockReq.push(null);
var next = function(){};
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
var parser = logfmt.bodyParserStream();
parser(mockReq, null, next)
var matches = [{path: '/'}, {foo: 'bar'}, {hello: 'kitty'}];
mockReq.body.on('readable', function(){
var data = mockReq.body.read();
assert.deepEqual(data, matches.pop())
if(matches.length == 0) done();
var matches = [{path: '/'}, {foo: 'bar'}, {hello: 'kitty'}];
mockReq.body.on('readable', function(){
var data = mockReq.body.read();
assert.deepEqual(data, matches.pop())
if(matches.length == 0) done();
})
})
})

@@ -5,111 +5,113 @@ var logfmt = require('../logfmt'),

test("body parser skips parsing when req._body is true", function(){
suite('logfmt.bodyParser', function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
test("skips parsing when req._body is true", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
mockReq._body = true;
mockReq._body = true;
var next = function(err){
assert.equal(mockReq.body, undefined)
};
var next = function(err){
assert.equal(mockReq.body, undefined)
};
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
test("body parser skips parsing when contentType does not match", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
test("skips parsing when contentType does not match", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(err){
assert.equal(mockReq.body, undefined)
};
var next = function(err){
assert.equal(mockReq.body, undefined)
};
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
})
test("body parser accepts contentType option", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
test("accepts contentType option", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'foo';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
};
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
};
var parser = logfmt.bodyParser({contentType: 'foo'});
parser(mockReq, null, next)
})
var parser = logfmt.bodyParser({contentType: 'foo'});
parser(mockReq, null, next)
})
test("body parser converts body lines to objects", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
test("converts body lines to objects", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty');
mockReq.push(null);
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
};
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
};
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
test("body parser parses all the lines", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty\n');
mockReq.push('foo=bar');
mockReq.push(null);
test("parses all the lines", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty\n');
mockReq.push('foo=bar');
mockReq.push(null);
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
assert.deepEqual(mockReq.body[1], {foo: 'bar'})
assert.equal(mockReq.body[2], undefined)
};
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
assert.deepEqual(mockReq.body[1], {foo: 'bar'})
assert.equal(mockReq.body[2], undefined)
};
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
test("body parser ignores trailing newline", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty\n');
mockReq.push(null);
test("ignores trailing newline", function(){
var mockReq = new stream.Readable;
mockReq.header = function(){
return 'application/logplex-1';
}
mockReq._read = function(){};
mockReq.push('hello=kitty\n');
mockReq.push(null);
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
assert.equal(mockReq.body[1], undefined)
};
var next = function(err){
assert.deepEqual(mockReq.body[0], {hello: 'kitty'})
assert.equal(mockReq.body[1], undefined)
};
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
var parser = logfmt.bodyParser();
parser(mockReq, null, next)
})
})

@@ -13,89 +13,91 @@ var logfmt = require('../logfmt'),

test("logs the time", function(done){
logfmt.time(function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^elapsed=\dms\n$/.test(actual), actual)
done();
suite('logfmt.time', function() {
test("logs the time", function(done){
logfmt.time(function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^elapsed=\dms\n$/.test(actual), actual)
done();
})
})
})
test("logs the time with your label", function(done){
logfmt.time('time', function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^time=\dms\n$/.test(actual), actual)
done();
test("logs the time with your label", function(done){
logfmt.time('time', function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^time=\dms\n$/.test(actual), actual)
done();
})
})
})
test("logs the time with your label and persistent data", function(done){
logfmt.time('time', {foo: 'bar'}, function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^foo=bar time=\dms\n$/.test(actual), actual)
done();
test("logs the time with your label and persistent data", function(done){
logfmt.time('time', {foo: 'bar'}, function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^foo=bar time=\dms\n$/.test(actual), actual)
done();
})
})
})
test("logs the time with persistent data", function(done){
logfmt.time({foo: 'bar'}, function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^foo=bar elapsed=\dms\n$/.test(actual), actual)
logger.log({moar: 'data'});
var actual = mock_sink.logline;
assert(/^moar=data foo=bar elapsed=\dms\n$/.test(actual), actual)
done();
})
})
//now we're using setTimeout to ensure the elapsed
//time reflects a known delay
test("accurancy in milliseconds", function(done){
logfmt.time(function(logger){
var wrapped = function() {
test("logs the time with persistent data", function(done){
logfmt.time({foo: 'bar'}, function(logger){
logger.log();
var actual = mock_sink.logline;
assert(/^elapsed=2\dms\n$/.test(actual), actual)
assert(/^foo=bar elapsed=\dms\n$/.test(actual), actual)
logger.log({moar: 'data'});
var actual = mock_sink.logline;
assert(/^moar=data foo=bar elapsed=\dms\n$/.test(actual), actual)
done();
}
setTimeout(wrapped, 20);
})
})
})
test("logs the time with your label and data", function(done){
logfmt.time('time', function(logger){
logger.log({foo: 'bar'});
var actual = mock_sink.logline;
assert(/^foo=bar time=\dms\n$/.test(actual), actual)
done();
//now we're using setTimeout to ensure the elapsed
//time reflects a known delay
test("accurancy in milliseconds", function(done){
logfmt.time(function(logger){
var wrapped = function() {
logger.log();
var actual = mock_sink.logline;
assert(/^elapsed=2\dms\n$/.test(actual), actual)
done();
}
setTimeout(wrapped, 20);
})
})
})
test("supports log(data, stream) interface", function(done){
logfmt.time(function(logger){
logger.log({foo: 'bar'}, mock_sink);
var actual = mock_sink.logline;
assert(/^foo=bar elapsed=\dms\n$/.test(actual), actual)
done();
test("logs the time with your label and data", function(done){
logfmt.time('time', function(logger){
logger.log({foo: 'bar'});
var actual = mock_sink.logline;
assert(/^foo=bar time=\dms\n$/.test(actual), actual)
done();
})
})
})
// tests you can pass the logger into a closure
// and call `log` multiple times.
// uses setTimeout to ensure the timing happens in 20ms
test("can log twice", function(done){
logfmt.time(function(logger){
logger.log({foo: 'bar'}, mock_sink);
var actual = mock_sink.logline;
assert(/^foo=bar elapsed=\dms\n$/.test(actual), actual)
var wrapped = function() {
logger.log({bar: 'foo'});
test("supports log(data, stream) interface", function(done){
logfmt.time(function(logger){
logger.log({foo: 'bar'}, mock_sink);
var actual = mock_sink.logline;
assert(/^bar=foo elapsed=2\dms\n$/.test(actual), actual)
assert(/^foo=bar elapsed=\dms\n$/.test(actual), actual)
done();
}
setTimeout(wrapped, 20);
})
})
// tests you can pass the logger into a closure
// and call `log` multiple times.
// uses setTimeout to ensure the timing happens in 20ms
test("can log twice", function(done){
logfmt.time(function(logger){
logger.log({foo: 'bar'}, mock_sink);
var actual = mock_sink.logline;
assert(/^foo=bar elapsed=\dms\n$/.test(actual), actual)
var wrapped = function() {
logger.log({bar: 'foo'});
var actual = mock_sink.logline;
assert(/^bar=foo elapsed=2\dms\n$/.test(actual), actual)
done();
}
setTimeout(wrapped, 20);
})
})
})

@@ -11,27 +11,29 @@ var logfmt = require('../logfmt'),

test("logs simple key value pairs", function(){
var data = {foo: 'bar', a: 14}
logfmt.log(data, mock_sink);
assert.equal("foo=bar a=14\n", mock_sink.logline)
})
suite('logfmt.log', function() {
test("logs simple key value pairs", function(){
var data = {foo: 'bar', a: 14}
logfmt.log(data, mock_sink);
assert.equal("foo=bar a=14\n", mock_sink.logline)
})
test("logs true and false as strings", function(){
var data = {foo: true, bar: false}
logfmt.log(data, mock_sink);
assert.equal("foo=true bar=false\n", mock_sink.logline)
})
test("logs true and false as strings", function(){
var data = {foo: true, bar: false}
logfmt.log(data, mock_sink);
assert.equal("foo=true bar=false\n", mock_sink.logline)
})
test("quotes strings with spaces in them", function(){
var data = {foo: "hello kitty"}
logfmt.log(data, mock_sink);
assert.equal("foo=\"hello kitty\"\n", mock_sink.logline)
})
test("quotes strings with spaces in them", function(){
var data = {foo: "hello kitty"}
logfmt.log(data, mock_sink);
assert.equal("foo=\"hello kitty\"\n", mock_sink.logline)
})
test("setting sink at object level", function(){
var data = {foo: "hello kitty"}
var sink = logfmt.sink;
logfmt.stream = mock_sink;
logfmt.log(data);
assert.equal("foo=\"hello kitty\"\n", mock_sink.logline)
logfmt.stream = sink;
test("setting sink at object level", function(){
var data = {foo: "hello kitty"}
var sink = logfmt.sink;
logfmt.stream = mock_sink;
logfmt.log(data);
assert.equal("foo=\"hello kitty\"\n", mock_sink.logline)
logfmt.stream = sink;
})
})
var logfmt = require('../logfmt'),
assert = require('assert');
test("simple flag parses", function(){
assert.deepEqual({'hello':true}, logfmt.parse('hello'));
})
suite('logfmt.parse', function() {
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 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 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('big numbers dont lose precision', function(){
var parsed = logfmt.parse("thing=90071992547409934");
assert.equal(parsed.thing.toString(), '90071992547409934');
})
test("string with escapes", function(){
assert.deepEqual({'hello':"\'kitty\'"}, logfmt.parse('hello="\'kitty\'"'));
assert.deepEqual({'hello':"\'kitty\'"}, logfmt.parse('hello=\'kitty\''));
})
test("number parse to strings", function(){
assert.deepEqual({'foo':'123', 'bar':'456.789'},
logfmt.parse('foo=123 bar=456.789'));
})
test("readme string parses", function(){
var test_string = "foo=bar a=14 baz=\"hello kitty\" cool%story=bro f %^asdf ";
test_string += "code=H12 path=/hello/user@foo.com/close";
var result = logfmt.parse(test_string)
assert.equal( "H12", result["code"])
assert.equal( "bar", result["foo"])
assert.equal(14, result.a)
assert.equal("hello kitty", result['baz'])
assert.equal('bro', result['cool%story'])
assert.equal(true, result.f)
assert.equal(true, result['%^asdf'])
assert.equal('/hello/user@foo.com/close', result['path'])
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(){
var test_string = "foo=bar a=14 baz=\"hello kitty\" "
test_string += "cool%story=bro f %^asdf ";
test_string += "code=H12 path=/hello/user@foo.com/close";
var result = logfmt.parse(test_string)
assert.equal( "H12", result["code"])
assert.equal( "bar", result["foo"])
assert.equal(14, result.a)
assert.equal("hello kitty", result['baz'])
assert.equal('bro', result['cool%story'])
assert.equal(true, result.f)
assert.equal(true, result['%^asdf'])
assert.equal('/hello/user@foo.com/close', result['path'])
})
})

@@ -11,18 +11,20 @@ var logfmt = require('../logfmt'),

test("key value pairs are restored", function(){
var data = {foo: 'bar', a: 14}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline));
})
suite('logfmt.parse(logfmt.log)', function(){
test("key value pairs are restored", function(){
var data = {foo: 'bar', a: 14}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline));
})
test("true and false are restored", function(){
var data = {foo: true, bar: false}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline));
})
test("true and false are restored", function(){
var data = {foo: true, bar: false}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline));
})
test("quoted strings are restored", function(){
var data = {foo: "hello kitty"}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline))
test("quoted strings are restored", function(){
var data = {foo: "hello kitty"}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline))
})
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc