Skip to content

Commit 19169db

Browse files
ematipicoflorian-lefebvre
andauthoredDec 20, 2023
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>
1 parent c384f69 commit 19169db

File tree

4 files changed

+34
-19
lines changed

4 files changed

+34
-19
lines changed
 

‎.changeset/popular-meals-yell.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'astro': patch
3+
---
4+
5+
Improves logging of the generated pages during the build

‎packages/astro/src/core/build/generate.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,14 @@ async function generatePage(
304304
for (let i = 0; i < paths.length; i++) {
305305
const path = paths[i];
306306
pipeline.getEnvironment().logger.debug('build', `Generating: ${path}`);
307+
const filePath = getOutputFilename(pipeline.getConfig(), path, pageData.route.type);
308+
const lineIcon = i === paths.length - 1 ? '└─' : '├─';
309+
logger.info(null, ` ${blue(lineIcon)} ${dim(filePath)}`, false);
307310
await generatePath(path, pipeline, generationOptions, route);
308311
const timeEnd = performance.now();
309312
const timeChange = getTimeStat(prevTimeEnd, timeEnd);
310313
const timeIncrease = `(+${timeChange})`;
311-
const filePath = getOutputFilename(pipeline.getConfig(), path, pageData.route.type);
312-
const lineIcon = i === paths.length - 1 ? '└─' : '├─';
313-
logger.info(null, ` ${blue(lineIcon)} ${dim(filePath)} ${dim(timeIncrease)}`);
314+
logger.info('SKIP_FORMAT', ` ${dim(timeIncrease)}`);
314315
prevTimeEnd = timeEnd;
315316
}
316317
}

‎packages/astro/src/core/logger/core.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export interface LogMessage {
5656
label: string | null;
5757
level: LoggerLevel;
5858
message: string;
59+
newLine: boolean;
5960
}
6061

6162
export const levels: Record<LoggerLevel, number> = {
@@ -67,13 +68,20 @@ export const levels: Record<LoggerLevel, number> = {
6768
};
6869

6970
/** Full logging API */
70-
export function log(opts: LogOptions, level: LoggerLevel, label: string | null, message: string) {
71+
export function log(
72+
opts: LogOptions,
73+
level: LoggerLevel,
74+
label: string | null,
75+
message: string,
76+
newLine = true
77+
) {
7178
const logLevel = opts.level;
7279
const dest = opts.dest;
7380
const event: LogMessage = {
7481
label,
7582
level,
7683
message,
84+
newLine,
7785
};
7886

7987
// test if this level is enabled or not
@@ -89,18 +97,18 @@ export function isLogLevelEnabled(configuredLogLevel: LoggerLevel, level: Logger
8997
}
9098

9199
/** Emit a user-facing message. Useful for UI and other console messages. */
92-
export function info(opts: LogOptions, label: string | null, message: string) {
93-
return log(opts, 'info', label, message);
100+
export function info(opts: LogOptions, label: string | null, message: string, newLine = true) {
101+
return log(opts, 'info', label, message, newLine);
94102
}
95103

96104
/** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */
97-
export function warn(opts: LogOptions, label: string | null, message: string) {
98-
return log(opts, 'warn', label, message);
105+
export function warn(opts: LogOptions, label: string | null, message: string, newLine = true) {
106+
return log(opts, 'warn', label, message, newLine);
99107
}
100108

101109
/** Emit a error message, Useful when Astro can't recover from some error. */
102-
export function error(opts: LogOptions, label: string | null, message: string) {
103-
return log(opts, 'error', label, message);
110+
export function error(opts: LogOptions, label: string | null, message: string, newLine = true) {
111+
return log(opts, 'error', label, message, newLine);
104112
}
105113

106114
type LogFn = typeof info | typeof warn | typeof error;
@@ -191,14 +199,14 @@ export class Logger {
191199
this.options = options;
192200
}
193201

194-
info(label: LoggerLabel | null, message: string) {
195-
info(this.options, label, message);
202+
info(label: LoggerLabel | null, message: string, newLine = true) {
203+
info(this.options, label, message, newLine);
196204
}
197-
warn(label: LoggerLabel | null, message: string) {
198-
warn(this.options, label, message);
205+
warn(label: LoggerLabel | null, message: string, newLine = true) {
206+
warn(this.options, label, message, newLine);
199207
}
200-
error(label: LoggerLabel | null, message: string) {
201-
error(this.options, label, message);
208+
error(label: LoggerLabel | null, message: string, newLine = true) {
209+
error(this.options, label, message, newLine);
202210
}
203211
debug(label: LoggerLabel, ...messages: any[]) {
204212
debug(label, ...messages);

‎packages/astro/src/core/logger/node.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ type ConsoleStream = Writable & {
77
};
88

99
export const nodeLogDestination: LogWritable<LogMessage> = {
10-
write(event: LogMessage) {
10+
write(event: LogMessage, newLine = true) {
1111
let dest: ConsoleStream = process.stderr;
1212
if (levels[event.level] < levels['error']) {
1313
dest = process.stdout;
1414
}
15+
let trailingLine = event.newLine ? '\n' : '';
1516
if (event.label === 'SKIP_FORMAT') {
16-
dest.write(event.message + '\n');
17+
dest.write(event.message + trailingLine);
1718
} else {
18-
dest.write(getEventPrefix(event) + ' ' + event.message + '\n');
19+
dest.write(getEventPrefix(event) + ' ' + event.message + trailingLine);
1920
}
2021
return true;
2122
},

0 commit comments

Comments
 (0)
Please sign in to comment.