Skip to main content

Logging Custom Audit Instances

Create an Audit Instance

To log custom information into the system audit log, you must first obtain an AuditService instance. AuditService can be autowired as a Spring bean defined in com.flowable.autoconfigure.audit.AuditEngineServicesAutoConfiguration. Another possibility is to obtain the auditService from the command context in a custom service task. For example:

protected AuditService getAuditService() {
AuditEngineConfigurationApi engineConfigurationApi = (AuditEngineConfigurationApi) Context.getCommandContext()
.getEngineConfigurations().get(AuditEngineConfiguration.KEY_AUDIT_ENGINE_CONFIG);
if (engineConfigurationApi == null) {
throw new FlowableException("Audit engine is not configured");
}
return engineConfigurationApi.getAuditService();
}

The auditService provides a builder to create an AuditInstance. Created AuditInstances are immutable. AuditlogService, which performs audit instance logging of Adding Additional Auditing Information, is an example of using the builder:

public class AuditLogService extends AbstractPlatformTask {

@Override
public void executeTask(VariableContainer variableContainer, ExtensionElementsContainer extensionElementsContainer) {
AuditInstanceBuilder auditInstanceBuilder = getAuditService().createAuditInstanceBuilder();
auditInstanceBuilder.
// variable container attributes
tenantId(VariableContainerUtil.getTenantId(variableContainer)).
// extension element attributes
externalId(getExternalId(variableContainer, extensionElementsContainer)).
creatorId(getCreatorId(variableContainer, extensionElementsContainer)).
type(getType(variableContainer, extensionElementsContainer)).
subType(getSubType(variableContainer, extensionElementsContainer)).
payload(getPayload(variableContainer, extensionElementsContainer));
setScopes(auditInstanceBuilder, variableContainer, extensionElementsContainer);
auditInstanceBuilder.create();
}

The created auditInstance is stored in the ${prefix}flw_audit_instance table.

Query for Audit Instances

The AuditInstanceQuery class is provided by the createAuditInstanceQuery method in the AuditService class. The available query options are: singleResult, list and count. For example, to query for all audit items of given process instance:

List<AuditInstance> auditInstances = auditEngineConfiguration.getAuditService().createAuditInstanceQuery().
scopeId(processInstanceId).
scopeType(ScopeTypes.BPMN)
list();

which is transformed into the following query:

SELECT *
FROM {prefix}flw_audit_instance
WHERE scope_id_ = '{processInstanceId}' AND scope_type_ = 'bpmn'
ORDER BY creation_time_ DESC;