Logging data change operations

Use data change Gosu APIs to configure logging within data change code. These APIs generate logging information that users can see in human-readable output in data change user interface.

Logging entity field-level changes

To log entity field-level changes, call DataChange.util.setDetailResultWriting(bundle). The logging information includes information about added objects, deleted objects, and field-level changes on every object. For updated properties, the logging information includes each field value before the change and after the change.

Logging arbitrary text data

To log arbitrary text data, call DataChange.util.ResultsWriter. That property returns an appender, which is an object that implements the interface java.lang.Appendable. That object has several method signatures of the append method. The simplest method signature takes a CharSequence object, such as a standard String object.

Example logging code

The following sample code uses the setDetailResultWriting method and the ResultsWriter property to create log messages.

gw.transaction.Transaction.runWithNewBundle(\ bundle -> {

   // For demonstration, get a User object and make minor data change to the first name
  var u = gw.api.database.Query.make(User).select().first()
  bundle.add(u)
  u.Contact.FirstName = u.Contact.FirstName + "SUFFIX"

   // Determine what you want to write to the data change log
  var msg = "For PublicID '${u.PublicID}' User.DisplayName is now '${u.DisplayName}'!"

   // To log arbitrary text in Data Change UI, get a results writer (type is java.lang.Appender)
  var rw = DataChange.util.ResultsWriter
  rw.append("Add arbitrary log message here\n")

   // enable detailed logging of each property value before and after our change
  DataChange.util.setDetailResultWriting(bundle)

   // for testing in Studio Scratchpad, also print to standard console
  // print("To console: " + msg)

 })

To test and debug your code in Studio Scratchpad, you may want to print to the console using the standard print statement. Also, add one more argument to the runWithNewBundle method to represent a user name. For example, pass the String value "su" to create your writable bundle as the super user.