Compare commits

...

1 Commits

Author SHA1 Message Date
Will Toozs 482e4aa2ee
stringify optimizations 2022-08-11 14:35:00 +02:00
3 changed files with 7 additions and 3 deletions

View File

@ -3,6 +3,8 @@
const os = require('os'); const os = require('os');
const safeJSONStringify = require('safe-json-stringify'); const safeJSONStringify = require('safe-json-stringify');
var stringify = require('fast-json-stable-stringify');
/* /*
* This function safely stringifies JSON. If an exception occcurs (due to * This function safely stringifies JSON. If an exception occcurs (due to
* circular references, exceptions thrown from object getters etc.), the module * circular references, exceptions thrown from object getters etc.), the module
@ -14,7 +16,7 @@ const safeJSONStringify = require('safe-json-stringify');
function safeStringify(obj) { function safeStringify(obj) {
let str; let str;
try { try {
str = JSON.stringify(obj); str = stringify(obj);
} catch (e) { } catch (e) {
// fallback to remove circular object references or other exceptions // fallback to remove circular object references or other exceptions
// eslint-disable-next-line no-param-reassign // eslint-disable-next-line no-param-reassign
@ -69,8 +71,9 @@ class SimpleLogger {
logFields.hostname = this.hostname; logFields.hostname = this.hostname;
logFields.pid = process.pid; logFields.pid = process.pid;
const safeString = safeStringify(logFields);
this.streams.forEach(s => s.stream this.streams.forEach(s => s.stream
.write(`${safeStringify(logFields)}\n`)); .write(`${safeString}\n`));
} }
info(fields, message) { info(fields, message) {

View File

@ -33,6 +33,7 @@
}, },
"homepage": "https://github.com/scality/werelogs#readme", "homepage": "https://github.com/scality/werelogs#readme",
"dependencies": { "dependencies": {
"fast-json-stable-stringify": "^2.1.0",
"safe-json-stringify": "^1.2.0" "safe-json-stringify": "^1.2.0"
}, },
"devDependencies": { "devDependencies": {

View File

@ -1064,7 +1064,7 @@ fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525" resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q== integrity sha512-f3qQ9oQy9j2AhBe/H9VC91wLmKBCCU/gDOnKNAYG5hswO7BLKj09Hc5HYNz9cGI++xlpDCIgDaitVs03ATR84Q==
fast-json-stable-stringify@^2.0.0: fast-json-stable-stringify@^2.0.0, fast-json-stable-stringify@^2.1.0:
version "2.1.0" version "2.1.0"
resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633" resolved "https://registry.yarnpkg.com/fast-json-stable-stringify/-/fast-json-stable-stringify-2.1.0.tgz#874bf69c6f404c2b5d99c481341399fd55892633"
integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw== integrity sha512-lhd/wF+Lk98HZoTCtlVraHtfh5XYijIjalXck7saUtuanSDyLMxnHhSXEDJqHxD7msR8D0uCmqlkwjCV8xvwHw==