btrz-logger
Advanced tools
Comparing version 5.0.5 to 5.0.6
38
index.js
@@ -1,3 +0,39 @@ | ||
"use strict"; | ||
function monkeyPatchMongoObjectID() { | ||
// "bson" is not listed in our package.json dependencies, but is instead indirectly imported by "mongodb". | ||
// We do this because we want to use whatever version of "bson" is used by the "mongodb" package. We are trying | ||
// to target the same version of "mongodb" that is used by "btrz-simple-dao". | ||
const {ObjectId} = require("bson"); | ||
// The 'bson' library at version 1.1.6 specifies a custom function to format an ObjectId when it is being inspected | ||
// with 'util.inspect'. It is buggy and causes an error to be thrown. We replace the custom inspect function in | ||
// the ObjectId prototype so that we do not encounter this bug. | ||
// See https://github.com/mongodb/js-bson/blob/6fc7a87d64369cdb64719f0de944d6aa3c70ee75/lib/bson/objectid.js#L207 | ||
delete ObjectId.prototype[Symbol.for('nodejs.util.inspect.custom')]; | ||
delete ObjectId.prototype.inspect; | ||
// From "bson" source code. Precomputed hex table enables speedy hex string conversion | ||
const hexTable = []; | ||
for (let i = 0; i < 256; i++) { | ||
hexTable[i] = (i <= 15 ? '0' : '') + i.toString(16); | ||
} | ||
ObjectId.prototype[Symbol.for("nodejs.util.inspect.custom")] = function inspect() { | ||
let hexString = ""; | ||
if (this.id instanceof Buffer) { | ||
for (let i = 0; i < 12; i++) { | ||
hexString += hexTable[this.id[i]]; | ||
} | ||
} else { | ||
for (var i = 0; i < this.id.length; i++) { | ||
hexString += hexTable[this.id.charCodeAt(i)]; | ||
} | ||
} | ||
return `new ObjectId(${hexString})` | ||
}; | ||
} | ||
monkeyPatchMongoObjectID(); | ||
exports.LoggerFactory = require("./src/logger-factory").LoggerFactory; | ||
@@ -4,0 +40,0 @@ exports.Logger = require("./src/logger").Logger; |
{ | ||
"name": "btrz-logger", | ||
"version": "5.0.5", | ||
"version": "5.0.6", | ||
"description": "A multi-transport logger", | ||
@@ -41,4 +41,5 @@ "main": "index.js", | ||
"sinon": "^19.0.2", | ||
"sinon-chai": "^3.7.0" | ||
"sinon-chai": "^3.7.0", | ||
"mongodb": "3.6.12" | ||
} | ||
} |
@@ -96,2 +96,4 @@ const util = require("node:util"); | ||
return {sanitizedValue: value}; | ||
} else if (value instanceof Date) { | ||
return {sanitizedValue: value}; | ||
} else if (value instanceof IncomingMessage) { | ||
@@ -98,0 +100,0 @@ return _sanitize({headers: value.headers, body: value.body}, currentDepth + 1, startTime); |
@@ -0,1 +1,2 @@ | ||
const {ObjectID} = require("bson"); | ||
const {expect} = require("chai"); | ||
@@ -94,2 +95,10 @@ const Chance = require("chance"); | ||
it("should correctly serialize a Mongo ObjectID", () => { | ||
logger.info("Some message", {_id: new ObjectID("6734ee02914f80d97e99586d")}); | ||
expectStringWasLogged("\n" + | ||
"{\n" + | ||
" _id: new ObjectId(6734ee02914f80d97e99586d)\n" + | ||
"}"); | ||
}); | ||
it("should correctly serialize 'null'", () => { | ||
@@ -96,0 +105,0 @@ logger.info("Some message", null); |
@@ -279,2 +279,8 @@ const {IncomingMessage} = require("node:http"); | ||
it("should return Date objects without any modifications", () => { | ||
const date = new Date(); | ||
const sanitizedValue = logCleaner.sanitize(date); | ||
expect(sanitizedValue).to.equal(date); | ||
}); | ||
it("should simplify a NodeJS 'IncomingMessage' and return only its 'headers' and 'body'", () => { | ||
@@ -281,0 +287,0 @@ const incomingMessage = new IncomingMessage(); |
71793
1730
8