Skip to content

Commit b0b839a

Browse files
authored
fix(logger): truncate log over 5000 characters long (#16581)
1 parent c3b3d3c commit b0b839a

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

packages/vite/src/node/logger.ts

+19-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import readline from 'node:readline'
44
import colors from 'picocolors'
55
import type { RollupError } from 'rollup'
66
import type { ResolvedServerUrls } from './server'
7+
import { splitRE } from './utils'
78

89
export type LogType = 'error' | 'warn' | 'info'
910
export type LogLevel = LogType | 'silent'
@@ -63,6 +64,8 @@ function getTimeFormatter() {
6364
return timeFormatter
6465
}
6566

67+
const MAX_LOG_CHAR = 5000
68+
6669
export function createLogger(
6770
level: LogLevel = 'info',
6871
options: LoggerOptions = {},
@@ -78,7 +81,22 @@ export function createLogger(
7881
allowClearScreen && process.stdout.isTTY && !process.env.CI
7982
const clear = canClearScreen ? clearScreen : () => {}
8083

81-
function format(type: LogType, msg: string, options: LogErrorOptions = {}) {
84+
function preventOverflow(msg: string) {
85+
if (msg.length > MAX_LOG_CHAR) {
86+
const shorten = msg.slice(0, MAX_LOG_CHAR)
87+
const lines = msg.slice(MAX_LOG_CHAR).match(splitRE)?.length || 0
88+
89+
return `${shorten}\n... and ${lines} lines more`
90+
}
91+
return msg
92+
}
93+
94+
function format(
95+
type: LogType,
96+
rawMsg: string,
97+
options: LogErrorOptions = {},
98+
) {
99+
const msg = preventOverflow(rawMsg)
82100
if (options.timestamp) {
83101
const tag =
84102
type === 'info'

packages/vite/src/node/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,7 @@ export function isFilePathESM(
434434
}
435435
}
436436

437-
const splitRE = /\r?\n/
437+
export const splitRE = /\r?\n/g
438438

439439
const range: number = 2
440440

0 commit comments

Comments
 (0)