Skip to content

Commit

Permalink
add custom err attribute key support
Browse files Browse the repository at this point in the history
  • Loading branch information
dougludlow authored and iamolegga committed Sep 21, 2023
1 parent 6ca55ca commit 6c48c9f
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
70 changes: 70 additions & 0 deletions __tests__/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,76 @@ describe('error logging', () => {
});
});

describe('setting custom attribute keys', () => {
it('setting the `err` custom attribute key', async () => {
const ctx = Math.random().toString();
const message = 'custom `err` attribute key';

@Controller('/')
class TestController {
constructor(private readonly logger: PinoLogger) {
this.logger.setContext(ctx);
}

@Get()
get() {
this.logger.info(new Error(message), 'baz');
return {};
}
}

const logs = await new TestCase(new PlatformAdapter(), {
controllers: [TestController],
})
.forRoot({ pinoHttp: { customAttributeKeys: { err: 'error' } } })
.run();
expect(
logs.some(
(v) =>
v.req &&
v.context === ctx &&
!v.err &&
v.error &&
(v.error as { message: string }).message === message,
),
).toBeTruthy();
});

it('setting the `req` custom attribute key', async () => {
const ctx = Math.random().toString();
const message = 'custom `req` attribute key';

@Controller('/')
class TestController {
constructor(private readonly logger: PinoLogger) {
this.logger.setContext(ctx);
}

@Get()
get() {
this.logger.info(new Error(message), 'baz');
return {};
}
}

const logs = await new TestCase(new PlatformAdapter(), {
controllers: [TestController],
})
.forRoot({ pinoHttp: { customAttributeKeys: { req: 'request' } } })
.run();
expect(
logs.some(
(v) =>
!v.req &&
v.request &&
v.context === ctx &&
v.err &&
v.err.message === message,
),
).toBeTruthy();
});
});

describe('keeps stack of thrown error', () => {
it('built-in error handler logs with correct stack', async () => {
const msg = Math.random().toString();
Expand Down
11 changes: 10 additions & 1 deletion src/PinoLogger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,19 @@ export class PinoLogger implements PinoMethods {

protected context = '';
protected readonly contextName: string;
protected readonly errorKey: string = 'err';

constructor(
@Inject(PARAMS_PROVIDER_TOKEN) { pinoHttp, renameContext }: Params,
) {
if (
typeof pinoHttp === 'object' &&
'customAttributeKeys' in pinoHttp &&
typeof pinoHttp.customAttributeKeys !== 'undefined'
) {
this.errorKey = pinoHttp.customAttributeKeys.err ?? 'err';
}

if (!outOfContext) {
if (Array.isArray(pinoHttp)) {
outOfContext = pino(...pinoHttp);
Expand Down Expand Up @@ -135,7 +144,7 @@ export class PinoLogger implements PinoMethods {
args = [
Object.assign(
{ [this.contextName]: this.context },
{ err: firstArg },
{ [this.errorKey]: firstArg },
),
...args.slice(1),
];
Expand Down

0 comments on commit 6c48c9f

Please sign in to comment.