XQuery/Using Triggers to Log Events

Motivation
You want to log all changes in files of a single collection.

Method
We will create a trigger that logs these events. The trigger will append a string to a log file.

There are six trigger event types:
 * store: Fired when a document is created in the collection or sub-collection
 * update: Fired when a document is updated in the collection or sub-collection
 * remove: Fired when a document is deleted from the collection or sub-collection
 * create: Fired when a sub-collection is created
 * rename: Fired when a sub-collection is renamed
 * delete: Fired when a sub-collection is deleted

Sample Code
 NOTE: These examples do not reliably work on eXist 1.4! I have not yet tested on 2.0.

In this example we will be logging all store, update and remove events from the collection /db/my-collection

Here is a sample trigger configuration file. This file is placed in the /db/system/config are with the same db path added to it that you want to monitor:

/db/system/config/db/my-collection

Here is what the trigger file looks like:

Note that the three trigger operations (store, update, remove) are listed in the event attribute and separated by commas. When these operations are fired the XQuery /db/triggers/log-changes.xq gets run. You can pass parameters to this query using the parameter element.