In commercial production web applications, you frequently find yourself wanting metadata about any actions that update your database. Typically this includes things like which user created each record, when the record was created, when it was last modified, etc. At first this starts out simply just by adding columns to a table or two. As developent progresses, our experience has been that you start wanting this functionality in more and more places. If you keep this up you will eventually have time stamps and user ID foreign keys scattered all over your database.

So we thought we would solve this problem once and for all with a centralized action log snaplet. The snaplet-actionlog package leverages snaplet-persistent to define a database table holding this information. It also defines wrappers around the core database CRUD operations that handle the logging for you. To use this snaplet, just add it to your app and instead of calling persistent’s insert function, call the snaplet’s loggedInsert function everywhere you want an insert action to be logged.

In addition to this basic metadata, the actionlog snaplet also deltas when database rows are updated. This gives you audit capability on who made what changes to anything in your database. It is implemented with a CanDelta type class that lets you define which columns you want diffs for on a per-table basis. If you don’t want deltas for a particular table, you can have CanDelta’s deltaFields function return an empty list.

The snaplet also includes templates with markup for displaying and filtering action log data. You can use this two ways. Either link to the standalone index.tpl and show.tpl pages on your own. Or, if you need more customization, you can include actionlog functionality in your existing templates by calling the partial page templates _details.tpl, _list.tpl, and _form.tpl templates with the apply tag.

All of the included templates are designed to work with Bootstrap CSS. We don’t include that CSS because in our applications where we might use actionlog we already have Bootstrap included. If you don’t want Bootstrap or want something completely different from the built-in markup, the snaplet system allows you to change that as you see fit. Snaplets allow actionlog to work and look decent immediately out of the box with no modification, but also gives you unlimited flexibility and customization–the best of all worlds.

We hope this will provide a good example of how snaplets can be used to publish self-contained chunks of reusable web functionality, and inspire more open-source activity.