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:
parent
c384f6924e
commit
19169db1f1
4 changed files with 34 additions and 19 deletions
5
.changeset/popular-meals-yell.md
Normal file
5
.changeset/popular-meals-yell.md
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
---
|
||||||
|
'astro': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Improves logging of the generated pages during the build
|
|
@ -304,13 +304,14 @@ async function generatePage(
|
||||||
for (let i = 0; i < paths.length; i++) {
|
for (let i = 0; i < paths.length; i++) {
|
||||||
const path = paths[i];
|
const path = paths[i];
|
||||||
pipeline.getEnvironment().logger.debug('build', `Generating: ${path}`);
|
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);
|
await generatePath(path, pipeline, generationOptions, route);
|
||||||
const timeEnd = performance.now();
|
const timeEnd = performance.now();
|
||||||
const timeChange = getTimeStat(prevTimeEnd, timeEnd);
|
const timeChange = getTimeStat(prevTimeEnd, timeEnd);
|
||||||
const timeIncrease = `(+${timeChange})`;
|
const timeIncrease = `(+${timeChange})`;
|
||||||
const filePath = getOutputFilename(pipeline.getConfig(), path, pageData.route.type);
|
logger.info('SKIP_FORMAT', ` ${dim(timeIncrease)}`);
|
||||||
const lineIcon = i === paths.length - 1 ? '└─' : '├─';
|
|
||||||
logger.info(null, ` ${blue(lineIcon)} ${dim(filePath)} ${dim(timeIncrease)}`);
|
|
||||||
prevTimeEnd = timeEnd;
|
prevTimeEnd = timeEnd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,7 @@ export interface LogMessage {
|
||||||
label: string | null;
|
label: string | null;
|
||||||
level: LoggerLevel;
|
level: LoggerLevel;
|
||||||
message: string;
|
message: string;
|
||||||
|
newLine: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const levels: Record<LoggerLevel, number> = {
|
export const levels: Record<LoggerLevel, number> = {
|
||||||
|
@ -67,13 +68,20 @@ export const levels: Record<LoggerLevel, number> = {
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Full logging API */
|
/** 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 logLevel = opts.level;
|
||||||
const dest = opts.dest;
|
const dest = opts.dest;
|
||||||
const event: LogMessage = {
|
const event: LogMessage = {
|
||||||
label,
|
label,
|
||||||
level,
|
level,
|
||||||
message,
|
message,
|
||||||
|
newLine,
|
||||||
};
|
};
|
||||||
|
|
||||||
// test if this level is enabled or not
|
// 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. */
|
/** Emit a user-facing message. Useful for UI and other console messages. */
|
||||||
export function info(opts: LogOptions, label: string | null, message: string) {
|
export function info(opts: LogOptions, label: string | null, message: string, newLine = true) {
|
||||||
return log(opts, 'info', label, message);
|
return log(opts, 'info', label, message, newLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */
|
/** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */
|
||||||
export function warn(opts: LogOptions, label: string | null, message: string) {
|
export function warn(opts: LogOptions, label: string | null, message: string, newLine = true) {
|
||||||
return log(opts, 'warn', label, message);
|
return log(opts, 'warn', label, message, newLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Emit a error message, Useful when Astro can't recover from some error. */
|
/** Emit a error message, Useful when Astro can't recover from some error. */
|
||||||
export function error(opts: LogOptions, label: string | null, message: string) {
|
export function error(opts: LogOptions, label: string | null, message: string, newLine = true) {
|
||||||
return log(opts, 'error', label, message);
|
return log(opts, 'error', label, message, newLine);
|
||||||
}
|
}
|
||||||
|
|
||||||
type LogFn = typeof info | typeof warn | typeof error;
|
type LogFn = typeof info | typeof warn | typeof error;
|
||||||
|
@ -191,14 +199,14 @@ export class Logger {
|
||||||
this.options = options;
|
this.options = options;
|
||||||
}
|
}
|
||||||
|
|
||||||
info(label: LoggerLabel | null, message: string) {
|
info(label: LoggerLabel | null, message: string, newLine = true) {
|
||||||
info(this.options, label, message);
|
info(this.options, label, message, newLine);
|
||||||
}
|
}
|
||||||
warn(label: LoggerLabel | null, message: string) {
|
warn(label: LoggerLabel | null, message: string, newLine = true) {
|
||||||
warn(this.options, label, message);
|
warn(this.options, label, message, newLine);
|
||||||
}
|
}
|
||||||
error(label: LoggerLabel | null, message: string) {
|
error(label: LoggerLabel | null, message: string, newLine = true) {
|
||||||
error(this.options, label, message);
|
error(this.options, label, message, newLine);
|
||||||
}
|
}
|
||||||
debug(label: LoggerLabel, ...messages: any[]) {
|
debug(label: LoggerLabel, ...messages: any[]) {
|
||||||
debug(label, ...messages);
|
debug(label, ...messages);
|
||||||
|
|
|
@ -7,15 +7,16 @@ type ConsoleStream = Writable & {
|
||||||
};
|
};
|
||||||
|
|
||||||
export const nodeLogDestination: LogWritable<LogMessage> = {
|
export const nodeLogDestination: LogWritable<LogMessage> = {
|
||||||
write(event: LogMessage) {
|
write(event: LogMessage, newLine = true) {
|
||||||
let dest: ConsoleStream = process.stderr;
|
let dest: ConsoleStream = process.stderr;
|
||||||
if (levels[event.level] < levels['error']) {
|
if (levels[event.level] < levels['error']) {
|
||||||
dest = process.stdout;
|
dest = process.stdout;
|
||||||
}
|
}
|
||||||
|
let trailingLine = event.newLine ? '\n' : '';
|
||||||
if (event.label === 'SKIP_FORMAT') {
|
if (event.label === 'SKIP_FORMAT') {
|
||||||
dest.write(event.message + '\n');
|
dest.write(event.message + trailingLine);
|
||||||
} else {
|
} else {
|
||||||
dest.write(getEventPrefix(event) + ' ' + event.message + '\n');
|
dest.write(getEventPrefix(event) + ' ' + event.message + trailingLine);
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue