diff --git a/README.md b/README.md index 461b708..52a7a18 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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)). +* 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): diff --git a/src/main/java/io/vanillabp/springboot/adapter/LoggingContext.java b/src/main/java/io/vanillabp/springboot/adapter/LoggingContext.java new file mode 100644 index 0000000..6cac4d8 --- /dev/null +++ b/src/main/java/io/vanillabp/springboot/adapter/LoggingContext.java @@ -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 ("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 + */ + 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 ("id" attribute of the BPMN XML tag in combination with + * the BPMN process ID the task belongs to - e.g. "MyProcess#MyTask"). + */ + 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> context = ThreadLocal.withInitial(HashMap::new); + + /** + * @return Immutable context + */ + public static Map getContext() { + return Map.copyOf(context.get()); + } + + protected static void clearContext() { + context.get().clear(); + } + + protected static Map 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); + } + + +} diff --git a/src/main/java/io/vanillabp/springboot/adapter/ProcessServiceImplementation.java b/src/main/java/io/vanillabp/springboot/adapter/ProcessServiceImplementation.java index 44ac3f7..7f310ae 100644 --- a/src/main/java/io/vanillabp/springboot/adapter/ProcessServiceImplementation.java +++ b/src/main/java/io/vanillabp/springboot/adapter/ProcessServiceImplementation.java @@ -11,4 +11,6 @@ public interface ProcessServiceImplementation extends ProcessService { void setParent(AdapterAwareProcessService parent); + String getPrimaryBpmnProcessId(); + }