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: add logger.hasErrorLogged(error) method #3957

Merged
merged 9 commits into from
Aug 4, 2021
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
12 changes: 6 additions & 6 deletions packages/vite/src/node/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -409,16 +409,16 @@ async function doBuild(
}

const outputBuildError = (e: RollupError) => {
config.logger.error(
chalk.red(`${e.plugin ? `[${e.plugin}] ` : ''}${e.message}`)
)
let msg = chalk.red((e.plugin ? `[${e.plugin}] ` : '') + e.message)
if (e.id) {
const loc = e.loc ? `:${e.loc.line}:${e.loc.column}` : ''
config.logger.error(`file: ${chalk.cyan(`${e.id}${loc}`)}`)
msg += `\nfile: ${chalk.cyan(
e.id + (e.loc ? `:${e.loc.line}:${e.loc.column}` : '')
)}`
}
if (e.frame) {
config.logger.error(chalk.yellow(e.frame))
msg += `\n` + chalk.yellow(e.frame)
}
config.logger.error(msg, { error: e })
}

try {
Expand Down
12 changes: 8 additions & 4 deletions packages/vite/src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ cli
await server.listen()
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when starting dev server:\n${e.stack}`)
chalk.red(`error when starting dev server:\n${e.stack}`),
{ error: e }
)
process.exit(1)
}
Expand Down Expand Up @@ -148,7 +149,8 @@ cli
})
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error during build:\n${e.stack}`)
chalk.red(`error during build:\n${e.stack}`),
{ error: e }
)
process.exit(1)
}
Expand Down Expand Up @@ -178,7 +180,8 @@ cli
await optimizeDeps(config, options.force, true)
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when optimizing deps:\n${e.stack}`)
chalk.red(`error when optimizing deps:\n${e.stack}`),
{ error: e }
)
process.exit(1)
}
Expand Down Expand Up @@ -222,7 +225,8 @@ cli
await preview(config, cleanOptions(options))
} catch (e) {
createLogger(options.logLevel).error(
chalk.red(`error when starting preview server:\n${e.stack}`)
chalk.red(`error when starting preview server:\n${e.stack}`),
{ error: e }
)
process.exit(1)
}
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,8 @@ export async function loadConfigFromFile(
}
} catch (e) {
createLogger(logLevel).error(
chalk.red(`failed to load config from ${resolvedPath}`)
chalk.red(`failed to load config from ${resolvedPath}`),
{ error: e }
)
throw e
}
Expand Down
1 change: 1 addition & 0 deletions packages/vite/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type { Plugin } from './plugin'
export type {
Logger,
LogOptions,
LogErrorOptions,
LogLevel,
LogType,
LoggerOptions
Expand Down
17 changes: 15 additions & 2 deletions packages/vite/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import chalk from 'chalk'
import readline from 'readline'
import os from 'os'
import { RollupError } from 'rollup'
import { Hostname } from './utils'

export type LogType = 'error' | 'warn' | 'info'
Expand All @@ -11,8 +12,9 @@ export interface Logger {
info(msg: string, options?: LogOptions): void
warn(msg: string, options?: LogOptions): void
warnOnce(msg: string, options?: LogOptions): void
error(msg: string, options?: LogOptions): void
error(msg: string, options?: LogErrorOptions): void
clearScreen(type: LogType): void
hasErrorLogged(error: Error | RollupError): boolean
hasWarned: boolean
}

Expand All @@ -21,6 +23,10 @@ export interface LogOptions {
timestamp?: boolean
}

export interface LogErrorOptions extends LogOptions {
error?: Error | RollupError | null
}

export const LogLevels: Record<LogLevel, number> = {
silent: 0,
error: 1,
Expand Down Expand Up @@ -54,14 +60,15 @@ export function createLogger(
return options.customLogger
}

const loggedErrors = new WeakSet<Error | RollupError>()
const { prefix = '[vite]', allowClearScreen = true } = options
const thresh = LogLevels[level]
const clear =
allowClearScreen && process.stdout.isTTY && !process.env.CI
? clearScreen
: () => {}

function output(type: LogType, msg: string, options: LogOptions = {}) {
function output(type: LogType, msg: string, options: LogErrorOptions = {}) {
if (thresh >= LogLevels[type]) {
const method = type === 'info' ? 'log' : type
const format = () => {
Expand All @@ -77,6 +84,9 @@ export function createLogger(
return msg
}
}
if (options.error) {
loggedErrors.add(options.error)
}
if (type === lastType && msg === lastMsg) {
sameCount++
clear()
Expand Down Expand Up @@ -118,6 +128,9 @@ export function createLogger(
if (thresh >= LogLevels[type]) {
clear()
}
},
hasErrorLogged(error) {
return loggedErrors.has(error)
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/optimizer/registerMissing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export function createMissingImporterRegisterFn(
} catch (e) {
logger.error(
chalk.red(`error while updating dependencies:\n${e.stack}`),
{ timestamp: true }
{ timestamp: true, error: e }
)
} finally {
server._isRunningOptimizer = false
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/middlewares/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ export function errorMiddleware(

server.config.logger.error(msg, {
clear: true,
timestamp: true
timestamp: true,
error: err
})

server.ws.send({
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/middlewares/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ export function proxyMiddleware(

proxy.on('error', (err) => {
config.logger.error(`${chalk.red(`http proxy error:`)}\n${err.stack}`, {
timestamp: true
timestamp: true,
error: err
})
})

Expand Down
9 changes: 5 additions & 4 deletions packages/vite/src/node/server/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ function executeNodeScript(scriptPath: string, url: string, logger: Logger) {
if (code !== 0) {
logger.error(
chalk.red(
'\nThe script specified as BROWSER environment variable failed.\n'
)
`\nThe script specified as BROWSER environment variable failed.\n\n${chalk.cyan(
scriptPath
)} exited with code ${code}.`
),
{ error: null }
)
logger.error(chalk.cyan(scriptPath) + ' exited with code ' + code + '.')
return
}
})
return true
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/server/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ export function createWebSocketServer(
wss.on('error', (e: Error & { code: string }) => {
if (e.code !== 'EADDRINUSE') {
config.logger.error(
chalk.red(`WebSocket server error:\n${e.stack || e.message}`)
chalk.red(`WebSocket server error:\n${e.stack || e.message}`),
{ error: e }
)
}
})
Expand Down
3 changes: 2 additions & 1 deletion packages/vite/src/node/ssr/ssrModuleLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ async function instantiateModule(
`Error when evaluating SSR module ${url}:\n${stacktrace}`,
{
timestamp: true,
clear: server.config.clearScreen
clear: server.config.clearScreen,
error: e
}
)
throw e
Expand Down