[READ-ONLY] Mirror of https://github.com/FoxxMD/logging. A typed, opinionated, batteries-included, Pino-based logging solution for backend TS/JS projects foxxmd.github.io/logging
child-logger logging logging-library nodejs pinojs typescript-library
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: Implement merging runtime log call labels with logger labels

FoxxMD (Mar 8, 2024, 10:22 AM EST) 4930462b e9b40db2

+24
+9
src/loggers.ts
··· 11 11 labels: loggerThis.labels ?? [] 12 12 } 13 13 }, 14 + mixinMergeStrategy(mergeObject: Record<any, any>, mixinObject: Record<any, any>) { 15 + if(mergeObject.labels === undefined || mixinObject.labels === undefined || mixinObject.labels.length === 0) { 16 + return Object.assign(mergeObject, mixinObject) 17 + } 18 + const runtimeLabels = Array.isArray(mergeObject.labels) ? mergeObject.labels : [mergeObject.labels]; 19 + const finalObj = Object.assign(mergeObject, mixinObject); 20 + finalObj.labels = [...(mixinObject.labels ?? []), ...runtimeLabels]; 21 + return finalObj; 22 + }, 14 23 level: defaultLevel, 15 24 customLevels: { 16 25 verbose: 25,
+15
tests/index.test.ts
··· 364 364 expect(raw.labels[4]).eq('Test5'); 365 365 }); 366 366 367 + it('merges labels from log call with logger labels', async function () { 368 + const [logger, testStream, rawStream] = testConsoleLogger(); 369 + const formattedBuff = pEvent(testStream, 'data'); 370 + const rawBuff = pEvent(rawStream, 'data'); 371 + 372 + logger.addLabel('Parent'); 373 + const child = childLogger(logger, ['Test1']); 374 + child.debug({labels: ['Runtime']},'log something'); 375 + await sleep(10); 376 + const formatted = (await formattedBuff).toString(); 377 + const raw = JSON.parse((await rawBuff).toString()) as LogData; 378 + expect(formatted).includes(' [Parent] [Test1]'); 379 + expect(formatted).includes(' [Runtime] '); 380 + }); 381 + 367 382 it('child labels do not affect parent logger labels', async function () { 368 383 const [logger, testStream, rawStream] = testConsoleLogger(); 369 384 const formattedBuff = pEvent(testStream, 'data');