Searching for Audit Log Entries
SAP Commerce provides a dedicated service that allows you to search for audit log entries for a particular type using specific search rules (even in a JSON audit payload).
An audit log is not backed by the SAP Commerce type system. As a result, it isn't possible to use ModelService or FlexibleSearch to obtain the entries.
The service is configured in Spring with the ID readAuditGateway, and has the following interface:
public interface ReadAuditGateway
{
<T extends AuditRecord> Stream<T> search(AuditSearchQuery searchQuery);
}
The interface returns a Stream of AuditRecord objects that is an object representation of an audit entry. An AuditSearchQuery object that is passed to the search method is the object that holds all the necessary information to perform the search query on an audit table. It allows you to search in standard type-related audit tables as well as in link-related audit tables.
The default implementation of audit is backed by JDBC. It is possible to implement a different persistence method since AuditSearchQuery only keeps information about the type to be searched together with a collection of SearchRule objects that define which fields in the audit table or which attributes (values) in the audit table must be searched.
Search Examples
The simplest query is a query for a type and a particular PK. To create such a query, first prepare an AuditSearchQuery object using some existing PK. In this example, we use User as a type:
// assuming User was found using UserService
final UserModel user = userService.getUserForUID("someUser");
// build the audit search query
final AuditSearchQuery query = AuditSearchQuery.forType("User").withPkSearchRules(user.getPk()).build();
// do search
final Stream<AuditRecord> records = readAuditGateway.search(query);
You can also search for more than one PK in one go:
// assuming User was found using UserService
final UserModel user1 = userService.getUserForUID("someUser-1");
final UserModel user2 = userService.getUserForUID("someUser-2");
final UserModel user3 = userService.getUserForUID("someUser-3");
// build the audit search query
final AuditSearchQuery query = AuditSearchQuery.forType("User").withPkSearchRules(user1.getPk(), user2.getPk(), user3.getPk()).build();
// do search
final Stream<AuditRecord> records = readAuditGateway.search(query);
Although it is possible to create a query that reads the entire audit table into memory, it is not recommended as it may cause out-of-memory errors. Such a dangerous query can look like this:
final Stream<AuditRecord> records = readAuditGateway.search(AuditSearchQuery.forType("User").build());
It is also possible to create a query that does a search within a JSON payload of an audit entry:
// assuming User was found using UserService
final UserModel user = userService.getUserForUID("someUser");
// build the audit search query
final AuditSearchQuery query = AuditSearchQuery.forType("User")
.withPkSearchRules(user.getPk())
.withPayloadSearchRule("someFieldInPayload", "value")
.withPayloadSearchRule("anotheFieldInPayload", "theOrherValue")
.build();
// do search
final Stream<AuditRecord> records = readAuditGateway.search(query);