Skip to content

Commit

Permalink
use debug package for debug logs
Browse files Browse the repository at this point in the history
  • Loading branch information
FredKSchott committed Jan 31, 2022
1 parent 9575ce6 commit 38b5b80
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 39 deletions.
2 changes: 2 additions & 0 deletions packages/astro/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,12 @@
"@proload/core": "^0.2.1",
"@proload/plugin-tsm": "^0.1.0",
"@types/babel__core": "^7.1.15",
"@types/debug": "^4.1.7",
"@web/parse5-utils": "^1.3.0",
"astring": "^1.7.5",
"ci-info": "^3.2.0",
"common-ancestor-path": "^1.0.1",
"debug": "^4.3.3",
"eol": "^0.9.1",
"es-module-lexer": "^0.9.3",
"esbuild": "0.13.7",
Expand Down
14 changes: 6 additions & 8 deletions packages/astro/src/core/build/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class AstroBuilder {
this.viteConfig = viteConfig;
const viteServer = await vite.createServer(viteConfig);
this.viteServer = viteServer;
debug(logging, 'build', timerMessage('Vite started', timer.viteStart));
debug('build', timerMessage('Vite started', timer.viteStart));

timer.loadStart = performance.now();
const { assets, allPages } = await collectPagesData({
Expand All @@ -94,13 +94,13 @@ class AstroBuilder {
// TODO: add better type inference to data.preload[1]
const frontmatter = (data.preload[1] as any).frontmatter;
if (Boolean(frontmatter.draft) && !this.config.buildOptions.drafts) {
debug(logging, 'build', timerMessage(`Skipping draft page ${page}`, timer.loadStart));
debug('build', timerMessage(`Skipping draft page ${page}`, timer.loadStart));
delete allPages[page];
}
}
});

debug(logging, 'build', timerMessage('All pages loaded', timer.loadStart));
debug('build', timerMessage('All pages loaded', timer.loadStart));

// The names of each pages
const pageNames: string[] = [];
Expand Down Expand Up @@ -132,7 +132,7 @@ class AstroBuilder {
viteServer: this.viteServer,
});
}
debug(logging, 'build', timerMessage('Vite build finished', timer.buildStart));
debug('build', timerMessage('Vite build finished', timer.buildStart));

// Write any additionally generated assets to disk.
timer.assetsStart = performance.now();
Expand All @@ -143,7 +143,7 @@ class AstroBuilder {
fs.writeFileSync(filePath, assets[k], 'utf8');
delete assets[k]; // free up memory
});
debug(logging, 'build', timerMessage('Additional assets copied', timer.assetsStart));
debug('build', timerMessage('Additional assets copied', timer.assetsStart));

// Build your final sitemap.
timer.sitemapStart = performance.now();
Expand All @@ -153,7 +153,7 @@ class AstroBuilder {
await fs.promises.mkdir(new URL('./', sitemapPath), { recursive: true });
await fs.promises.writeFile(sitemapPath, sitemap, 'utf8');
}
debug(logging, 'build', timerMessage('Sitemap built', timer.sitemapStart));
debug('build', timerMessage('Sitemap built', timer.sitemapStart));

// You're done! Time to clean up.
await viteServer.close();
Expand All @@ -164,8 +164,6 @@ class AstroBuilder {

/** Stats */
private async printStats({ logging, timeStart, pageCount }: { logging: LogOptions; timeStart: number; pageCount: number }) {
/* eslint-disable no-console */
debug(logging, ''); // empty line for debug
const buildTime = performance.now() - timeStart;
const total = buildTime < 750 ? `${Math.round(buildTime)}ms` : `${(buildTime / 1000).toFixed(2)}s`;
const perPage = `${Math.round(buildTime / pageCount)}ms`;
Expand Down
8 changes: 4 additions & 4 deletions packages/astro/src/core/build/page-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
})
.then((routes) => {
const html = `${route.pathname}`.replace(/\/?$/, '/index.html');
debug(logging, 'build', `├── ${colors.bold(colors.green('✔'))} ${route.component}${colors.yellow(html)}`);
debug('build', `├── ${colors.bold(colors.green('✔'))} ${route.component}${colors.yellow(html)}`);
return routes;
})
.catch((err) => {
debug(logging, 'build', `├── ${colors.bold(colors.red('✘'))} ${route.component}`);
debug('build', `├── ${colors.bold(colors.red('✘'))} ${route.component}`);
throw err;
}),
};
Expand All @@ -69,11 +69,11 @@ export async function collectPagesData(opts: CollectPagesDataOptions): Promise<C
const result = await getStaticPathsForRoute(opts, route)
.then((_result) => {
const label = _result.staticPaths.length === 1 ? 'page' : 'pages';
debug(logging, 'build', `├── ${colors.bold(colors.green('✔'))} ${route.component}${colors.magenta(`[${_result.staticPaths.length} ${label}]`)}`);
debug('build', `├── ${colors.bold(colors.green('✔'))} ${route.component}${colors.magenta(`[${_result.staticPaths.length} ${label}]`)}`);
return _result;
})
.catch((err) => {
debug(logging, 'build', `├── ${colors.bold(colors.red('✗'))} ${route.component}`);
debug('build', `├── ${colors.bold(colors.red('✗'))} ${route.component}`);
throw err;
});
const rssFn = generateRssFunction(astroConfig.buildOptions.site, route);
Expand Down
5 changes: 2 additions & 3 deletions packages/astro/src/core/build/static-build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ async function collectRenderers(opts: StaticBuildOptions): Promise<Renderer[]> {
}

async function generatePages(result: RollupOutput, opts: StaticBuildOptions, internals: BuildInternals, facadeIdToPageDataMap: Map<string, PageBuildData>) {
debug(opts.logging, 'generate', 'End build step, now generating');
debug('build', 'Finish build. Begin generating.');

// Get renderers to be shared for each page generation.
const renderers = await collectRenderers(opts);
Expand Down Expand Up @@ -331,11 +331,10 @@ async function generatePath(pathname: string, opts: StaticBuildOptions, gopts: G
const [params, pageProps] = await getParamsAndProps({
route: pageData.route,
routeCache,
logging,
pathname,
});

debug(logging, 'generate', `Generating: ${pathname}`);
debug('build', `Generating: ${pathname}`);

const rootpath = new URL(astroConfig.buildOptions.site || 'http://localhost/').pathname;
const links = new Set<SSRElement>(
Expand Down
26 changes: 17 additions & 9 deletions packages/astro/src/core/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { bold, blue, dim, red, grey, underline, yellow } from 'kleur/colors';
import { performance } from 'perf_hooks';
import { Writable } from 'stream';
import stringWidth from 'string-width';
import debugPackage from 'debug';
import { format as utilFormat } from 'util';

type ConsoleStream = Writable & {
Expand Down Expand Up @@ -61,7 +62,7 @@ interface LogWritable<T> extends Writable {
}

export type LoggerLevel = 'debug' | 'info' | 'warn' | 'error' | 'silent'; // same as Pino
export type LoggerEvent = 'debug' | 'info' | 'warn' | 'error';
export type LoggerEvent = 'info' | 'warn' | 'error';

export interface LogOptions {
dest?: LogWritable<LogMessage>;
Expand Down Expand Up @@ -107,27 +108,35 @@ export function log(opts: LogOptions = {}, level: LoggerLevel, type: string | nu
dest.write(event);
}

/** Emit a message only shown in debug mode */
export function debug(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'debug', type, ...messages);
const debuggers: Record<string, debugPackage.Debugger['log']> = {};
/**
* Emit a message only shown in debug mode.
* Astro (along with many of its dependencies) uses the `debug` package for debug logging.
* You can enable these logs with the `DEBUG=astro:*` environment variable.
* More info https://github.com/debug-js/debug#environment-variables
*/
export function debug(type: string, ...messages: Array<any>) {
const namespace = `astro:${type}`;
debuggers[namespace] = debuggers[namespace] || debugPackage(namespace);
return debuggers[namespace](messages);
}

/** Emit a general info message (be careful using this too much!) */
/** Emit a user-facing message. Useful for UI and other console messages. */
export function info(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'info', type, ...messages);
}

/** Emit a warning a user should be aware of */
/** Emit a warning message. Useful for high-priority messages that aren't necessarily errors. */
export function warn(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'warn', type, ...messages);
}

/** Emit a fatal error message the user should address. */
/** Emit a error message, Useful when Astro can't recover from some error. */
export function error(opts: LogOptions, type: string | null, ...messages: Array<any>) {
return log(opts, 'error', type, ...messages);
}

type LogFn = typeof debug | typeof info | typeof warn | typeof error;
type LogFn = typeof info | typeof warn | typeof error;

export function table(opts: LogOptions, columns: number[]) {
return function logTable(logFn: LogFn, ...input: Array<any>) {
Expand Down Expand Up @@ -163,7 +172,6 @@ ${frame}

// A default logger for when too lazy to pass LogOptions around.
export const logger = {
debug: debug.bind(null, defaultLogOptions),
info: info.bind(null, defaultLogOptions),
warn: warn.bind(null, defaultLogOptions),
error: error.bind(null, defaultLogOptions),
Expand Down
14 changes: 2 additions & 12 deletions packages/astro/src/core/ssr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,7 @@ export async function preload({ astroConfig, filePath, viteServer }: SSROptions)
return [renderers, mod];
}

export async function getParamsAndProps({
route,
routeCache,
pathname,
logging,
}: {
route: RouteData | undefined;
routeCache: RouteCache;
pathname: string;
logging: LogOptions;
}): Promise<[Params, Props]> {
export async function getParamsAndProps({ route, routeCache, pathname }: { route: RouteData | undefined; routeCache: RouteCache; pathname: string }): Promise<[Params, Props]> {
// Handle dynamic routes
let params: Params = {};
let pageProps: Props;
Expand All @@ -151,7 +141,7 @@ export async function getParamsAndProps({
throw new Error(`[${route.component}] Internal error: route cache was empty, but expected to be full.`);
}
const paramsKey = JSON.stringify(params);
const matchedStaticPath = findPathItemByKey(routeCacheEntry.staticPaths, paramsKey, logging);
const matchedStaticPath = findPathItemByKey(routeCacheEntry.staticPaths, paramsKey);
if (!matchedStaticPath) {
throw new Error(`[getStaticPaths] route pattern matched, but no matching static path found. (${pathname})`);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/ssr/route-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,12 @@ export class RouteCache {
}
}

export function findPathItemByKey(staticPaths: GetStaticPathsResultKeyed, paramsKey: string, logging: LogOptions) {
export function findPathItemByKey(staticPaths: GetStaticPathsResultKeyed, paramsKey: string) {
let matchedStaticPath = staticPaths.keyed.get(paramsKey);
if (matchedStaticPath) {
return matchedStaticPath;
}

debug(logging, 'findPathItemByKey', `Unexpected cache miss looking for ${paramsKey}`);
debug('findPathItemByKey', `Unexpected cache miss looking for ${paramsKey}`);
matchedStaticPath = staticPaths.find(({ params: _params }) => JSON.stringify(_params) === paramsKey);
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1752,7 +1752,7 @@
dependencies:
"@types/node" "*"

"@types/debug@^4.0.0":
"@types/debug@^4.0.0", "@types/debug@^4.1.7":
version "4.1.7"
resolved "https://registry.yarnpkg.com/@types/debug/-/debug-4.1.7.tgz#7cc0ea761509124709b8b2d1090d8f6c17aadb82"
integrity sha512-9AonUzyTjXXhEOa0DnqpzZi6VHlqKMswga9EXjpXnnqxwLtdvPPtlO8evrI5D9S6asFRCQ6v+wpiUKbw+vKqyg==
Expand Down

0 comments on commit 38b5b80

Please sign in to comment.