Skip to content

Commit b7d14ee

Browse files
committed
switch to console warning, when both persistentLogAttributes and persistentKeys are used in constructor, update tests
1 parent 2e8c71f commit b7d14ee

File tree

2 files changed

+21
-9
lines changed

2 files changed

+21
-9
lines changed

packages/logger/src/Logger.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,8 +1067,8 @@ class Logger extends Utility implements LoggerInterface {
10671067
} = options;
10681068

10691069
if (persistentLogAttributes && persistentKeys) {
1070-
throw new Error(
1071-
`Both persistentLogAttributes and persistentKeys options are provided. Use only persistentKeys as persistentLogAttributes is deprecated.`
1070+
this.warn(
1071+
'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases'
10721072
);
10731073
}
10741074

packages/logger/tests/unit/Logger.test.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -496,8 +496,12 @@ describe('Class: Logger', () => {
496496
);
497497
});
498498

499-
test('it throws when both persistentKeys and persistentLogAttributes are used in the constructor', () => {
499+
test('it emits a warning when both persistentKeys and persistentLogAttributes are used in the constructor', () => {
500500
// Prepare
501+
// Since the buffer is private and we are bypassing the public warn method, we need to spy on the console.warn
502+
process.env.POWERTOOLS_DEV = 'true';
503+
const warningSpy = jest.spyOn(console, 'warn').mockImplementation();
504+
501505
type TestConstructorOptions = {
502506
persistentLogAttributes?: Record<string, string>;
503507
persistentKeys?: Record<string, string>;
@@ -512,10 +516,18 @@ describe('Class: Logger', () => {
512516
},
513517
};
514518

515-
// Act & Assess
516-
expect(() => new Logger(loggerOptions as ConstructorOptions)).toThrow(
517-
'Both persistentLogAttributes and persistentKeys options are provided. Use only persistentKeys as persistentLogAttributes is deprecated.'
519+
// Act
520+
new Logger(loggerOptions as ConstructorOptions);
521+
522+
// Assess
523+
expect(warningSpy).toHaveBeenCalledTimes(1);
524+
expect(warningSpy).toHaveBeenCalledWith(
525+
expect.stringContaining(
526+
'Both persistentLogAttributes and persistentKeys options were provided. Using persistentKeys as persistentLogAttributes is deprecated and will be removed in future releases'
527+
)
518528
);
529+
// Cleanup
530+
warningSpy.mockRestore();
519531
});
520532

521533
test('when a custom environment is passed, returns a Logger instance with the correct properties', () => {
@@ -2764,7 +2776,7 @@ describe('Class: Logger', () => {
27642776
);
27652777
});
27662778

2767-
it('uses log level set by ALC & emits a warning when setting a higher log level than ALC', () => {
2779+
test('it uses log level set by ALC & emits a warning when setting a higher log level than ALC', () => {
27682780
// Prepare
27692781
process.env.AWS_LAMBDA_LOG_LEVEL = 'ERROR';
27702782
process.env.LOG_LEVEL = undefined;
@@ -2784,7 +2796,7 @@ describe('Class: Logger', () => {
27842796
);
27852797
});
27862798

2787-
it('uses log level set by ALC & emits a warning when initializing with a higher log level than ALC', () => {
2799+
test('it uses log level set by ALC & emits a warning when initializing with a higher log level than ALC', () => {
27882800
// Prepare
27892801
process.env.AWS_LAMBDA_LOG_LEVEL = 'INFO';
27902802
process.env.LOG_LEVEL = undefined;
@@ -3082,7 +3094,7 @@ describe('Class: Logger', () => {
30823094
);
30833095
});
30843096

3085-
it('logs a DEBUG log when the sample rate sets the level to DEBUG', () => {
3097+
test('logs a DEBUG log when the sample rate sets the level to DEBUG', () => {
30863098
// Prepare
30873099
// Since the buffer is private and we are bypassing the public warn method, we need to spy on the console.warn
30883100
process.env.POWERTOOLS_DEV = 'true';

0 commit comments

Comments
 (0)