Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
CFWheels Model Logger
This will let you log every time a user inserts or updates a record.
So you can later say:
Tim performed insert to the "25" record in the Page table via Pages' Edit a few hours ago, etc
Create a table called Logs with the following columns:
id int primary autoinc
userid int
modelid varchar
savetype varchar
model varchar
controller varchar
action varchar
createdat datetime
Add super.init(); to the init function of all Models that you want to log.
Then make sure your main Model.cfc and Controller.cfc look like the following files.
Credits: Thanks to Chris Peters for his harsh critique ;)
<cfscript>
component extends="Wheels"
{
function init()
{
filters(through="setLogInfo");
}
private function setLogInfo()
{
var loc = {};
loc.loguserid = "";
if(!isNull(session.user.id))
{
loc.loguserid = session.user.id;
}
request.logit =
{
userId = loc.loguserid,
controller = params.controller,
action = params.action,
modelid = "", // set later in Model.cfc
model = "", // set later in Model.cfc
savetype = "", // set later in Model.cfc
createdat = $convertToString(now())
};
}
}
</cfscript>
<cfscript>
component extends="Wheels"
{
function init()
{
afterCreate('logCreate');
afterUpdate('logUpdate');
afterDelete('logDelete');
}
private function logCreate()
{
logIt("Create");
}
private function logUpdate()
{
logIt("Update");
}
private function logDelete()
{
logIt("Delete");
}
private function logIt(savetype)
{
param name="this.id" default="";
var loc = {};
if (StructKeyExists(request, "logit") AND IsStruct(request.logit))
{
loc.thisModelName = ListLast(getMetaData(this).fullname,".");
loc.logit = request.logit;
loc.logit.model = loc.thisModelName;
loc.logit.modelid = this.id;
loc.logit.savetype = arguments.savetype;
// Can't use the Model().create function here because it'll throw an error
// gotta go vanilla (I'm using railo's query function, if your using ACF, use their nastier markup
query datasource="myds" {
writeoutput("INSERT INTO cats (userid, modelid, savetype, model, controller, action, createdat) VALUES (");
queryparam value="#loc.logit.userid#" cfsqltype="id";
writeoutput(",");
queryparam value="#loc.logit.modelid#" cfsqltype="varchar";
writeoutput(",");
queryparam value="#loc.logit.savetype#" cfsqltype="varchar";
writeoutput(",");
queryparam value="#loc.logit.model#" cfsqltype="varchar";
writeoutput(",");
queryparam value="#loc.logit.controller#" cfsqltype="varchar";
writeoutput(",");
queryparam value="#loc.logit.action#" cfsqltype="varchar";
writeoutput(",");
queryparam value="#loc.logit.createdat#" cfsqltype="varchar";
writeOutput(");");
}
}
}
}
</cfscript>
@ellor1138

This comment has been minimized.

Copy link

@ellor1138 ellor1138 commented Feb 4, 2015

Nicely done! I also added this bit of code to log what has changed in the model:
loc.logit.changes = SerializeJSON(this.allChanges());

@tapirus

This comment has been minimized.

Copy link

@tapirus tapirus commented Apr 10, 2015

Hi Tim,
I tried your model logger. Looks useful.
But it creates a log entry for every hasMany association of a changed model.
Is this desired behaviour?
In my case a change to my main model leads to 9 entries!
How to you use this?
Regards
tapirus

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment