logger4node
Advanced tools
Comparing version 1.0.15 to 1.0.16
{ | ||
"name": "logger4node", | ||
"version": "1.0.15", | ||
"version": "1.0.16", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -37,3 +37,4 @@ export declare const enum LogSeverity { | ||
private log; | ||
private static handleJSONSpecialCharacter; | ||
} | ||
export {}; |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
var desc = Object.getOwnPropertyDescriptor(m, k); | ||
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { | ||
desc = { enumerable: true, get: function() { return m[k]; } }; | ||
} | ||
Object.defineProperty(o, k2, desc); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
@@ -8,2 +31,3 @@ return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
const util_1 = __importDefault(require("util")); | ||
const fs = __importStar(require("node:fs")); | ||
exports.LogLevel = { | ||
@@ -79,8 +103,8 @@ verbose: 1, | ||
static errorStack(...args) { | ||
return args | ||
return this.handleJSONSpecialCharacter(args | ||
.filter((each) => (each instanceof Error)) | ||
.map((each) => each.stack).join('\n|\n'); | ||
.map((each) => each.stack).join('\\n|\\n')); | ||
} | ||
static jsonTransformArgs(...args) { | ||
return util_1.default.format(...args.map((each) => { | ||
const message = util_1.default.format(...args.map((each) => { | ||
if (['string', 'number', 'boolean', 'bigint', 'function', 'undefined'].includes(typeof each)) { | ||
@@ -90,4 +114,4 @@ return each; | ||
return JSON.stringify(each); | ||
})).replace(/([^\\])"/g, '$1\\"') | ||
.replace(/\n/g, '\\n'); | ||
})); | ||
return this.handleJSONSpecialCharacter(message); | ||
} | ||
@@ -151,2 +175,3 @@ verbose(formatter, ...args) { | ||
console.log(`{"className":"${this.name}","level":"${logSeverity}","message":"${Logger.jsonTransformArgs(formatter, ...args)}","stack":"${Logger.errorStack(formatter, ...args)}"}`); | ||
fs.writeFileSync('./test.txt', `{"className":"${this.name}","level":"${logSeverity}","message":"${Logger.jsonTransformArgs(formatter, ...args)}","stack":"${Logger.errorStack(formatter, ...args)}"}`, 'utf-8'); | ||
return; | ||
@@ -156,4 +181,12 @@ } | ||
} | ||
static handleJSONSpecialCharacter(message) { | ||
return message | ||
.replace(/\\/g, '\\\\') | ||
// .replace(/([^\\])"/g, '$1\\"') | ||
.replace(/"/g, '\\"') | ||
.replace(/\r\n/g, '\\r\\n') | ||
.replace(/\n/g, '\\n'); | ||
} | ||
} | ||
exports.Logger = Logger; | ||
//# sourceMappingURL=logger.js.map |
@@ -233,10 +233,25 @@ "use strict"; | ||
(0, chai_1.expect)(callbackSpy.getCall(0).args[0]).to | ||
.equal('{"className":"Logger:Instance","level":"error","message":"this is line1 \\" {\\"var\\":1,\\"var2\\":2}","stack":""}'); | ||
.equal('{"className":"Logger:Instance","level":"error","message":"this is line1 \\\\\\" {\\"var\\":1,\\"var2\\":2}","stack":""}'); | ||
(0, chai_1.expect)(JSON.parse(callbackSpy.getCall(0).args[0])).to.deep.equal({ | ||
className: 'Logger:Instance', | ||
level: 'error', | ||
message: 'this is line1 " {"var":1,"var2":2}', | ||
message: 'this is line1 \\" {"var":1,"var2":2}', | ||
stack: '', | ||
}); | ||
}); | ||
it('should log properly when message contains string as well as json"', () => { | ||
try { | ||
throw new class TestError extends Error { | ||
constructor() { | ||
super(); | ||
this.message = 'Received an error with invalid JSON from Parse: <html>\r\n<head><title>503 Service Temporarily Unavailable</title></head>\r\n<body>\r\n<center><h1>503 Service Temporarily Unavailable</h1></center>\r\n<hr><center>nginx</center>\r\n</body>\r\n</html>'; | ||
} | ||
}(); | ||
} | ||
catch (error) { | ||
loggerInstance.error(error); | ||
} | ||
(0, chai_1.expect)(callbackSpy.callCount).to.equal(1); | ||
(0, chai_1.expect)(typeof JSON.parse(callbackSpy.getCall(0).args[0])).to.equal('object'); | ||
}); | ||
afterEach(() => { | ||
@@ -243,0 +258,0 @@ callbackSpy.restore(); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65713
692