Skip to content

Commit

Permalink
fix(): add enrich capability for correlationId as detailed in README …
Browse files Browse the repository at this point in the history
…but was actually missing
  • Loading branch information
mikaelvesavuori committed Feb 14, 2023
1 parent b9daffa commit 0af59a7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/entities/MikroLog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ export class MikroLog {
MikroLog.metadataConfig = Object.assign(MikroLog.metadataConfig, input.metadataConfig || {});
MikroLog.event = Object.assign(MikroLog.event, input.event || {});
MikroLog.context = Object.assign(MikroLog.context, input.context || {});
MikroLog.correlationId = input?.correlationId || this.correlationId || '';
}

/**
Expand Down
33 changes: 33 additions & 0 deletions tests/MikroLog.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,36 @@ test.serial('It should be able to merge enrichment even if input is essentially

t.deepEqual(cleanedResponse, cleanedExpected);
});

test.serial('It should be able to enrich with correlation ID', (t) => {
MikroLog.reset();
const message = 'Hello World';

const logger = MikroLog.start();
MikroLog.enrich({ correlationId: 'abc123' });
const response: any = logger.info(message);

const expected: any = {
correlationId: 'abc123',
error: false,
httpStatusCode: 200,
isColdStart: true,
level: 'INFO',
message: 'Hello World'
};

// Ensure exactness of message field
t.is(response['message'], message);

// Check presence of dynamic fields
t.true(response['id'] !== null);
t.true(response['timestamp'] !== null);
t.true(response['timestampEpoch'] !== null);
t.true(response['isColdStart'] !== null);

// Drop dynamic fields for test validation
const cleanedResponse = cleanObject(response);
const cleanedExpected = cleanObject(expected);

t.deepEqual(cleanedResponse, cleanedExpected);
});

0 comments on commit 0af59a7

Please sign in to comment.