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

feat: prevent objects with serializers from being trimmed #75

Merged
merged 3 commits into from
Apr 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
"test:ci": "skuba test --coverage"
},
"dependencies": {
"dtrim": "^1.9.0",
"dtrim": "^1.11.0",
"pino": "^8.0.0",
"pino-std-serializers": "^6.0.0"
"pino-std-serializers": "^6.2.0"
},
"devDependencies": {
"@types/split2": "3.2.1",
Expand Down
9 changes: 6 additions & 3 deletions src/formatters/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { trimmer } from 'dtrim';
import pino from 'pino';
import pino, { LoggerOptions } from 'pino';

export interface FormatterOptions {
/**
Expand All @@ -14,9 +14,12 @@ export interface FormatterOptions {
}

export const createFormatters = (
opts: FormatterOptions,
opts: FormatterOptions & Pick<LoggerOptions, 'serializers'>,
): pino.LoggerOptions['formatters'] => {
const trim = trimmer({ depth: opts.maxObjectDepth ?? 4 });
const trim = trimmer({
depth: opts.maxObjectDepth ?? 4,
retain: new Set(Object.keys(opts.serializers ?? {})),
});

return {
log: (input) => trim(input) as typeof input,
Expand Down
78 changes: 67 additions & 11 deletions src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,39 +147,39 @@ testLog(

testLog(
'should truncate objects deeper than the default depth of 4 levels',
{ req: { url: { c: { d: { e: { f: { g: {} } } } } } } },
{ req: { url: { c: { d: '[Object]' } } } },
{ foo: { url: { c: { d: { e: { f: { g: {} } } } } } } },
{ foo: { url: { c: { d: '[Object]' } } } },
);

testLog(
'should truncate objects deeper than the configured object depth',
{ req: { url: { c: { d: { e: { f: { g: {} } } } } } } },
{ req: { url: { c: { d: { e: '[Object]' } } } } },
{ foo: { url: { c: { d: { e: { f: { g: {} } } } } } } },
{ foo: { url: { c: { d: { e: '[Object]' } } } } },
undefined,
{ maxObjectDepth: 5 },
);

testLog(
'should truncate Buffers',
{
res: {
foo: {
statusCode: Buffer.from('aaa'),
},
},
{ res: { statusCode: 'Buffer(3)' } },
{ foo: { statusCode: 'Buffer(3)' } },
);

testLog(
'should truncate strings longer than 512 characters',
{ req: { url: 'a'.repeat(555) } },
{ req: { url: `${'a'.repeat(512)}...` } },
{ foo: { url: 'a'.repeat(555) } },
{ foo: { url: `${'a'.repeat(512)}...` } },
);

testLog(
'should truncate arrays containing more than 64 items',
{ err: { message: 'a'.repeat(64 + 10).split('') } },
{ foo: { message: 'a'.repeat(64 + 10).split('') } },
{
err: {
foo: {
message: 'Array(74)',
},
},
Expand Down Expand Up @@ -322,7 +322,10 @@ testLog(
request: {
_options: {
method: 'get',
headers: '[Object]',
headers: {
Accept: 'application/json, text/plain, */*',
authorization: '[Redacted]',
},
},
},
},
Expand Down Expand Up @@ -502,3 +505,56 @@ test('should log customized timestamp if timestamp logger option is supplied', a

expect(log.timestamp).toBe(mockTimestamp);
});

class Req {
get socket() {
return {
remoteAddress: 'localhost',
remotePort: '4000',
};
}
}
testLog(
'should not truncate objects with a serializer',
{
errWithCause: {
a: {
b: {},
},
},
err: {
a: {
b: {},
},
},
req: new Req(),
notSerialized: {
a: {
b: {},
},
},
},
{
errWithCause: {
a: {
b: {},
},
},
err: {
a: {
b: {},
},
},
req: {
remoteAddress: 'localhost',
remotePort: '4000',
},
notSerialized: {
a: '[Object]',
},
},
'info',
{
maxObjectDepth: 2,
},
);
3 changes: 1 addition & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@ export default (
sync: true,
}),
): Logger => {
const formatters = createFormatters(opts);

opts.redact = redact.addDefaultRedactPathStrings(opts.redact);
opts.serializers = {
...serializers,
...opts.serializers,
};
const formatters = createFormatters(opts);
opts.base = {
...base,
...opts.base,
Expand Down
11 changes: 11 additions & 0 deletions src/serializers/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,14 @@ describe('res', () => {
});
});
});

describe('serializers', () => {
test('it exports only err, errWithCause, req, res', () => {
expect(serializers).toStrictEqual({
err: expect.any(Function),
errWithCause: expect.any(Function),
req: expect.any(Function),
res: expect.any(Function),
});
});
});
5 changes: 3 additions & 2 deletions src/serializers/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import stdSerializers from 'pino-std-serializers';
import { err, errWithCause } from 'pino-std-serializers';

interface Socket {
remoteAddress?: string;
Expand Down Expand Up @@ -46,7 +46,8 @@ const res = (response: Response) =>
: response;

export default {
...stdSerializers,
err,
errWithCause,
res,
req,
};
Loading