Skip to content

Commit

Permalink
Add logging MDC
Browse files Browse the repository at this point in the history
  • Loading branch information
stephanpelikan committed Aug 27, 2024
1 parent 071f09c commit cf13ee7
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 0 deletions.
23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class TaxiApplication {

1. [Worker ID](#worker-id)
1. [Workflow modules](#workflow-modules)
2. [Logging](#logging)
1. [Spring boot profiles](#spring-boot-profiles)
1. [Migrating from one BPM system to another](#migrating-from-one-bpm-system-to-another)
1. [Noteworthy & Contributors](#noteworthy--contributors)
Expand Down Expand Up @@ -129,6 +130,28 @@ Therefore, on migrating workflows from one engine to another, the BPMN files hav
according to new engine used. Having this in mind one should also define a resources location specific to the current engine used,
even if there are no plans to migrate to any other engine yet (see sample above).

## Logging

All VanillaBP adapter implementations use `Slf4J` as a logging-framework wrapper. According to the current
action (running a method annotated by `@WorkflowTask` or a method of `ProcessService` called) MDCs are setting
using the keys defined in class `io.vanillabp.springboot.adapter.LoggingContext`:

* The current workflow module's ID.
* The current VanillaBP adapter's ID.
* The current aggregate's ID - may be null if not yet filled by the database for auto-increments.
* The current workflow's BPMN process ID ("id" attribute of BPMN "process" tag)
regardless whether the current action belongs to a call-activity's BPMN task. Secondary BPMN process IDs
are not available for logging (see [Call-activities](https://github.com/vanillabp/spi-for-java#call-activities))</a>.
* The current workflow's ID, specific to underlying BPM system (aka process instance ID) - if already known by the adapter.
* The current workflow task's ID.
* The current workflow task's BPMN node ("id" attribute of the BPMN XML tag in combination with the BPMN process ID the task belongs to - e.g. "MyProcess#MyTask").
* The current workflow task's BPMN node ID (aka flow node instance ID).

However, if those values
are needed without using `Slf4J` then you can use the `LoggingContext`'s static class methods to retrieve them.

*Hint:* Adapters may add additional logging context. Checkout their README.md for details.

## Spring boot profiles

Typically, profiles are used to set environment specific properties (e.g. stages):
Expand Down
129 changes: 129 additions & 0 deletions src/main/java/io/vanillabp/springboot/adapter/LoggingContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package io.vanillabp.springboot.adapter;

import java.util.HashMap;
import java.util.Map;

public abstract class LoggingContext {

/**
* The current workflow module's ID.
*/
public static final String WORKFLOW_MODULE_ID = "workflowModuleId";

/**
* The current VanillaBP adapter's ID.
*/
public static final String WORKFLOW_ADAPTER_ID = "workflowAdapterId";

/**
* The current aggregate's ID - may be null if not yet filled by the database for auto-increments.
*/
public static final String WORKFLOW_AGGREGATE_ID = "workflowAggregateId";

/**
* The current workflow's BPMN process ID (&quot;id&quot; attribute of BPMN &quot;process&quot; tag)
* regardless whether the current action belongs to a call-activity's BPMN task. Secondary BPMN process IDs
* are not available for logging.
*
* #see <a href="https://github.com/vanillabp/spi-for-java#call-activities">Call-activities</a>
*/
public static final String WORKFLOW_BPMN_ID = "workflowBpmnId";

/**
* The current workflow's ID, specific to underlying BPM system (aka process instance ID)
* - if already known by the adapter.
*/
public static final String WORKFLOW_BPM_ID = "workflowBpmId";

/**
* The current workflow task's ID.
*/
public static final String WORKFLOW_TASK_ID = "workflowTaskId";

/**
* The current workflow task's BPMN node (&quot;id&quot; attribute of the BPMN XML tag in combination with
* the BPMN process ID the task belongs to - e.g. &quot;MyProcess#MyTask&quot;).
*/
public static final String WORKFLOW_TASK_NODE = "workflowTaskNode";

/**
* The current workflow task's BPMN node ID (aka flow node instance ID).
*/
public static final String WORKFLOW_TASK_NODE_ID = "workflowTaskNodeId";


private static ThreadLocal<Map<String, Object>> context = ThreadLocal.withInitial(HashMap::new);

/**
* @return Immutable context
*/
public static Map<String, Object> getContext() {
return Map.copyOf(context.get());
}

protected static void clearContext() {
context.get().clear();
}

protected static Map<String, Object> getWriteableContext() {
return context.get();
}

/**
* @see LoggingContext#WORKFLOW_AGGREGATE_ID
*/
public static String getWorkflowAggregateId() {
return (String) context.get().get(WORKFLOW_AGGREGATE_ID);
}

/**
* @see LoggingContext#WORKFLOW_BPMN_ID
*/
public static String getWorkflowBpmnId() {
return (String) context.get().get(WORKFLOW_BPMN_ID);
}

/**
* @see LoggingContext#WORKFLOW_BPM_ID
*/
public static String getWorkflowBpmId() {
return (String) context.get().get(WORKFLOW_BPM_ID);
}

/**
* @see LoggingContext#WORKFLOW_MODULE_ID
*/
public static String getWorkflowModuleId() {
return (String) context.get().get(WORKFLOW_MODULE_ID);
}

/**
* @see LoggingContext#WORKFLOW_ADAPTER_ID
*/
public static String getWorkflowAdapterId() {
return (String) context.get().get(WORKFLOW_ADAPTER_ID);
}

/**
* @see LoggingContext#WORKFLOW_TASK_ID
*/
public static String getWorkflowTaskId() {
return (String) context.get().get(WORKFLOW_TASK_ID);
}

/**
* @see LoggingContext#WORKFLOW_TASK_NODE
*/
public static String getWorkflowTaskNode() {
return (String) context.get().get(WORKFLOW_TASK_NODE);
}

/**
* @see LoggingContext#WORKFLOW_TASK_NODE_ID
*/
public static String getWorkflowTaskNodeId() {
return (String) context.get().get(WORKFLOW_TASK_NODE_ID);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ProcessServiceImplementation<DE> extends ProcessService<DE> {

void setParent(AdapterAwareProcessService<DE> parent);

String getPrimaryBpmnProcessId();

}

0 comments on commit cf13ee7

Please sign in to comment.