Listing Audit Entries (Logs) for a Node - Alfresco Content Services - 23.4 - 23.4 - Ready - Alfresco - external

Alfresco Content Services

Platform
Alfresco
Product
Alfresco Content Services
Release
23.4
License

Listing all the audit logs for a node uses the listAuditEntriesForNode method of the AuditApi.

For more information about this ReST API endpoint, see List Audit Entries (Logs) for a Node.

For a description of the common parameters, such as fields, see Common Parameters.

import org.alfresco.core.handler.AuditApi;
import org.alfresco.core.model.AuditEntryEntry;
import org.alfresco.core.model.AuditEntryPaging;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class ListNodeAuditLogsCmd {
    static final Logger LOGGER = LoggerFactory.getLogger(ListNodeAuditLogsCmd.class);

    @Autowired
    AuditApi auditApi;

    public void execute(String nodeId) throws IOException {
        Integer skipCount = 0;
        Integer maxItems = 100;
        String where = null;
        List<String> fields = null;
        List<String> include = new ArrayList<>();
        List<String> orderBy = null;

        // Include the log values
        include.add("values");

        LOGGER.info("Listing logs for node ID {}:", nodeId);
        AuditEntryPaging auditLogs = auditApi.listAuditEntriesForNode(
                nodeId, skipCount, orderBy, maxItems, where, include, fields).getBody();
        for (AuditEntryEntry auditAppEntry: auditLogs.getList().getEntries()) {
            String username = "N/A";
            if (auditAppEntry.getEntry().getCreatedByUser() != null) {
                username = auditAppEntry.getEntry().getCreatedByUser().getId();
            }
            String log = null;
            if (auditAppEntry.getEntry().getValues().toString().length() > 60) {
                log = auditAppEntry.getEntry().getValues().toString().substring(0, 60);
            } else {
                log = auditAppEntry.getEntry().getValues().toString();
            }
            LOGGER.info("  {} {} {} {}", auditAppEntry.getEntry().getId(), auditAppEntry.getEntry().getCreatedAt(),
                    username, log);
        }
    }
}

Note that you have to add values to the include parameter for the logs to include all the data. Also, the audit log value have been truncated for readability.

Executing this code will list the audit logs for passed in audit app id and audit entry id:

% java -jar target/rest-api-0.0.1-SNAPSHOT.jar list-audit-logs-node 37eedde2-3c78-4d25-bade-5360e22579f4

2021-05-10 09:27:33.714  INFO 51172 --- [           main] o.a.tutorial.restapi.RestApiApplication  : Started RestApiApplication in 4.069 seconds (JVM running for 4.724)
2021-05-10 09:27:33.716  INFO 51172 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[0]: list-audit-logs-node
2021-05-10 09:27:33.717  INFO 51172 --- [           main] o.a.tutorial.restapi.RestApiApplication  : args[1]: 37eedde2-3c78-4d25-bade-5360e22579f4
2021-05-10 09:27:33.717  INFO 51172 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       : Listing logs for node ID 37eedde2-3c78-4d25-bade-5360e22579f4:
2021-05-10 09:41:55.396  INFO 51505 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       :   76 2021-05-10T08:26:58.965Z admin {/alfresco-access/transaction/sub-actions=updateNodeProperti
2021-05-10 09:41:55.396  INFO 51505 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       :   77 2021-05-10T08:27:06.541Z admin {/alfresco-access/transaction/sub-actions=readContent, /alfr
2021-05-10 09:41:55.396  INFO 51505 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       :   78 2021-05-10T08:27:11.353Z admin {/alfresco-access/transaction/sub-actions=updateContent upda
2021-05-10 09:41:55.396  INFO 51505 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       :   80 2021-05-10T08:27:12.270Z admin {/alfresco-access/transaction/sub-actions=updateNodeProperti
2021-05-10 09:41:55.397  INFO 51505 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       :   81 2021-05-10T08:27:12.429Z admin {/alfresco-access/transaction/sub-actions=readContent, /alfr
2021-05-10 09:41:55.397  INFO 51505 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       :   83 2021-05-10T08:27:13.524Z admin {/alfresco-access/transaction/sub-actions=updateNodeProperti
2021-05-10 09:41:55.397  INFO 51505 --- [           main] o.a.t.restapi.ListNodeAuditLogsCmd       :   84 2021-05-10T08:27:13.609Z admin {/alfresco-acce