Skip to content

Commit

Permalink
Add recursion handling
Browse files Browse the repository at this point in the history
  • Loading branch information
terehov committed Oct 18, 2022
1 parent 4c12332 commit 4502eff
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"ignorePatterns": ["dist/*", "*/dist/*", "/examples/browser/*", "tests/*", "*/tests/*", "build.js"],
"ignorePatterns": ["dist/*", "*/dist/*", "/examples/*", "tests/*", "*/tests/*", "build.js"],
"rules": {
"linebreak-style": ["error", "unix"],
"quotes": ["error", "double"],
Expand Down
3 changes: 3 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,6 @@ build.js
typedoc.js
.eslintrc.json
tsconfig*

# Ignore map files in npm
*.js.map
102 changes: 102 additions & 0 deletions examples/nodejs/old_example.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { Logger } from "../../src";

class MyClass {
private readonly _logger = new Logger({
prefix: [1, 2],
});

public constructor() {
this._logger.silly("I am a silly log.");
}

public myMethod(): void {
const jsonObj: any = {
name: "John Doe",
age: 30,
cars: {
car1: "Audi",
car2: "BMW",
car3: "Tesla",
},
obj: undefined,
};
jsonObj.obj = jsonObj;

this._logger.debug("I am a debug log.");
this._logger.info("I am an info log.");
this._logger.warn("I am a warn log with a json object:", jsonObj);
this._logger.error("I am an error log.");
try {
/* @ts-ignore */
null.foo();
} catch (err) {
this._logger.fatal(err);
}
}
}

const myClass: MyClass = new MyClass();
myClass.myMethod();

const log = new Logger({});
log.silly("I am a silly log.");
// log.trace("I am a trace log with a stack trace.");
log.debug("I am a debug log.");
log.info("I am an info log.");
log.warn("I am a warn log with a json object:", { foo: "bar" });
log.error("I am an error log.");
log.fatal(new Error("I am a pretty Error with a stacktrace."));

/*
* Circular example
* */
function Foo() {
/* @ts-ignore */
this.abc = "Hello";
/* @ts-ignore */
this.circular = this;
}
/* @ts-ignore */
const foo = new Foo();
const logMessage = log.debug(foo);
console.log("JSON.stringify circular log message", logMessage);

/* Child Logger Example */

const mainLogger = new Logger({
prefix: ["main"],
});
mainLogger.info("MainLogger initiated");

const childLogger1 = mainLogger.getSubLogger({
prefix: ["child1"],
});
childLogger1.info("ChildLogger1 initiated");

const childLogger1_1 = childLogger1.getSubLogger({
prefix: ["child1-1"],
});
childLogger1_1.info("ChildLogger1-1 initiated");
childLogger1_1.silly("ChildLogger1-1 silly 1");
childLogger1_1.silly("ChildLogger1-1 silly 2");
childLogger1_1.silly("ChildLogger1-1 silly 3");

childLogger1_1.silly("ChildLogger1-1 silly 4");
childLogger1_1.silly("ChildLogger1-1 silly 5");

childLogger1_1.silly("ChildLogger1-1 silly 6");
childLogger1_1.debug("ChildLogger1-1 debug finish");
const yetAnotherLogger = childLogger1_1.getSubLogger();
yetAnotherLogger.info("Yet another Logger with a name function");

/** Example: Hide Secrets */
let verySecretiveObject = {
password: "swordfish",
Authorization: 1234567,
stringPwd: "swordfish",
nested: {
regularString: "I am just a regular string.",
otherString: "pass1234.567",
},
};
verySecretiveObject.nested["circular"] = verySecretiveObject;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"build-server": "tsc -b tsconfig.src.json tsconfig.example.json",
"start": "node --enable-source-maps --experimental-specifier-resolution=node examples/dist/examples/nodejs/index.js",
"dev-ts": "nodemon --watch './**/*.ts' --exec 'node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm' examples/nodejs/index.ts",
"dev-ts-old-example": "nodemon --watch './**/*.ts' --exec 'node --enable-source-maps --experimental-specifier-resolution=node --no-warnings --loader ts-node/esm' examples/nodejs/old_example.ts",
"dev-js": "tsc -b tsconfig.example.json && node --enable-source-maps --experimental-specifier-resolution=node examples/dist/examples/nodejs/index.js",
"lint": "eslint --ext .js,.ts .",
"format": "prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
Expand Down
8 changes: 6 additions & 2 deletions src/BaseLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,14 @@ export class BaseLogger<LogObj> {
});
}

private _maskValuesOfKeysRecursive<T>(obj: T, keys: (number | string)[]): T {
private _maskValuesOfKeysRecursive<T>(obj: T, keys: (number | string)[], seen: unknown[] = []): T {
if (typeof obj !== "object" || obj == null) {
return obj;
}
if (seen.includes(obj)) {
return obj;
}
seen.push(obj);

Object.keys(obj).map((key) => {
const thisKey = this.settings.maskValuesOfKeysCaseInsensitive !== true ? key : key.toLowerCase();
Expand All @@ -180,7 +184,7 @@ export class BaseLogger<LogObj> {
}

if (typeof obj[key] === "object" && obj[key] !== null) {
this._maskValuesOfKeysRecursive(obj[key], keys);
this._maskValuesOfKeysRecursive(obj[key], keys, seen);
}
});

Expand Down
23 changes: 23 additions & 0 deletions tests/Nodejs/13_Recursive.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import "ts-jest";
import { Logger } from "../../src";

describe("Recursive", () => {
test("hidden", (): void => {
const mainLogger = new Logger({ type: "hidden" });

/*
* Circular example
* */
function Foo() {
/* @ts-ignore */
this.abc = "Hello";
/* @ts-ignore */
this.circular = this;
}
/* @ts-ignore */
const foo = new Foo();
const logMsg = mainLogger.info("circular", foo);
expect(logMsg["0"]).toBe("circular");
expect(logMsg["1"]["circular"]).toBe(logMsg["1"]);
});
});
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@
"removeComments": false,
"declaration": true
},
"exclude": ["node_modules", "**/node_modules", "**/dist", "**/test", "*.test.ts"]
"exclude": ["node_modules", "**/node_modules", "**/dist", "**/test", "*.test.ts", "examples"]
}

0 comments on commit 4502eff

Please sign in to comment.