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: improve serialization of system log input #4414

Merged
merged 2 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions packages/build/src/log/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,21 @@ export const logWarningSubHeader = function (logs, string, opts) {
const reduceLogLines = function (lines) {
return lines
.map((input) => {
if (input instanceof Error) {
return `${input.message} ${input.stack}`
}

if (typeof input === 'object') {
return JSON.stringify(input)
try {
return JSON.stringify(input)
} catch {
// Value could not be serialized to JSON, so we return the string
// representation.
return String(input)
}
}

return input.toString()
return String(input)
})
.join(' ')
}
Expand Down
20 changes: 20 additions & 0 deletions packages/build/tests/unit/logger/snapshots/tests.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Snapshot report for `packages/build/tests/unit/logger/tests.js`

The actual snapshot is saved in `tests.js.snap`.

Generated by [AVA](https://avajs.dev).

## System logger writes to file descriptor

> Snapshot 1

`Hello world {"object":true,"problem":false} Something went wrong Error: Something went wrong␊
STACK TRACE␊
`

## System logger does not write to file descriptor when `debug: true`

> Snapshot 1

`Hello world {"object":true,"problem":false} Something went wrong Error: Something went wrong␊
STACK TRACE`
Binary file not shown.
15 changes: 11 additions & 4 deletions packages/build/tests/unit/logger/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@ import test from 'ava'
import tmp from 'tmp-promise'

import { getSystemLogger } from '../../../src/log/logger.js'
import { normalizeOutput } from '../../helpers/normalize.js'

test('System logger writes to file descriptor', async (t) => {
const { fd, cleanup, path } = await tmp.file()
const mockProcess = {
stdout: [],
}
const systemLog = getSystemLogger(mockProcess, false, fd)
const error = new Error('Something went wrong')

systemLog('Hello world', { object: true, problem: false })
systemLog('Hello world', { object: true, problem: false }, error)

t.is(await fs.readFile(path, 'utf8'), 'Hello world {"object":true,"problem":false}\n')
const output = normalizeOutput(await fs.readFile(path, 'utf8'))

t.snapshot(output)
t.is(mockProcess.stdout.length, 0)

await cleanup()
Expand All @@ -26,11 +30,14 @@ test('System logger does not write to file descriptor when `debug: true`', async
stdout: [],
}
const systemLog = getSystemLogger(mockProcess, true, fd)
const error = new Error('Something went wrong')

systemLog('Hello world', { object: true, problem: false }, error)

systemLog('Hello world', { object: true, problem: false })
const output = normalizeOutput(mockProcess.stdout[0])

t.is(mockProcess.stdout.length, 1)
t.is(mockProcess.stdout[0], 'Hello world {"object":true,"problem":false}')
t.snapshot(output)
t.is(await fs.readFile(path, 'utf8'), '')

await cleanup()
Expand Down