pino-mozlog
Advanced tools
Comparing version
14
index.js
#!/usr/bin/env node | ||
'use strict'; | ||
@@ -7,11 +8,12 @@ const pump = require('pump'); | ||
const { | ||
createParseFunction, | ||
createTransformFunction, | ||
parseOptions, | ||
} = require('./src/index'); | ||
var _require = require('./src/index'); | ||
const createParseFunction = _require.createParseFunction, | ||
createTransformFunction = _require.createTransformFunction, | ||
parseOptions = _require.parseOptions; | ||
const options = parseOptions(process.argv.slice(2)); | ||
const mozlogTransport = through.obj(createTransformFunction({ options })); | ||
pump(process.stdin, split(createParseFunction({ options })), mozlogTransport); | ||
pump(process.stdin, split(createParseFunction({ options })), mozlogTransport); |
{ | ||
"name": "pino-mozlog", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "A transport for transforming pino logs into mozlog", | ||
@@ -10,2 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"build": "rimraf dist/ && babel -d dist/ index.js && babel -d dist/src/ src/ && cp -r LICENSE.txt README.md package.json dist/", | ||
"prettier": "prettier --write '**'", | ||
@@ -26,5 +27,9 @@ "test": "jest" | ||
"devDependencies": { | ||
"babel-cli": "^6.26.0", | ||
"babel-plugin-transform-object-rest-spread": "^6.26.0", | ||
"babel-preset-env": "^1.7.0", | ||
"jest": "^23.4.2", | ||
"jest-json-schema": "^2.0.1", | ||
"prettier": "^1.14.0" | ||
"prettier": "^1.14.0", | ||
"rimraf": "^2.6.2" | ||
}, | ||
@@ -31,0 +36,0 @@ "jest": { |
@@ -0,1 +1,5 @@ | ||
'use strict'; | ||
function _objectWithoutProperties(obj, keys) { var target = {}; for (var i in obj) { if (keys.indexOf(i) >= 0) continue; if (!Object.prototype.hasOwnProperty.call(obj, i)) continue; target[i] = obj[i]; } return target; } | ||
const minimist = require('minimist'); | ||
@@ -9,7 +13,7 @@ const pinoSyslog = require('pino-syslog/lib/utils'); | ||
type: 'app.log', | ||
debug: false, | ||
debug: false | ||
}; | ||
const createParseFunction = ({ options = DEFAULT_OPTIONS } = {}) => { | ||
return (data) => { | ||
return data => { | ||
try { | ||
@@ -19,3 +23,3 @@ return JSON.parse(data); | ||
if (options.debug) { | ||
console.error('Could not parse:', data); | ||
console.error('[pino-mozlog] could not parse:', { data }); | ||
} | ||
@@ -28,18 +32,15 @@ } | ||
const convertTimestampToRFC3339 = (timestamp) => { | ||
const convertTimestampToRFC3339 = timestamp => { | ||
return toRFC3339(new Date(timestamp)); | ||
}; | ||
const format = ( | ||
{ | ||
hostname, | ||
level, | ||
name, | ||
pid, | ||
time, | ||
v, // this field is ignored | ||
...fields | ||
}, | ||
options = DEFAULT_OPTIONS | ||
) => { | ||
const format = (_ref, options = DEFAULT_OPTIONS) => { | ||
let hostname = _ref.hostname, | ||
level = _ref.level, | ||
name = _ref.name, | ||
pid = _ref.pid, | ||
time = _ref.time, | ||
v = _ref.v, | ||
fields = _objectWithoutProperties(_ref, ['hostname', 'level', 'name', 'pid', 'time', 'v']); | ||
return { | ||
@@ -54,3 +55,3 @@ EnvVersion: ENV_VERSION, | ||
Timestamp: time, | ||
Type: options.type, | ||
Type: options.type | ||
}; | ||
@@ -61,7 +62,11 @@ }; | ||
_format = format, | ||
options = DEFAULT_OPTIONS, | ||
options = DEFAULT_OPTIONS | ||
} = {}) => { | ||
return (record, enc, cb) => { | ||
if (typeof record.time !== 'undefined') { | ||
console.log(JSON.stringify(_format(record, options))); | ||
try { | ||
if (typeof record.time !== 'undefined') { | ||
console.log(JSON.stringify(_format(record, options))); | ||
} | ||
} catch (error) { | ||
console.error('[pino-mozlog] could not format:', { error, record }); | ||
} | ||
@@ -73,11 +78,15 @@ | ||
const parseOptions = (argv) => { | ||
const parseOptions = argv => { | ||
const keys = Object.keys(DEFAULT_OPTIONS); | ||
const { _, ...options } = minimist(argv, { | ||
boolean: keys.filter((k) => typeof DEFAULT_OPTIONS[k] === 'boolean'), | ||
var _minimist = minimist(argv, { | ||
boolean: keys.filter(k => typeof DEFAULT_OPTIONS[k] === 'boolean'), | ||
default: DEFAULT_OPTIONS, | ||
string: keys.filter((k) => typeof DEFAULT_OPTIONS[k] === 'string'), | ||
unknown: () => false, | ||
string: keys.filter(k => typeof DEFAULT_OPTIONS[k] === 'string'), | ||
unknown: () => false | ||
}); | ||
const _ = _minimist._, | ||
options = _objectWithoutProperties(_minimist, ['_']); | ||
return options; | ||
@@ -93,3 +102,3 @@ }; | ||
format, | ||
parseOptions, | ||
}; | ||
parseOptions | ||
}; |
21477
4.75%91
2.25%7
133.33%