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

fix(logger): truncate log over 5000 characters long #16581

Merged
merged 4 commits into from
May 8, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
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
21 changes: 20 additions & 1 deletion packages/vite/src/node/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import readline from 'node:readline'
import colors from 'picocolors'
import type { RollupError } from 'rollup'
import type { ResolvedServerUrls } from './server'
import { splitRE } from './utils'

export type LogType = 'error' | 'warn' | 'info'
export type LogLevel = LogType | 'silent'
Expand Down Expand Up @@ -63,6 +64,8 @@ function getTimeFormatter() {
return timeFormatter
}

const MAX_LOG_CHAR = 5000

export function createLogger(
level: LogLevel = 'info',
options: LoggerOptions = {},
Expand All @@ -78,7 +81,23 @@ export function createLogger(
allowClearScreen && process.stdout.isTTY && !process.env.CI
const clear = canClearScreen ? clearScreen : () => {}

function format(type: LogType, msg: string, options: LogErrorOptions = {}) {
function preventOverflow(msg: string) {
if (msg.length > MAX_LOG_CHAR) {
const shorten = msg.slice(0, MAX_LOG_CHAR)
const shortenLines = shorten.match(splitRE)?.length || 0
const lines = msg.match(splitRE)?.length || 0 - shortenLines
bluwy marked this conversation as resolved.
Show resolved Hide resolved

return `${shorten}\n... and ${lines} lines more`
}
return msg
}

function format(
type: LogType,
rawMsg: string,
options: LogErrorOptions = {},
) {
const msg = preventOverflow(rawMsg)
if (options.timestamp) {
const tag =
type === 'info'
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/node/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ export function isFilePathESM(
}
}

const splitRE = /\r?\n/
export const splitRE = /\r?\n/g

const range: number = 2

Expand Down