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.9.0 to 0.9.1

examples/express_mime.js

4

lib/body_parser.js

@@ -27,4 +27,4 @@

req.body = req.body || {};
var is_logplex = req.get('content-type') === "application/logplex-1";
if (!is_logplex) return next();
var is_mime = req.get('content-type') === options.contentType;
if (!is_mime) return next();
req._body = true;

@@ -31,0 +31,0 @@ var buf;

@@ -36,4 +36,6 @@ var split = require('split');

exports.bodyParser = function() {
return bodyParser({content_type: "application/logplex-1", parser: logfmtBodyParser})
exports.bodyParser = function(options) {
if(options == null) options = {};
var mime = options.contentType || "application/logplex-1"
return bodyParser({contentType: mime, parser: logfmtBodyParser})
}

@@ -44,7 +46,11 @@

var bodyParserStream = function(options){
if(options == null) options = {};
var mime = options.contentType || "application/logplex-1";
return function(req, res, next) {
//setup
if (req._body) return next();
var is_logplex = req.get('content-type') === "application/logplex-1";
if (!is_logplex) return next();
console.log(req.get('content-type'), mime);
var is_mime = req.get('content-type') === mime;
if (!is_mime) return next();
req._body = true;

@@ -51,0 +57,0 @@

{
"name": "logfmt",
"version": "0.9.0",
"version": "0.9.1",
"description": "Key-Value log line parser",

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

# node-logfmt
"logfmt" is the name for a [key value logging convention](https://github.com/kr/logfmt) we've adopted at Heroku.
This library is for both logging object to logfmt format and converting
lines in logfmt format to objects. It provides a parser, a simple log
method, and both streaming and non-streaming body parsers for express.
You should use this library if you're trying to write structured logs or
if you're consuming them (especially if you're writing a logplex drain).
## install

@@ -57,12 +66,18 @@

Requires `express` to be installed
#### Streaming
##### `logfmt.bodyParserStream(opts)`
Valid Options:
contentType: defaults to 'application/logplex-1'
If you use the `logfmt.bodyParserStream()` for a body parser,
you will have a `req.body` that is a readable stream.
Pipes FTW:
```javascript
var app = require('express')();
var http = require('http');
var through = require('through');
var logfmt = require('logfmt');

@@ -72,9 +87,10 @@

// req.body is now a Readable Stream
app.post('/logs', function(req, res){
req.body.on('readable', function(){
var parsedLine = req.body.read();
if(parsedLine) console.log(parsedLine);
else res.send('OK');
})
if(!req.body) return res.send('OK');
req.body.pipe(through(function(line){
console.dir(line);
}))
res.send('OK');
})

@@ -85,6 +101,7 @@

Or you can just use the `readable` event:
```javascript
var app = require('express')();
var http = require('http');
var through = require('through');
var logfmt = require('logfmt');

@@ -94,10 +111,9 @@

// req.body is now a Readable Stream
app.post('/logs', function(req, res){
if(!req.body) return res.send('OK');
req.body.pipe(through(function(line){
console.dir(line);
}))
res.send('OK');
req.body.on('readable', function(){
var parsedLine = req.body.read();
if(parsedLine) console.log(parsedLine);
else res.send('OK');
})
})

@@ -110,2 +126,7 @@

##### `logfmt.bodyParser(opts)`
Valid Options:
contentType: defaults to 'application/logplex-1'
If you use the `logfmt.bodyParser()` for a body parser,

@@ -112,0 +133,0 @@ you will have a `req.body` that is an array of objects.

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