Skip to content

Commit

Permalink
Properly deal with lazy logger initialization in defaultSink().
Browse files Browse the repository at this point in the history
  • Loading branch information
mjameswh committed Apr 9, 2024
1 parent 18fa479 commit ac116d3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
9 changes: 8 additions & 1 deletion packages/worker/src/worker-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,7 +615,7 @@ export interface ReplayWorkerOptions
* Build the sink used internally by the SDK to forwards log messages from the Workflow sandbox to an actual logger.
*
* @param logger a {@link Logger} - defaults to the {@link Runtime} singleton logger.
*
* @deprecated Calling `defaultSink()` is no longer required. To configure a custom logger, set the
* {@link Runtime.logger} property instead.
*/
Expand All @@ -625,6 +625,13 @@ export function defaultSinks(logger?: Logger): InjectedSinks<LoggerSinks> {
// code that is still calling defaultSinks() expects return type to match the deprecated LoggerSinks API. Silently
// cast just to mask type checking issues, even though we know this is wrong. Users shouldn't call functions directly
// on the returned object anyway.

// If no logger was provided, the legacy behavior was to _lazyly_ set the sink's logger to the Runtime's logger.
// This was required because may call defaultSinks() before the Runtime is initialized. We preserve that behavior
// here by silently not initializing the sink if no logger is provided.
// eslint-disable-next-line deprecation/deprecation
if (!logger) return {} as InjectedSinks<LoggerSinks>;

// eslint-disable-next-line deprecation/deprecation
return initLoggerSink(logger) as unknown as InjectedSinks<LoggerSinks>;
}
Expand Down
19 changes: 7 additions & 12 deletions packages/worker/src/workflow/logger.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
import { type LoggerSinksInternal } from '@temporalio/workflow/lib/logs';
import { type InjectedSinks } from '../sinks';
import { Runtime } from '../runtime';
import { withMetadata, type Logger } from '../logger';

/**
* Injects a logger sink that forwards to the worker's logger
*/
export function initLoggerSink(logger?: Logger): InjectedSinks<LoggerSinksInternal> {
export function initLoggerSink(logger: Logger): InjectedSinks<LoggerSinksInternal> {
const loggerWithMetadata = withMetadata(logger, {});
return {
__temporal_logger: {
trace: {
fn(_, message, attrs) {
logger ??= withMetadata(Runtime.instance().logger);
logger.trace(message, attrs);
loggerWithMetadata.trace(message, attrs);
},
},
debug: {
fn(_, message, attrs) {
logger ??= Runtime.instance().logger;
logger.debug(message, attrs);
loggerWithMetadata.debug(message, attrs);
},
},
info: {
fn(_, message, attrs) {
logger ??= Runtime.instance().logger;
logger.info(message, attrs);
loggerWithMetadata.info(message, attrs);
},
},
warn: {
fn(_, message, attrs) {
logger ??= Runtime.instance().logger;
logger.warn(message, attrs);
loggerWithMetadata.warn(message, attrs);
},
},
error: {
fn(_, message, attrs) {
logger ??= Runtime.instance().logger;
logger.error(message, attrs);
loggerWithMetadata.error(message, attrs);
},
},
},
Expand Down

0 comments on commit ac116d3

Please sign in to comment.