Skip to content

Commit

Permalink
feat: improve serialization of system log input (#4414)
Browse files Browse the repository at this point in the history
Co-authored-by: kodiakhq[bot] <49736102+kodiakhq[bot]@users.noreply.github.com>
  • Loading branch information
eduardoboucas and kodiakhq[bot] committed Aug 8, 2022
1 parent 390c989 commit a873036
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
14 changes: 12 additions & 2 deletions packages/build/src/log/logger.js
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
@@ -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
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

0 comments on commit a873036

Please sign in to comment.