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

chore: smoother logging when building pages #9487

Merged
merged 4 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/popular-meals-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'astro': patch
---

Improved logging of the generated pages during the build
ematipico marked this conversation as resolved.
Show resolved Hide resolved
7 changes: 4 additions & 3 deletions packages/astro/src/core/build/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
36 changes: 21 additions & 15 deletions packages/astro/src/core/logger/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { blue, bold, dim, red, yellow } from 'kleur/colors';
import stringWidth from 'string-width';

export interface LogWritable<T> {
write: (chunk: T) => boolean;
write: (chunk: T, newLine: boolean) => boolean;
}

export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino
Expand Down Expand Up @@ -67,7 +67,13 @@ 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 = {
Expand All @@ -81,26 +87,26 @@ export function log(opts: LogOptions, level: LoggerLevel, label: string | null,
return; // do nothing
}

dest.write(event);
dest.write(event, newLine);
}

export function isLogLevelEnabled(configuredLogLevel: LoggerLevel, level: LoggerLevel) {
return levels[configuredLogLevel] <= levels[level];
}

/** 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;
Expand Down Expand Up @@ -191,14 +197,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);
Expand Down
7 changes: 4 additions & 3 deletions packages/astro/src/core/logger/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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;
},
Expand Down
Loading