AEM Audit Log API

Photo by Lukas on Pexels.com

Adobe Experience Manager (AEM) provides an audit log, where different types of events are registered. For instance you can find the users who downloaded assets from the DAM, or users that modified content.

There are situations when you want to add entries to the log, or query the log programmatically, and Adobe provides an API for that.

API

AEM provides the com.day.cq.audit.AuditLog class that allows querying and adding new log events to the audit log.

Adding Events

The code below uses OSGI annotations for importing the AuditLog Service and add a new DAM download event to the log.

 … 
 @Reference
 private AuditLog auditLog;
 
 public void myMethod(){
   …
   AuditLogEntry entry = new AuditLogEntry(DamEvent.EVENT_TOPIC, new Date(), user, assetPath,Type.DOWNLOADED.toString(), null);
   auditLog.add(entry);
   …
 } 

Querying the Log

The AuditLog API also provides helper methods to query getLatestEvent and getLatestEvents that allow you to specify the event type, category, path and the maximum number of events. 

API vs Repository Query

You obviously can query the repository using the resource resolver and find the events on /var/audit and manually parse that information in your Java code. There is a big disadvantage however:  Adobe has changed the location where it stores the audit log in the past, and can change the audit log structure in the future too. 

By using their API, you will more likely have less issues upgrading in the future. If Adobe decides to make any change, they will either update the implementation, or update the API. In the long run it will reduce the amount of work you will have in the future to upgrade your AEM instances.

Leave a Reply