From 19169db1f1574d36cc284fd9a0319d9b1e92b49a Mon Sep 17 00:00:00 2001 From: Emanuele Stoppa Date: Wed, 20 Dec 2023 16:48:06 +0000 Subject: [PATCH] chore: smoother logging when building pages (#9487) * chore: smoother logging when building pages * chore: improve logging during the build * fix: put `newLine` top `LogMessage` * Update .changeset/popular-meals-yell.md Co-authored-by: Florian Lefebvre --------- Co-authored-by: Florian Lefebvre --- .changeset/popular-meals-yell.md | 5 ++++ packages/astro/src/core/build/generate.ts | 7 +++-- packages/astro/src/core/logger/core.ts | 34 ++++++++++++++--------- packages/astro/src/core/logger/node.ts | 7 +++-- 4 files changed, 34 insertions(+), 19 deletions(-) create mode 100644 .changeset/popular-meals-yell.md diff --git a/.changeset/popular-meals-yell.md b/.changeset/popular-meals-yell.md new file mode 100644 index 0000000000..772e9b82a5 --- /dev/null +++ b/.changeset/popular-meals-yell.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Improves logging of the generated pages during the build diff --git a/packages/astro/src/core/build/generate.ts b/packages/astro/src/core/build/generate.ts index 9dc80f8223..d26aad71e6 100644 --- a/packages/astro/src/core/build/generate.ts +++ b/packages/astro/src/core/build/generate.ts @@ -304,13 +304,14 @@ async function generatePage( for (let i = 0; i < paths.length; i++) { const path = paths[i]; pipeline.getEnvironment().logger.debug('build', `Generating: ${path}`); + const filePath = getOutputFilename(pipeline.getConfig(), path, pageData.route.type); + const lineIcon = i === paths.length - 1 ? '└─' : '├─'; + logger.info(null, ` ${blue(lineIcon)} ${dim(filePath)}`, false); await generatePath(path, pipeline, generationOptions, route); const timeEnd = performance.now(); const timeChange = getTimeStat(prevTimeEnd, timeEnd); const timeIncrease = `(+${timeChange})`; - const filePath = getOutputFilename(pipeline.getConfig(), path, pageData.route.type); - const lineIcon = i === paths.length - 1 ? '└─' : '├─'; - logger.info(null, ` ${blue(lineIcon)} ${dim(filePath)} ${dim(timeIncrease)}`); + logger.info('SKIP_FORMAT', ` ${dim(timeIncrease)}`); prevTimeEnd = timeEnd; } } diff --git a/packages/astro/src/core/logger/core.ts b/packages/astro/src/core/logger/core.ts index db571d8f14..f2230f7542 100644 --- a/packages/astro/src/core/logger/core.ts +++ b/packages/astro/src/core/logger/core.ts @@ -56,6 +56,7 @@ export interface LogMessage { label: string | null; level: LoggerLevel; message: string; + newLine: boolean; } export const levels: Record = { @@ -67,13 +68,20 @@ export const levels: Record = { }; /** Full logging API */ -export function log(opts: LogOptions, level: LoggerLevel, label: string | null, message: string) { +export function log( + opts: LogOptions, + level: LoggerLevel, + label: string | null, + message: string, + newLine = true +) { const logLevel = opts.level; const dest = opts.dest; const event: LogMessage = { label, level, message, + newLine, }; // test if this level is enabled or not @@ -89,18 +97,18 @@ export function isLogLevelEnabled(configuredLogLevel: LoggerLevel, level: Logger } /** Emit a user-facing message. Useful for UI and other console messages. */ -export function info(opts: LogOptions, label: string | null, message: string) { - return log(opts, 'info', label, message); +export function info(opts: LogOptions, label: string | null, message: string, newLine = true) { + return log(opts, 'info', label, message, newLine); } /** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */ -export function warn(opts: LogOptions, label: string | null, message: string) { - return log(opts, 'warn', label, message); +export function warn(opts: LogOptions, label: string | null, message: string, newLine = true) { + return log(opts, 'warn', label, message, newLine); } /** Emit a error message, Useful when Astro can't recover from some error. */ -export function error(opts: LogOptions, label: string | null, message: string) { - return log(opts, 'error', label, message); +export function error(opts: LogOptions, label: string | null, message: string, newLine = true) { + return log(opts, 'error', label, message, newLine); } type LogFn = typeof info | typeof warn | typeof error; @@ -191,14 +199,14 @@ export class Logger { this.options = options; } - info(label: LoggerLabel | null, message: string) { - info(this.options, label, message); + info(label: LoggerLabel | null, message: string, newLine = true) { + info(this.options, label, message, newLine); } - warn(label: LoggerLabel | null, message: string) { - warn(this.options, label, message); + warn(label: LoggerLabel | null, message: string, newLine = true) { + warn(this.options, label, message, newLine); } - error(label: LoggerLabel | null, message: string) { - error(this.options, label, message); + error(label: LoggerLabel | null, message: string, newLine = true) { + error(this.options, label, message, newLine); } debug(label: LoggerLabel, ...messages: any[]) { debug(label, ...messages); diff --git a/packages/astro/src/core/logger/node.ts b/packages/astro/src/core/logger/node.ts index 2c75968d2f..15f86a5b49 100644 --- a/packages/astro/src/core/logger/node.ts +++ b/packages/astro/src/core/logger/node.ts @@ -7,15 +7,16 @@ type ConsoleStream = Writable & { }; export const nodeLogDestination: LogWritable = { - write(event: LogMessage) { + write(event: LogMessage, newLine = true) { let dest: ConsoleStream = process.stderr; if (levels[event.level] < levels['error']) { dest = process.stdout; } + let trailingLine = event.newLine ? '\n' : ''; if (event.label === 'SKIP_FORMAT') { - dest.write(event.message + '\n'); + dest.write(event.message + trailingLine); } else { - dest.write(getEventPrefix(event) + ' ' + event.message + '\n'); + dest.write(getEventPrefix(event) + ' ' + event.message + trailingLine); } return true; },