Skip to content

Commit fec3943

Browse files
committed
fix(logger): not passing persistentKeys to child
1 parent ccf0035 commit fec3943

File tree

3 files changed

+94
-4
lines changed

3 files changed

+94
-4
lines changed

packages/logger/src/Logger.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,7 @@ class Logger extends Utility implements LoggerInterface {
335335
logFormatter: this.getLogFormatter(),
336336
customConfigService: this.getCustomConfigService(),
337337
environment: this.powertoolsLogData.environment,
338-
persistentLogAttributes:
339-
this.#attributesStore.getPersistentAttributes(),
338+
persistentKeys: this.#attributesStore.getPersistentAttributes(),
340339
jsonReplacerFn: this.#jsonReplacerFn,
341340
correlationIdSearchFn: this.#correlationIdSearchFn,
342341
...(this.#bufferConfig.enabled && {

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

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,88 @@ describe('Log levels', () => {
110110
);
111111
});
112112

113+
it('overrides the service name when creating a child logger', () => {
114+
// Prepare
115+
vi.stubEnv('POWERTOOLS_SERVICE_NAME', 'hello-world');
116+
const logger = new Logger();
117+
const childLogger = logger.createChild({ serviceName: 'child-service' });
118+
119+
// Act
120+
childLogger.info('Hello, world!');
121+
122+
// Assess
123+
expect(console.info).toHaveBeenCalledTimes(1);
124+
expect(console.info).toHaveLoggedNth(
125+
1,
126+
expect.objectContaining({ service: 'child-service' })
127+
);
128+
});
129+
130+
it('maintains persistentKeys when creating a child logger', () => {
131+
// Prepare
132+
const mockDate = new Date(1466424490000);
133+
vi.useFakeTimers().setSystemTime(mockDate);
134+
const logger = new Logger({
135+
persistentKeys: {
136+
foo: 'hello',
137+
overridable: 1,
138+
},
139+
});
140+
141+
logger.appendKeys({
142+
resettableKey: 'some-id',
143+
});
144+
145+
logger.appendPersistentKeys({
146+
dynamic: 'stays',
147+
});
148+
149+
const childLogger = logger.createChild({
150+
serviceName: 'child-service',
151+
persistentKeys: {
152+
bar: 'world',
153+
overridable: 2,
154+
},
155+
});
156+
157+
// Act
158+
childLogger.info('Hello, world!');
159+
childLogger.resetKeys();
160+
childLogger.info('Hello again!');
161+
162+
// Assess
163+
expect(console.info).toHaveBeenCalledTimes(2);
164+
expect(console.info).toHaveLoggedNth(
165+
1,
166+
expect.objectContaining({
167+
service: 'child-service',
168+
foo: 'hello',
169+
bar: 'world',
170+
dynamic: 'stays',
171+
message: 'Hello, world!',
172+
overridable: 2,
173+
resettableKey: 'some-id',
174+
})
175+
);
176+
177+
expect(console.info).toHaveLoggedNth(
178+
2,
179+
// using direct match here to ensure removal of resetKeys
180+
{
181+
service: 'child-service',
182+
foo: 'hello',
183+
bar: 'world',
184+
dynamic: 'stays',
185+
message: 'Hello again!',
186+
overridable: 2,
187+
level: 'INFO',
188+
sampling_rate: 0,
189+
timestamp: '2016-06-20T12:08:10.000Z',
190+
xray_trace_id: '1-abcdef12-3456abcdef123456abcdef12',
191+
}
192+
);
193+
});
194+
113195
it('`logRecordOrder` should be passed down to child logger', () => {
114196
// Prepare
115197
const expectedKeys = [

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,16 @@ describe('Working with keys', () => {
519519
});
520520

521521
// Act
522-
const childLogger = logger.createChild();
522+
const childLogger = logger.createChild({
523+
persistentKeys: {
524+
bar: 'foo',
525+
},
526+
});
523527

524528
// Assess
525529
expect(childLogger.getPersistentLogAttributes()).toEqual({
526530
foo: 'bar',
531+
bar: 'foo',
527532
});
528533
});
529534

@@ -671,7 +676,11 @@ describe('Working with keys', () => {
671676

672677
it('should pass persistentKeys to child with no warning', () => {
673678
// Prepare
674-
const logger = new Logger();
679+
const logger = new Logger({
680+
persistentKeys: {
681+
foo: 'bar',
682+
},
683+
});
675684
logger.createChild({ persistentKeys: { abc: 'xyz' } });
676685

677686
// Assess

0 commit comments

Comments
 (0)