Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(logger): enable logging of arbitrary objects #883

Merged
merged 5 commits into from May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions docs/core/logger.md
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,10 @@ If you already have an object containing a `message` key and an additional prope
logger.info('This is a log with an extra variable', { data: myImportantVariable });

// You can also pass multiple parameters
logger.info('This is a log with 2 extra variables',
logger.info('This is a log with 3 extra objects',
saragerion marked this conversation as resolved.
Show resolved Hide resolved
{ data: myImportantVariable },
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } }
{ correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } },
{ lambdaEvent: _event }
This conversation was marked as resolved.
Show resolved Hide resolved
);

// Simply pass a string for logging additional data
Expand Down
5 changes: 4 additions & 1 deletion packages/logger/examples/additional-keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Logger } from '../src';

const logger = new Logger();

const lambdaHandler: Handler = async () => {
const lambdaHandler: Handler = async (event) => {

// Pass a custom correlation ID
logger.warn('This is a WARN log', { correlationIds: { myCustomCorrelationId: 'foo-bar-baz' } });
Expand All @@ -23,6 +23,9 @@ const lambdaHandler: Handler = async () => {
// Pass a simple string as additional data
logger.info('This is an INFO log', 'Extra log data');

// Pass an arbitrary object as additional data
logger.debug('This is a DEBUG log', { lambdaEvent: event });

return {
foo: 'bar'
};
Expand Down
2 changes: 1 addition & 1 deletion packages/logger/src/types/Log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type LogLevelThresholds = {
[key in LogLevel]: number;
};

type LogAttributeValue = string | number | boolean | null | undefined | LogAttributeValue[] | { [key: string]: LogAttributeValue } | Error;
type LogAttributeValue = unknown;
This conversation was marked as resolved.
Show resolved Hide resolved
type LogAttributes = { [key: string]: LogAttributeValue };

type LogAttributesWithMessage = LogAttributes & {
Expand Down
21 changes: 21 additions & 0 deletions packages/logger/tests/unit/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,9 @@ describe('Class: Logger', () => {
});
const consoleSpy = jest.spyOn(logger['console'], methodOfLogger).mockImplementation();

interface ArbitraryObject { value: 'CUSTOM' | 'USER_DEFINED' }
const arbitraryObject: ArbitraryObject = { value: 'CUSTOM' };
This conversation was marked as resolved.
Show resolved Hide resolved

// Act
if (logger[methodOfLogger]) {
logger[methodOfLogger]('A log item without extra parameters');
Expand All @@ -289,6 +292,8 @@ describe('Class: Logger', () => {
logger[methodOfLogger]('A log item with a string as first parameter, and an error as second parameter', new Error('Something happened!'));
logger[methodOfLogger]('A log item with a string as first parameter, and an error with custom key as second parameter', { myCustomErrorKey: new Error('Something happened!') });
logger[methodOfLogger]('A log item with a string as first parameter, and a string as second parameter', 'parameter');
logger[methodOfLogger]('A log item with a string as first parameter, and an inline object as second parameter', { extra: { custom: mockDate } });
logger[methodOfLogger]('A log item with a string as first parameter, and an arbitrary object as second parameter', { extra: arbitraryObject });
}

// Assess
Expand Down Expand Up @@ -360,6 +365,22 @@ describe('Class: Logger', () => {
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
extra: 'parameter',
}));
expect(consoleSpy).toHaveBeenNthCalledWith(8, JSON.stringify({
level: method.toUpperCase(),
message: 'A log item with a string as first parameter, and an inline object as second parameter',
service: 'hello-world',
timestamp: '2016-06-20T12:08:10.000Z',
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
extra: { custom: '2016-06-20T12:08:10.000Z' }
}));
expect(consoleSpy).toHaveBeenNthCalledWith(9, JSON.stringify({
level: method.toUpperCase(),
message: 'A log item with a string as first parameter, and an arbitrary object as second parameter',
service: 'hello-world',
timestamp: '2016-06-20T12:08:10.000Z',
xray_trace_id: '1-5759e988-bd862e3fe1be46a994272793',
extra: { value: 'CUSTOM' }
}));
});
});

Expand Down