0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-06 22:10:10 -05:00

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 <contact@florian-lefebvre.dev>

---------

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
This commit is contained in:
Emanuele Stoppa 2023-12-20 16:48:06 +00:00 committed by GitHub
parent c384f6924e
commit 19169db1f1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 19 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Improves logging of the generated pages during the build

View file

@ -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;
}
}

View file

@ -56,6 +56,7 @@ export interface LogMessage {
label: string | null;
level: LoggerLevel;
message: string;
newLine: boolean;
}
export const levels: Record<LoggerLevel, number> = {
@ -67,13 +68,20 @@ export const levels: Record<LoggerLevel, number> = {
};
/** 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);

View file

@ -7,15 +7,16 @@ type ConsoleStream = Writable & {
};
export const nodeLogDestination: LogWritable<LogMessage> = {
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;
},