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 1.0.0-pre to 1.0.0-pre2

lib/stringify.js

24

lib/logger.js

@@ -5,4 +5,2 @@ var _ = require('lodash');

if(stream == undefined) stream = this.stream;
var line = '';
var logData = _.extend({}, this.defaultData, data);

@@ -15,23 +13,3 @@

Object.keys(logData).forEach(function(key){
var value = logData[key];
var is_null = false;
if(value == null) {
is_null = true;
value = '';
}
else value = value.toString();
var needs_quoting = value.indexOf(' ') > -1 || value.indexOf('=') > -1;
var needs_escaping = value.indexOf('"') > -1;
if(needs_escaping) value = value.replace(/"/g, '\\"');
if(needs_quoting) value = '"' + value + '"';
if(value === '' && !is_null) value = '""';
line += key + '=' + value + ' ';
})
//trim traling space and print w. newline
stream.write(line.substring(0,line.length-1) + "\n");
stream.write(this.stringify(logData) + "\n");
}

@@ -38,0 +16,0 @@

@@ -7,2 +7,3 @@ var _ = require('lodash');

var requestLogger = require('./lib/request_logger');
var serializer = require('./lib/stringify');

@@ -19,4 +20,7 @@ function logfmt(stream, defaultData, timer) {

//Build up logfmt prototype
_.extend(logfmt.prototype, logger);
logfmt.prototype.stringify = serializer.stringify;
logfmt.prototype.parse = logfmtParser.parse;

@@ -23,0 +27,0 @@

{
"name": "logfmt",
"version": "1.0.0-pre",
"version": "1.0.0-pre2",
"description": "key=value logger and parser",

@@ -5,0 +5,0 @@ "main": "logfmt.js",

@@ -24,6 +24,7 @@ # node-logfmt

var logfmt = require('logfmt');
```
logfmt.log({foo: "bar", a: 14, baz: 'hello kitty'})
//=> foo=bar a=14 baz="hello kitty"
Parse a line in logfmt format
```javascript
logfmt.parse('foo=bar a=14 baz="hello kitty"')

@@ -33,7 +34,21 @@ //=> { foo: "bar", a: 14, baz: 'hello kitty'}

the logfmt function is a singleton that works directly from require. however, you can use the `new` command
to make another logfmt like this one.
Serialize an object to logfmt format
```javascript
logfmt.serialize({foo: "bar", a: 14, baz: 'hello kitty'})
//=> 'foo=bar a=14 baz="hello kitty"'
```
Log an object to `logfmt.stream` (defaults to STDOUT)
```javascript
logfmt.log({foo: "bar", a: 14, baz: 'hello kitty'})
// foo=bar a=14 baz="hello kitty"
//=> undefined
```
The `logfmt` module is a singleton that works directly from require.
Because it is also a function, you can use the idiom `new logfmt` to create
a new `logfmt` object.
## parser

@@ -51,6 +66,9 @@

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.
We cannot arbitrarily convert numbers because that will drop precision for numbers that require more than 32 bits to represent them.
## logging
Uses the `logfmt.serialize` function to write the result to `logfmt.stream`
### `logfmt.log(object, [stream])`

@@ -57,0 +75,0 @@

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

var OutStream = require('./outstream');
var mock_sink = new OutStream;
suite('logfmt.log', function() {
test("logs simple key value pairs", function(){
test("passing location as second param", function(){
var mock_sink = new OutStream;
var data = {foo: 'bar', a: 14}

@@ -15,42 +15,4 @@ logfmt.log(data, mock_sink);

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 equals in them", function(){
var data = {foo: "hello=kitty"}
logfmt.log(data, mock_sink);
assert.equal("foo=\"hello=kitty\"\n", mock_sink.logline)
})
test("escapes quotes within strings with spaces in them", function(){
var data = {foo: 'hello my "friend"'}
logfmt.log(data, mock_sink);
assert.equal('foo="hello my \\"friend\\""\n', mock_sink.logline)
var data = {foo: 'hello my "friend" whom I "love"'}
logfmt.log(data, mock_sink);
assert.equal('foo="hello my \\"friend\\" whom I \\"love\\""\n', mock_sink.logline)
})
test("undefined is logged as nothing", function(){
var data = {foo: undefined}
logfmt.log(data, mock_sink);
assert.equal("foo=\n", mock_sink.logline)
})
test("null is logged as nothing", function(){
var data = {foo: null}
logfmt.log(data, mock_sink);
assert.equal("foo=\n", mock_sink.logline)
})
test("setting sink at object level", function(){
var mock_sink = new OutStream;
var data = {foo: "hello kitty"}

@@ -57,0 +19,0 @@ var stream = logfmt.stream;

var logfmt = require('../logfmt'),
assert = require('assert');
var OutStream = require('./outstream');
var mock_sink = new OutStream;
suite('logfmt.parse(logfmt.log)', function(){
suite('roundtrip', 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));
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)));
})

@@ -16,4 +12,3 @@

var data = {foo: true, bar: false}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline));
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)));
})

@@ -23,4 +18,3 @@

var data = {foo: "hello kitty"}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline))
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)))
})

@@ -30,4 +24,3 @@

var data = {foo: "hello=kitty"}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline))
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)))
})

@@ -37,7 +30,5 @@

var data = {foo: 'hello my "friend"'}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline))
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)))
data = {foo: 'hello my "friend" whom I "love"'}
logfmt.log(data, mock_sink);
assert.deepEqual(data, logfmt.parse(mock_sink.logline))
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)))
})

@@ -47,4 +38,3 @@

var data = {foo: null}
logfmt.log(data, mock_sink);
assert.deepEqual({foo: null}, logfmt.parse(mock_sink.logline))
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)))
})

@@ -54,5 +44,4 @@

var data = {foo: ''}
logfmt.log(data, mock_sink);
assert.deepEqual({foo: ''}, logfmt.parse(mock_sink.logline))
assert.deepEqual(data, logfmt.parse(logfmt.stringify(data)))
})
})

@@ -9,2 +9,12 @@ var logfmt = require('../logfmt'),

test('parses', function(){
var data = logfmt.parse('foo=bar');
assert.equal('bar', data.foo);
})
test('stringifies', function(){
var data = logfmt.stringify({foo: 'bar'});
assert.equal('foo=bar', data);
})
test('maxErrorLines is configured', function(){

@@ -11,0 +21,0 @@ assert.equal(10, logfmt.maxErrorLines);

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