Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 8 additions & 4 deletions code/core/src/bin/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ addToGlobalContext('cliVersion', version);
*/

const handleCommandFailure = async (logFilePath: string | boolean): Promise<never> => {
const logFile = await logTracker.writeToFile(logFilePath);
logger.log(`Debug logs are written to: ${logFile}`);
try {
const logFile = await logTracker.writeToFile(logFilePath);
logger.log(`Debug logs are written to: ${logFile}`);
} catch {}
Comment on lines +30 to +33

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move the try/catch block inside of writeToFile so that this is applied globally and if we end up introducing this call elsewhere, we don't have to think about wrapping it in a try/catch?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about it, but then we also have to move logger.log inside of it (which we can't because sometimes we use logger.info and sometimes logger.outro), otherwise we introduce an if block.

logger.outro('Storybook exited with an error');
process.exit(1);
};
Expand Down Expand Up @@ -66,8 +68,10 @@ const command = (name: string) =>
})
.hook('postAction', async (command) => {
if (logTracker.shouldWriteLogsToFile) {
const logFile = await logTracker.writeToFile(command.getOptionValue('logfile'));
logger.outro(`Debug logs are written to: ${logFile}`);
try {
const logFile = await logTracker.writeToFile(command.getOptionValue('logfile'));
logger.outro(`Debug logs are written to: ${logFile}`);
} catch {}
}
});

Expand Down
2 changes: 0 additions & 2 deletions code/core/src/core-server/withTelemetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import {
import type { EventType } from 'storybook/internal/telemetry';
import type { CLIOptions } from 'storybook/internal/types';

import { dedent } from 'ts-dedent';

import { StorybookError } from '../storybook-error';

type TelemetryOptions = {
Expand Down
2 changes: 2 additions & 0 deletions code/core/src/node-logger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ export { protectUrls, createHyperlink } from './wrap-utils';
export { CLI_COLORS } from './logger/colors';
export { ConsoleLogger, StyledConsoleLogger } from './logger/console';

export type { LogLevel } from './logger/logger';

// The default is stderr, which can cause some tools (like rush.js) to think
// there are issues with the build: https://github.com/storybookjs/storybook/issues/14621
npmLog.stream = process.stdout;
Expand Down
4 changes: 3 additions & 1 deletion code/core/src/types/modules/core-common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import type { FileSystemCache } from 'storybook/internal/common';
import { type StoryIndexGenerator } from 'storybook/internal/core-server';
import { type CsfFile } from 'storybook/internal/csf-tools';
import type { LogLevel } from 'storybook/internal/node-logger';

import type { Server as HttpServer, IncomingMessage, ServerResponse } from 'http';
import type { Server as NetServer } from 'net';
Expand Down Expand Up @@ -173,7 +174,8 @@ export interface CLIBaseOptions {
disableTelemetry?: boolean;
enableCrashReports?: boolean;
configDir?: string;
loglevel?: string;
loglevel?: LogLevel;
logfile?: string | boolean;
quiet?: boolean;
}

Expand Down
6 changes: 5 additions & 1 deletion code/frameworks/angular/build-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,11 @@
"loglevel": {
"type": "string",
"description": "Controls level of logging during build. Can be one of: [silly, verbose, info (default), warn, error, silent].",
"pattern": "(silly|verbose|info|warn|silent)"
"pattern": "(trace|debug|info|warn|error|silent)"
},
Comment thread
valentinpalkovic marked this conversation as resolved.
"logfile": {
"type": "string",
"description": "If provided, the log output will be written to the specified file path."
},
"debugWebpack": {
"type": "boolean",
Expand Down
15 changes: 14 additions & 1 deletion code/frameworks/angular/src/builders/build-storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getEnvConfig, getProjectRoot, versions } from 'storybook/internal/commo
import { buildStaticStandalone, withTelemetry } from 'storybook/internal/core-server';
import { addToGlobalContext } from 'storybook/internal/telemetry';
import type { CLIOptions } from 'storybook/internal/types';
import { logger } from 'storybook/internal/node-logger';
import { logger, logTracker } from 'storybook/internal/node-logger';

import type {
BuilderContext,
Expand Down Expand Up @@ -60,6 +60,7 @@ export type StorybookBuilderOptions = JsonObject & {
| 'statsJson'
| 'disableTelemetry'
| 'debugWebpack'
| 'logfile'
| 'previewUrl'
>;

Expand All @@ -71,6 +72,14 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = async (
options,
context
): Promise<BuilderOutput> => {
// Apply logger configuration from builder options
if (options.loglevel) {
logger.setLogLevel(options.loglevel);
}
if (options.logfile) {
logTracker.enableLogWriting();
}
Comment thread
valentinpalkovic marked this conversation as resolved.

logger.intro('Building Storybook');

const { tsConfig } = await setup(options, context);
Expand Down Expand Up @@ -147,6 +156,10 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = async (
};

await runInstance({ ...standaloneOptions, mode: 'static' });
if (logTracker.shouldWriteLogsToFile) {
const logFile = await logTracker.writeToFile(options.logfile as any);
logger.info(`Debug logs are written to: ${logFile}`);
}
Comment thread
valentinpalkovic marked this conversation as resolved.
logger.outro('Storybook build completed successfully');
return { success: true } as BuilderOutput;
};
Expand Down
20 changes: 19 additions & 1 deletion code/frameworks/angular/src/builders/start-storybook/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getEnvConfig, getProjectRoot, versions } from 'storybook/internal/commo
import { buildDevStandalone, withTelemetry } from 'storybook/internal/core-server';
import { addToGlobalContext } from 'storybook/internal/telemetry';
import type { CLIOptions } from 'storybook/internal/types';
import { logger } from 'storybook/internal/node-logger';
import { logger, logTracker } from 'storybook/internal/node-logger';

import type {
BuilderContext,
Expand Down Expand Up @@ -65,6 +65,7 @@ export type StorybookBuilderOptions = JsonObject & {
| 'open'
| 'docs'
| 'debugWebpack'
| 'logfile'
| 'webpackStatsJson'
| 'statsJson'
| 'loglevel'
Expand All @@ -80,6 +81,14 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = (
return new Observable<BuilderOutput>((observer) => {
(async () => {
try {
// Apply logger configuration from builder options
if (options.loglevel) {
logger.setLogLevel(options.loglevel);
}
if (options.logfile) {
logTracker.enableLogWriting();
}

logger.intro('Starting Storybook');

const { tsConfig } = await setup(options, context);
Expand Down Expand Up @@ -187,6 +196,15 @@ const commandBuilder: BuilderHandlerFn<StorybookBuilderOptions> = (
// so the dev server continues running. Architect will keep subscribing
// until the Observable completes, which allows watch mode to work.
} catch (error) {
// Write logs to file on failure when enabled
try {
Comment thread
yannbf marked this conversation as resolved.
if (logTracker.shouldWriteLogsToFile) {
try {
const logFile = await logTracker.writeToFile(options.logfile as any);
logger.outro(`Debug logs are written to: ${logFile}`);
} catch {}
}
} catch {}
observer.error(error);
}
})();
Expand Down
6 changes: 5 additions & 1 deletion code/frameworks/angular/start-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,11 @@
"loglevel": {
"type": "string",
"description": "Controls level of logging during build. Can be one of: [silly, verbose, info (default), warn, error, silent].",
"pattern": "(silly|verbose|info|warn|silent)"
"pattern": "(trace|debug|info|warn|error|silent)"
},
Comment thread
valentinpalkovic marked this conversation as resolved.
"logfile": {
"type": "string",
"description": "If provided, the log output will be written to the specified file path."
},
"sourceMap": {
"type": ["boolean", "object"],
Expand Down
12 changes: 8 additions & 4 deletions code/lib/cli-storybook/src/bin/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ const handleCommandFailure =
logger.error(String(error));
}

const logFile = await logTracker.writeToFile(logFilePath);
logger.log(`Debug logs are written to: ${logFile}`);
try {
const logFile = await logTracker.writeToFile(logFilePath);
logger.log(`Debug logs are written to: ${logFile}`);
} catch {}
logger.outro('');
process.exit(1);
};
Expand Down Expand Up @@ -79,8 +81,10 @@ const command = (name: string) =>
})
.hook('postAction', async (command) => {
if (logTracker.shouldWriteLogsToFile) {
const logFile = await logTracker.writeToFile(command.getOptionValue('logfile'));
logger.log(`Debug logs are written to: ${logFile}`);
try {
const logFile = await logTracker.writeToFile(command.getOptionValue('logfile'));
logger.log(`Debug logs are written to: ${logFile}`);
} catch {}
logger.outro(CLI_COLORS.success('Done!'));
}
});
Expand Down
2 changes: 2 additions & 0 deletions code/lib/cli-storybook/src/upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
logger,
prompt,
} from 'storybook/internal/node-logger';
import type { LogLevel } from 'storybook/internal/node-logger';
import {
UpgradeStorybookToLowerVersionError,
UpgradeStorybookUnknownCurrentVersionError,
Expand Down Expand Up @@ -123,6 +124,7 @@ export type UpgradeOptions = {
configDir?: string[];
fixId?: string;
skipInstall?: boolean;
loglevel?: LogLevel;
logfile?: string | boolean;
};

Expand Down
4 changes: 3 additions & 1 deletion code/lib/create-storybook/src/bin/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ const createStorybookProgram = program
})
.hook('postAction', async (command) => {
if (logTracker.shouldWriteLogsToFile) {
await logTracker.writeToFile(command.getOptionValue('logfile'));
try {
await logTracker.writeToFile(command.getOptionValue('logfile'));
} catch {}
}
});

Expand Down
6 changes: 4 additions & 2 deletions code/lib/create-storybook/src/commands/FinalizationCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ export class FinalizationCommand {
);
this.printNextSteps(storybookCommand);

const logFile = await logTracker.writeToFile(this.logfile);
logger.warn(`Debug logs are written to: ${logFile}`);
try {
const logFile = await logTracker.writeToFile(this.logfile);
logger.warn(`Debug logs are written to: ${logFile}`);
} catch {}
}

/** Print success message with feature summary */
Expand Down