Skip to content

Commit

Permalink
Remove overly verbose data from custom formatter (#8353)
Browse files Browse the repository at this point in the history
* refactor: remove overly verbose data from custom formatter

* test: add tests for LogFormatter custom filter

---------

Co-authored-by: David Thyresson <dthyresson@gmail.com>
  • Loading branch information
cravend and dthyresson authored May 24, 2023
1 parent c792d73 commit b627156
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 34 deletions.
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

0 comments on commit b627156

Please sign in to comment.