metric-logger
Advanced tools
Comparing version 1.0.2 to 1.0.3
62
index.js
module.exports = { | ||
logMetric: logMetric, | ||
// logMetric2: logMetric2, | ||
logMetric: logMetric | ||
}; | ||
function logMetric(metric) { | ||
let o = Object.assign({ | ||
node_env: process.env.NODE_ENV | ||
function logMetric(metric, opts) { | ||
let obj = Object.assign({ | ||
node_env: process.env.NODE_ENV, | ||
// pid: process.pid | ||
}, metric); | ||
let log = 'METRIC ' + toKeyValue(obj, opts); | ||
console.log(log); | ||
return log; | ||
} | ||
var log = "METRIC " + Object.keys(o).map((key) => { | ||
let val = o[key]; | ||
var string = JSON.stringify(val) | ||
if (string && string.indexOf('{') == 0) { | ||
string = string.replace(/"/g,'\\"'); | ||
string = '"'+string+'"'; | ||
function toKeyValue(obj, opts = {}, prefix) { | ||
return Object.keys(obj).map(k => { | ||
const v = obj[k]; | ||
const prefixedKey = typeof prefix !== typeof undefined ? prefix + "__" + k : k; //add prefix if provided | ||
if (opts.deepPrefix && typeof v === typeof {} && !Array.isArray(v)) { | ||
return toKeyValue(v, opts, prefixedKey); | ||
} | ||
return `${prefixedKey}=${escape(v)}` | ||
}).join(' ') | ||
} | ||
return key + '=' + string | ||
const escape = v => { | ||
let string = JSON.stringify(v); | ||
return string.startsWith('{') || string.startsWith('[') ? JSON.stringify(string) : string | ||
} | ||
}).join(" "); | ||
console.log(log); | ||
return log; | ||
} | ||
// | ||
// function logMetric2(metric) { | ||
// let o = Object.assign({ | ||
// node_env: process.env.NODE_ENV | ||
// }, metric); | ||
// | ||
// | ||
// var log = "METRIC " + Object.keys(o).map((key) => { | ||
// let val = o[key]; | ||
// var string = JSON.stringify(val) | ||
// if (string && string.indexOf('{') == 0) { | ||
// string = string.replace(/"/g, '\\"'); | ||
// string = '"' + string + '"'; | ||
// } | ||
// | ||
// return key + '=' + string | ||
// | ||
// }).join(" "); | ||
// console.log(log); | ||
// return log; | ||
// } | ||
// | ||
{ | ||
"name": "metric-logger", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -17,8 +17,10 @@ var logMetric = require('../index.js').logMetric; | ||
it('should serialise json properly', function() { | ||
var json = { w: 1, x: "blah", w: [0,1,"2"], z: { a: "1", 1: 2 }}; | ||
var json = { w: 1, x: "blah", w: [0,1,"spaced words"], z: { a: "1", 1: 2, "withquotes": '"test"(*&^%$£!' }}; | ||
assert.equal(logMetric({ json: json }), expected('json='+JSON.stringify('{\"w\":[0,1,\"spaced words\"],\"x\":\"blah\",\"z\":{\"1\":2,\"a\":\"1\",\"withquotes\":\"\\"test\\"(*&^%$£!\"}}') )); | ||
}); | ||
assert.equal(logMetric({ json: json }), expected('json="{\\"w\\":[0,1,\\"2\\"],\\"x\\":\\"blah\\",\\"z\\":{\\"1\\":2,\\"a\\":\\"1\\"}}"' )); | ||
it('should prefix deep structures', function() { | ||
var json = { w: 1, x: "blah", deep: {"z": 5}, array: [1,2,"spaced words"]}; | ||
assert.equal(logMetric({ json: json }, { deepPrefix: true}), expected('json__w=1 json__x="blah" json__deep__z=5 json__array="[1,2,\\"spaced words\\"]"' )); | ||
}); | ||
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
2768
4
66
1