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

Remove overly verbose data from custom formatter #8353

Merged
merged 3 commits into from
May 24, 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
89 changes: 55 additions & 34 deletions packages/api-server/src/__tests__/logFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,47 +122,68 @@ describe('LogFormatter', () => {
logFormatter({
level: 10,
custom: {
msg: 'I should see this custom message in the log',
string: 'I should see this custom message in the log',
},
})
).toMatch('I should see this custom message in the log')
})
})

it('Should include the custom log attribute info with a number attribute', () => {
expect(
logFormatter({
level: 10,
custom: {
msg: 'I should see this custom message and number in the log',
number: 100,
},
})
).toMatch('100')
})
it('Should include the custom log attribute info with a number attribute', () => {
expect(
logFormatter({
level: 10,
custom: {
string: 'I should see this custom message and number in the log',
number: 100,
},
})
).toMatch('100')
})

it('Should include the custom log attribute info with a nested object attribute', () => {
expect(
logFormatter({
level: 10,
custom: {
msg: 'I should see this custom object in the log',
obj: { foo: 'bar' },
},
})
).toMatch('"foo": "bar"')
})
it('Should include the custom log attribute info with a nested object attribute', () => {
expect(
logFormatter({
level: 10,
custom: {
string: 'I should see this custom object in the log',
obj: { foo: 'bar' },
},
})
).toMatch('"foo": "bar"')
})

it('Should include the custom log attribute info with a nested object attribute', () => {
expect(
logFormatter({
level: 10,
custom: {
msg: 'I should see this custom object in the log',
obj: { foo: 'bar' },
},
})
).toMatch('"foo": "bar"')
it('Should include the custom log attribute info with a nested object attribute', () => {
expect(
logFormatter({
level: 10,
custom: {
string: 'I should see this custom object in the log',
obj: { foo: 'bar' },
},
})
).toMatch('"foo": "bar"')
})

it('Should filter out overly verbose custom log attributes', () => {
expect(
logFormatter({
level: 10,
custom: {
time: 1,
pid: 1,
hostname: 'should not appear',
reqId: 'should not appear',
req: {
method: 'should not appear',
url: 'should not appear',
hostname: 'should not appear',
remoteAddress: 'should not appear',
remotePort: 1,
},
},
})
).not.toMatch('should not appear')
})
})

it('Should format error stack traces', () => {
Expand Down
19 changes: 19 additions & 0 deletions packages/api-server/src/logFormatter/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ export const emojiLog: Record<string, string> = {
trace: '🧵',
}

export const ignoredCustomData: Array<string> = [
'time',
'pid',
'hostname',
'msg',
'res',
'req',
'reqId',
'responseTime',
]

export const isObject = (object?: Record<string, unknown>) => {
return object && Object.prototype.toString.apply(object) === '[object Object]'
}
Expand All @@ -36,6 +47,14 @@ export const formatBundleSize = (bundle: string) => {
}

export const formatCustom = (query?: Record<string, unknown>) => {
if (!query) {
return
}

ignoredCustomData.forEach((key) => {
delete query[key]
})

if (!isEmptyObject(query)) {
return chalk.white(
NEWLINE + '🗒 Custom' + NEWLINE + JSON.stringify(query, null, 2)
Expand Down