Skip to content

Commit

Permalink
Ensure replayed logs aren't emitted (#403)
Browse files Browse the repository at this point in the history
Also create a console.log()
  • Loading branch information
jackkleeman authored Jul 12, 2024
1 parent abdd44d commit 2e9baa7
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
19 changes: 12 additions & 7 deletions packages/restate-sdk/src/logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,9 @@ export const defaultLogger: Logger = (
message?: any,
...optionalParams: any[]
) => {
if (params.replaying) {
return;
}
if (logLevel(params.level) < logLevel(DEFAULT_LOGGER_LOG_LEVEL)) {
return;
}
Expand Down Expand Up @@ -171,6 +174,13 @@ export function createRestateConsole(
context?: LoggerContext,
isReplaying: () => boolean = () => false
): Console {
const info = loggerForLevel(
logger,
source,
RestateLogLevel.INFO,
isReplaying,
context
);
return Object.create(console, {
trace: loggerForLevel(
logger,
Expand All @@ -186,13 +196,8 @@ export function createRestateConsole(
isReplaying,
context
),
info: loggerForLevel(
logger,
source,
RestateLogLevel.INFO,
isReplaying,
context
),
info,
log: info,
warn: loggerForLevel(
logger,
source,
Expand Down
9 changes: 6 additions & 3 deletions packages/restate-sdk/src/state_machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,15 @@ export class StateMachine implements RestateStreamConsumer {
this.localStateStore = invocation.localStateStore;
this.console = createStateMachineConsole(logger, loggerContext);

this.journal = new Journal(this.invocation);
this.restateContext = new ContextImpl(
this.invocation.id,
// The console exposed by RestateContext filters logs in replay, while the internal one is based on the ENV variables.
createRestateConsole(logger, LogSource.USER, loggerContext, () =>
this.journal.isReplaying()
createRestateConsole(
logger,
LogSource.USER,
loggerContext,
this.journal.isReplaying.bind(this.journal)
),
handlerKind,
invocation.userKey,
Expand All @@ -108,7 +112,6 @@ export class StateMachine implements RestateStreamConsumer {
connection.headers(),
this
);
this.journal = new Journal(this.invocation);
this.promiseCombinatorTracker = new PromiseCombinatorTracker(
this.readCombinatorOrderEntry.bind(this),
this.writeCombinatorOrderEntry.bind(this)
Expand Down

0 comments on commit 2e9baa7

Please sign in to comment.