Skip to content

Commit

Permalink
feat: fix issue where metadata was removed globally and not just on t…
Browse files Browse the repository at this point in the history
…he log
  • Loading branch information
mikaelvesavuori committed Jan 11, 2025
1 parent 6238faa commit d8a0ecc
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 18 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "mikrolog",
"description": "The JSON logger you always wanted for Lambda.",
"version": "2.2.2",
"version": "2.2.3",
"author": "Mikael Vesavuori",
"license": "MIT",
"type": "module",
Expand Down
4 changes: 3 additions & 1 deletion src/domain/entities/MikroLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,9 @@ export class MikroLog {
* @description Create the log envelope.
*/
private createLog(log: LogInput): LogOutput {
const staticMetadata: any = MikroLog.metadataConfig;
const staticMetadata: any = JSON.parse(
JSON.stringify(MikroLog.metadataConfig)
);
const redactedKeys = staticMetadata.redactedKeys
? staticMetadata.redactedKeys
: undefined;
Expand Down
48 changes: 34 additions & 14 deletions tests/MikroLog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ describe('Logs output', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should return (print out) a structured log when given a string message', () => {
Expand All @@ -122,7 +122,7 @@ describe('Logs output', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should return (print out) a structured informational log when given a string message', () => {
Expand All @@ -147,7 +147,7 @@ describe('Logs output', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should return (print out) a structured debug log when given a string message', () => {
Expand All @@ -173,7 +173,7 @@ describe('Logs output', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should return (print out) a structured warning log when given a string message', () => {
Expand All @@ -199,7 +199,7 @@ describe('Logs output', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should return (print out) a structured error log when given a string message', () => {
Expand Down Expand Up @@ -227,7 +227,7 @@ describe('Logs output', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should retain falsy but defined values in logs', () => {
Expand Down Expand Up @@ -265,7 +265,7 @@ describe('Logs output', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});
});

Expand Down Expand Up @@ -466,7 +466,7 @@ describe('Redact data', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should redact nested keys when given a "redactedKeys" list', () => {
Expand Down Expand Up @@ -497,6 +497,7 @@ describe('Redact data', () => {
id: '1256767f-c875-4d82-813d-bc260bd0ba07',
timestamp: '2022-07-25T08:52:21.121Z',
timestampEpoch: '1656438566041',
team: 'MyDemoTeam',
jurisdiction: 'EU'
};

Expand All @@ -513,7 +514,7 @@ describe('Redact data', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});
});

Expand Down Expand Up @@ -559,7 +560,26 @@ describe('Mask data', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should persist masking settings values across multiple logs', () => {
MikroLog.reset();
const expected = 'MASKED';
const message = 'Hello World';

const _metadataConfig = JSON.parse(JSON.stringify(metadataConfig));
_metadataConfig.auth = { token: 'abc123' };
_metadataConfig.maskedValues = ['auth.token'];

const logger = MikroLog.start({ metadataConfig: _metadataConfig });
const response = logger.error(message);
const response2 = logger.error(message);

// @ts-ignore
expect(response.auth.token).toBe(expected);
// @ts-ignore
expect(response2.auth.token).toBe(expected);
});

test('It should mask nested values when given a "maskedValues" list', () => {
Expand Down Expand Up @@ -605,7 +625,7 @@ describe('Mask data', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});
});

Expand Down Expand Up @@ -649,7 +669,7 @@ describe('Metadata', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});
});

Expand Down Expand Up @@ -683,7 +703,7 @@ describe('Enrichment', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should be able to enrich with correlation ID', () => {
Expand Down Expand Up @@ -716,7 +736,7 @@ describe('Enrichment', () => {
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

expect(cleanedResponse).toMatchObject(cleanedExpected);
expect(cleanedResponse).toEqual(cleanedExpected);
});

test('It should enrich a single-level log with a one-time root item and ensure it is not present in later calls', () => {
Expand Down

0 comments on commit d8a0ecc

Please sign in to comment.