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

Performance improvements #80

Merged
merged 18 commits into from
Jan 12, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor(Output): Localize output code
  • Loading branch information
nokome committed Jan 11, 2021
commit c1268cf6aa08c545cd1dd46f7493e4193fbf108a
2 changes: 1 addition & 1 deletion index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('TTY', () => {

// Fake that we are using a TTY device
process.stderr.isTTY = true
const consoleError = spyOnWriter()
const consoleError = jest.spyOn(console, 'error')

log.error('an error message')

Expand Down
23 changes: 9 additions & 14 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,26 +258,27 @@ export function defaultHandler(

// Generate a human readable or machine readable log entry based on
// environment
let entry = ''
if (
typeof process !== 'undefined' &&
process.stderr !== undefined &&
process.stderr.isTTY !== true
) {
const { fastTime = false } = options
entry = `{"time":${
let json = `{"time":${
fastTime ? Date.now() : `"${new Date().toISOString()}"`
},"tag":"${tag}","level":${level},"message":"${message}"`
if (stack !== undefined) {
entry += `,"stack":"${escape(stack)}"`
json += `,"stack":"${escape(stack)}"`
}
entry += '}\n'
json += '}\n'
process.stderr.write(json)
} else {
const index = level < 0 ? 0 : level > 3 ? 3 : level
const label = LogLevel[index].toUpperCase().padEnd(5, ' ')
let line
/* istanbul ignore next */
if (typeof window !== 'undefined') {
entry = `${label} ${tag} ${message}`
line = `${label} ${tag} ${message}`
} else {
const emoji = [
'🚨', // error
Expand All @@ -293,19 +294,13 @@ export function defaultHandler(
][index]
const cyan = '\u001b[36m'
const reset = '\u001b[0m'
entry = `${emoji} ${colour}${label}${reset} ${cyan}${tag}${reset} ${message}`
line = `${emoji} ${colour}${label}${reset} ${cyan}${tag}${reset} ${message}`
}

const { showStack = false } = options
if (showStack && stack !== undefined) entry += '\n ' + stack
}
if (showStack && stack !== undefined) line += '\n ' + stack

// On Node.js, writing directly to stderr provides a performance boost
// of ~ 150% (based on our benchmarking)
if (typeof process !== 'undefined') {
process.stderr.write(entry)
} else {
console.error(entry)
console.error(line)
}

const { exitOnError = true } = options
Expand Down