Skip to content

Commit

Permalink
fix(bundler): Pass Webpack output to logger in a single record (#1414)
Browse files Browse the repository at this point in the history
Co-authored-by: James Watkins-Harvey <[email protected]>
  • Loading branch information
ikonst and mjameswh authored May 21, 2024
1 parent 61398a5 commit e3c1574
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@types/ms": "^0.7.34",
"@types/node": "^20.10.8",
"@types/stack-utils": "^2.0.3",
"@types/supports-color": "^8.1.3",
"@typescript-eslint/eslint-plugin": "^6.18.1",
"@typescript-eslint/parser": "^6.18.1",
"arg": "^5.0.2",
Expand Down
4 changes: 4 additions & 0 deletions packages/worker/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@
"rxjs": "^7.8.1",
"source-map": "^0.7.4",
"source-map-loader": "^4.0.2",
"supports-color": "^8.1.1",
"swc-loader": "^0.2.3",
"unionfs": "^4.5.1",
"webpack": "^5.89.0"
},
"devDependencies": {
"@types/supports-color": "^8.1.3"
},
"bugs": {
"url": "https://github.com/temporalio/sdk-typescript/issues"
},
Expand Down
25 changes: 24 additions & 1 deletion packages/worker/src/logger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { formatWithOptions } from 'node:util';
import * as supportsColor from 'supports-color';
import { getTimeOfDay } from '@temporalio/core-bridge';
import { LogLevel, LogMetadata, Logger } from '@temporalio/common';

Expand All @@ -13,11 +14,23 @@ export interface LogEntry {
meta?: LogMetadata;
}

/**
* @internal
*/
interface LoggerWithColorSupport extends Logger {
[loggerHasColorsSymbol]?: boolean;
}

export const LogTimestamp = Symbol.for('log_timestamp');

const severities: LogLevel[] = ['TRACE', 'DEBUG', 'INFO', 'WARN', 'ERROR'];

const format = formatWithOptions.bind(undefined, { colors: true });
/**
* @internal
*/
const loggerHasColorsSymbol = Symbol.for('logger_has_colors');
const stderrHasColors = !!supportsColor.stderr;
const format = formatWithOptions.bind(undefined, { colors: stderrHasColors });

/**
* Log messages to `stderr` using basic formatting
Expand Down Expand Up @@ -53,6 +66,8 @@ export class DefaultLogger implements Logger {
protected readonly logFunction = defaultLogFunction
) {
this.severity = severities.indexOf(this.level);
(this as LoggerWithColorSupport)[loggerHasColorsSymbol] =
(logFunction === defaultLogFunction && stderrHasColors) ?? false;
}

log(level: LogLevel, message: string, meta?: LogMetadata): void {
Expand Down Expand Up @@ -88,6 +103,13 @@ export class DefaultLogger implements Logger {
}
}

/**
* @internal
*/
export function hasColorSupport(logger: Logger): boolean {
return (logger as LoggerWithColorSupport)[loggerHasColorsSymbol] ?? false;
}

export function withMetadata(logger: Logger, meta: LogMetadata | (() => LogMetadata)): Logger {
return new LoggerWithMetadata(logger, meta);
}
Expand All @@ -105,6 +127,7 @@ class LoggerWithMetadata implements Logger {
this.parentLogger = parent;
this.metaChain = [meta];
}
(this as LoggerWithColorSupport)[loggerHasColorsSymbol] = hasColorSupport(parent);
}

log(level: LogLevel, message: string, meta?: LogMetadata): void {
Expand Down
12 changes: 7 additions & 5 deletions packages/worker/src/workflow/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import util from 'node:util';
import * as unionfs from 'unionfs';
import * as memfs from 'memfs';
import { Configuration, webpack } from 'webpack';
import { DefaultLogger, Logger } from '../logger';
import { DefaultLogger, Logger, hasColorSupport } from '../logger';
import { toMB } from '../utils';

export const defaultWorkflowInterceptorModules = [require.resolve('../workflow-log-interceptor')];
Expand Down Expand Up @@ -262,10 +262,12 @@ exports.importInterceptors = function importInterceptors() {
const hasError = stats.hasErrors();
// To debug webpack build:
// const lines = stats.toString({ preset: 'verbose' }).split('\n');
const lines = stats.toString({ chunks: false, colors: true, errorDetails: true }).split('\n');
for (const line of lines) {
this.logger[hasError ? 'error' : 'info'](line);
}
const webpackOutput = stats.toString({
chunks: false,
colors: hasColorSupport(this.logger),
errorDetails: true,
});
this.logger[hasError ? 'error' : 'info'](webpackOutput);
if (hasError) {
reject(
new Error(
Expand Down

0 comments on commit e3c1574

Please sign in to comment.