chore: Use implementation class for logger from context (#1542)

* chore: Use workflow and activity class when getting the logger from context

Signed-off-by: Javier Aliaga <javier@diagrid.io>

* chore: Keep previous contructor for backward compatibility

Signed-off-by: Javier Aliaga <javier@diagrid.io>

* chore: Fix method name

Signed-off-by: Javier Aliaga <javier@diagrid.io>

---------

Signed-off-by: Javier Aliaga <javier@diagrid.io>
This commit is contained in:
Javier Aliaga 2025-09-04 21:30:37 +02:00 committed by GitHub
parent e05a744b17
commit 12a1185958
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
8 changed files with 36 additions and 11 deletions

View File

@ -32,7 +32,18 @@ class DefaultWorkflowActivityContext implements WorkflowActivityContext {
* @throws IllegalArgumentException if context is null
*/
public DefaultWorkflowActivityContext(TaskActivityContext context) throws IllegalArgumentException {
this(context, LoggerFactory.getLogger(WorkflowActivityContext.class));
this(context, WorkflowActivityContext.class);
}
/**
* Constructor for WorkflowActivityContext.
*
* @param context TaskActivityContext
* @param clazz Class to use for logger
* @throws IllegalArgumentException if context is null
*/
public DefaultWorkflowActivityContext(TaskActivityContext context, Class<?> clazz) throws IllegalArgumentException {
this(context, LoggerFactory.getLogger(clazz));
}
/**

View File

@ -42,17 +42,28 @@ public class DefaultWorkflowContext implements WorkflowContext {
private final Logger logger;
/**
* Constructor for DaprWorkflowContextImpl.
* Constructor for DefaultWorkflowContext.
*
* @param context TaskOrchestrationContext
* @throws IllegalArgumentException if context is null
*/
public DefaultWorkflowContext(TaskOrchestrationContext context) throws IllegalArgumentException {
this(context, LoggerFactory.getLogger(WorkflowContext.class));
this(context, WorkflowContext.class);
}
/**
* Constructor for DaprWorkflowContextImpl.
* Constructor for DefaultWorkflowContext.
*
* @param context TaskOrchestrationContext
* @param clazz Class to use for logger
* @throws IllegalArgumentException if context is null
*/
public DefaultWorkflowContext(TaskOrchestrationContext context, Class<?> clazz) throws IllegalArgumentException {
this(context, LoggerFactory.getLogger(clazz));
}
/**
* Constructor for DefaultWorkflowContext.
*
* @param context TaskOrchestrationContext
* @param logger Logger

View File

@ -70,7 +70,7 @@ public class WorkflowActivityClassWrapper<T extends WorkflowActivity> implements
String.format("Unable to instantiate instance of activity class '%s'", this.name), e);
}
result = activity.run(new DefaultWorkflowActivityContext(ctx));
result = activity.run(new DefaultWorkflowActivityContext(ctx, activity.getClass()));
return result;
};
}

View File

@ -51,6 +51,6 @@ public class WorkflowActivityInstanceWrapper<T extends WorkflowActivity> impleme
@Override
public TaskActivity create() {
return ctx -> activity.run(new DefaultWorkflowActivityContext(ctx));
return ctx -> activity.run(new DefaultWorkflowActivityContext(ctx, activity.getClass()));
}
}

View File

@ -57,7 +57,7 @@ class WorkflowClassWrapper<T extends Workflow> implements TaskOrchestrationFacto
);
}
workflow.run(new DefaultWorkflowContext(ctx));
workflow.run(new DefaultWorkflowContext(ctx, workflow.getClass()));
};
}
}

View File

@ -36,6 +36,6 @@ class WorkflowInstanceWrapper<T extends Workflow> implements TaskOrchestrationFa
@Override
public TaskOrchestration create() {
return ctx -> workflow.run(new DefaultWorkflowContext(ctx));
return ctx -> workflow.run(new DefaultWorkflowContext(ctx, workflow.getClass()));
}
}

View File

@ -42,13 +42,13 @@ import static org.mockito.Mockito.*;
public class DefaultWorkflowContextTest {
private DefaultWorkflowContext context;
private DefaultWorkflowContext contextWithClass;
private TaskOrchestrationContext mockInnerContext;
private WorkflowContext testWorkflowContext;
@BeforeEach
public void setUp() {
mockInnerContext = mock(TaskOrchestrationContext.class);
context = new DefaultWorkflowContext(mockInnerContext);
testWorkflowContext = new WorkflowContext() {
@Override
public Logger getLogger() {
@ -141,6 +141,8 @@ public class DefaultWorkflowContextTest {
}
};
context = new DefaultWorkflowContext(mockInnerContext);
contextWithClass = new DefaultWorkflowContext(mockInnerContext, testWorkflowContext.getClass());
}
@Test

View File

@ -3,6 +3,7 @@ package io.dapr.workflows.runtime;
import io.dapr.durabletask.TaskActivityContext;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
@ -34,7 +35,7 @@ class DefaultWorkflowActivityContextTest {
@DisplayName("Should throw IllegalArgumentException when context parameter is null")
void shouldThrowIllegalArgumentExceptionWhenContextParameterIsNull() {
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
new DefaultWorkflowActivityContext(null);
new DefaultWorkflowActivityContext(null, TaskActivityContext.class);
});
assertEquals("Context cannot be null", exception.getMessage());
}
@ -45,7 +46,7 @@ class DefaultWorkflowActivityContextTest {
TaskActivityContext mockInnerContext = mock(TaskActivityContext.class);
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
new DefaultWorkflowActivityContext(mockInnerContext, null);
new DefaultWorkflowActivityContext(mockInnerContext, (Logger) null);
});
assertEquals("Logger cannot be null", exception.getMessage());
}