Skip to content

Commit

Permalink
fix: allow for the value of a masked property to be undefined or null (
Browse files Browse the repository at this point in the history
…#1760)

fix: allow for the value of a masked property to be undefined or null
  • Loading branch information
jmcdo29 authored Oct 1, 2023
2 parents eba51c8 + 6cd729d commit 11177bb
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-icons-help.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ogma/logger': patch
---

Allow for the value of a masked property to be null or undefined
12 changes: 9 additions & 3 deletions packages/logger/src/logger/ogma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class Ogma {
const seen = new WeakSet();
return (key: string, value: any): string | number | boolean => {
if (this.cachedMasks?.has(key)) {
return '*'.repeat(value.toString().length);
return '*'.repeat(value?.toString().length ?? 9);
}

if (value === null) return value;
Expand Down Expand Up @@ -326,10 +326,16 @@ export class Ogma {
return '';
}

if (this.cachedContextFormatted.has(value) && this.cachedContextFormatted.get(value).has(color))
if (
this.cachedContextFormatted.has(value) &&
this.cachedContextFormatted.get(value).has(color)
) {
return this.cachedContextFormatted.get(value).get(color);
}

if (!this.cachedContextFormatted.has(value)) this.cachedContextFormatted.set(value, new Map());
if (!this.cachedContextFormatted.has(value)) {
this.cachedContextFormatted.set(value, new Map());
}

const cachedValue = colorize(
this.wrapInBrackets(value),
Expand Down
22 changes: 21 additions & 1 deletion packages/logger/test/ogma.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ OgmaSuite(
);
for (const json of [true, false]) {
OgmaSuite(
`it should make the value for the password. JSON: ${json}`,
`it should mask the value for the password. JSON: ${json}`,
({ writeSpy, ogmaFactory, getFirstCallString }) => {
const ogma = ogmaFactory({ json, masks: ['password'] });
ogma.log({ username: 'something', password: 'mask this' });
Expand All @@ -281,6 +281,26 @@ for (const json of [true, false]) {
match(loggedVal, /"password":\s?"\*{9}"/);
},
);
OgmaSuite(
`it should allow for an undefined value for the masked field. JSON: ${json}`,
({ writeSpy, ogmaFactory, getFirstCallString }) => {
const ogma = ogmaFactory({ json, masks: ['password'] });
ogma.log({ username: 'something', password: undefined });
const loggedVal = getFirstCallString(writeSpy);
match(loggedVal, /"username":\s?"something",/);
match(loggedVal, /"password":\s?"\*{9}"/);
},
);
OgmaSuite(
`it should allow for an null value for the masked field. JSON: ${json}`,
({ writeSpy, ogmaFactory, getFirstCallString }) => {
const ogma = ogmaFactory({ json, masks: ['password'] });
ogma.log({ username: 'something', password: null });
const loggedVal = getFirstCallString(writeSpy);
match(loggedVal, /"username":\s?"something",/);
match(loggedVal, /"password":\s?"\*{9}"/);
},
);
}
OgmaSuite(
'it should print each array value if the option "each" is true',
Expand Down

0 comments on commit 11177bb

Please sign in to comment.