Skip to content

Commit 12a1185

Browse files
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>
1 parent e05a744 commit 12a1185

File tree

8 files changed

+36
-11
lines changed

8 files changed

+36
-11
lines changed

sdk-workflows/src/main/java/io/dapr/workflows/runtime/DefaultWorkflowActivityContext.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,18 @@ class DefaultWorkflowActivityContext implements WorkflowActivityContext {
3232
* @throws IllegalArgumentException if context is null
3333
*/
3434
public DefaultWorkflowActivityContext(TaskActivityContext context) throws IllegalArgumentException {
35-
this(context, LoggerFactory.getLogger(WorkflowActivityContext.class));
35+
this(context, WorkflowActivityContext.class);
36+
}
37+
38+
/**
39+
* Constructor for WorkflowActivityContext.
40+
*
41+
* @param context TaskActivityContext
42+
* @param clazz Class to use for logger
43+
* @throws IllegalArgumentException if context is null
44+
*/
45+
public DefaultWorkflowActivityContext(TaskActivityContext context, Class<?> clazz) throws IllegalArgumentException {
46+
this(context, LoggerFactory.getLogger(clazz));
3647
}
3748

3849
/**

sdk-workflows/src/main/java/io/dapr/workflows/runtime/DefaultWorkflowContext.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,28 @@ public class DefaultWorkflowContext implements WorkflowContext {
4242
private final Logger logger;
4343

4444
/**
45-
* Constructor for DaprWorkflowContextImpl.
45+
* Constructor for DefaultWorkflowContext.
4646
*
4747
* @param context TaskOrchestrationContext
4848
* @throws IllegalArgumentException if context is null
4949
*/
5050
public DefaultWorkflowContext(TaskOrchestrationContext context) throws IllegalArgumentException {
51-
this(context, LoggerFactory.getLogger(WorkflowContext.class));
51+
this(context, WorkflowContext.class);
5252
}
5353

5454
/**
55-
* Constructor for DaprWorkflowContextImpl.
55+
* Constructor for DefaultWorkflowContext.
56+
*
57+
* @param context TaskOrchestrationContext
58+
* @param clazz Class to use for logger
59+
* @throws IllegalArgumentException if context is null
60+
*/
61+
public DefaultWorkflowContext(TaskOrchestrationContext context, Class<?> clazz) throws IllegalArgumentException {
62+
this(context, LoggerFactory.getLogger(clazz));
63+
}
64+
65+
/**
66+
* Constructor for DefaultWorkflowContext.
5667
*
5768
* @param context TaskOrchestrationContext
5869
* @param logger Logger

sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowActivityClassWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public TaskActivity create() {
7070
String.format("Unable to instantiate instance of activity class '%s'", this.name), e);
7171
}
7272

73-
result = activity.run(new DefaultWorkflowActivityContext(ctx));
73+
result = activity.run(new DefaultWorkflowActivityContext(ctx, activity.getClass()));
7474
return result;
7575
};
7676
}

sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowActivityInstanceWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,6 @@ public String getName() {
5151

5252
@Override
5353
public TaskActivity create() {
54-
return ctx -> activity.run(new DefaultWorkflowActivityContext(ctx));
54+
return ctx -> activity.run(new DefaultWorkflowActivityContext(ctx, activity.getClass()));
5555
}
5656
}

sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowClassWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ public TaskOrchestration create() {
5757
);
5858
}
5959

60-
workflow.run(new DefaultWorkflowContext(ctx));
60+
workflow.run(new DefaultWorkflowContext(ctx, workflow.getClass()));
6161
};
6262
}
6363
}

sdk-workflows/src/main/java/io/dapr/workflows/runtime/WorkflowInstanceWrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,6 @@ public String getName() {
3636

3737
@Override
3838
public TaskOrchestration create() {
39-
return ctx -> workflow.run(new DefaultWorkflowContext(ctx));
39+
return ctx -> workflow.run(new DefaultWorkflowContext(ctx, workflow.getClass()));
4040
}
4141
}

sdk-workflows/src/test/java/io/dapr/workflows/DefaultWorkflowContextTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@
4242

4343
public class DefaultWorkflowContextTest {
4444
private DefaultWorkflowContext context;
45+
private DefaultWorkflowContext contextWithClass;
4546
private TaskOrchestrationContext mockInnerContext;
4647
private WorkflowContext testWorkflowContext;
4748

4849
@BeforeEach
4950
public void setUp() {
5051
mockInnerContext = mock(TaskOrchestrationContext.class);
51-
context = new DefaultWorkflowContext(mockInnerContext);
5252
testWorkflowContext = new WorkflowContext() {
5353
@Override
5454
public Logger getLogger() {
@@ -141,6 +141,8 @@ public void setCustomStatus(Object status) {
141141

142142
}
143143
};
144+
context = new DefaultWorkflowContext(mockInnerContext);
145+
contextWithClass = new DefaultWorkflowContext(mockInnerContext, testWorkflowContext.getClass());
144146
}
145147

146148
@Test

sdk-workflows/src/test/java/io/dapr/workflows/runtime/DefaultWorkflowActivityContextTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.dapr.durabletask.TaskActivityContext;
44
import org.junit.jupiter.api.DisplayName;
55
import org.junit.jupiter.api.Test;
6+
import org.slf4j.Logger;
67

78
import static org.junit.jupiter.api.Assertions.*;
89
import static org.mockito.ArgumentMatchers.any;
@@ -34,7 +35,7 @@ void shouldSuccessfullyCreateContextAndReturnCorrectValuesForAllMethods() {
3435
@DisplayName("Should throw IllegalArgumentException when context parameter is null")
3536
void shouldThrowIllegalArgumentExceptionWhenContextParameterIsNull() {
3637
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
37-
new DefaultWorkflowActivityContext(null);
38+
new DefaultWorkflowActivityContext(null, TaskActivityContext.class);
3839
});
3940
assertEquals("Context cannot be null", exception.getMessage());
4041
}
@@ -45,7 +46,7 @@ void shouldThrowIllegalArgumentExceptionWhenLoggerParameterIsNull() {
4546
TaskActivityContext mockInnerContext = mock(TaskActivityContext.class);
4647

4748
IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> {
48-
new DefaultWorkflowActivityContext(mockInnerContext, null);
49+
new DefaultWorkflowActivityContext(mockInnerContext, (Logger) null);
4950
});
5051
assertEquals("Logger cannot be null", exception.getMessage());
5152
}

0 commit comments

Comments
 (0)