Bump on a log
So for the last few posts, I’ve discussed logs. What to look for, how to sort through them etc. It’s overview material, I know, but given the forum, it’s the point that matters. Today I plan to discuss how the log can be modified to suit your own ColdFusion programming needs.
One of the many cool features of ColdFusion 7 and now ColdFusion 8 (I imagine, we’re working on 7 so I can’t say for sure on 8) is the addition of the application.cfc. Now for the most part, we could use the old application.cfm just as much, but the cfc version certainly makes it easier.
When an uncaught exception is thrown in ColdFusion MX 7, using the application.cfc, the program goes to the onError event. Here is a nice and tidy way of catching everything the programmer needs to know about what happened. The onError event is passed the exception, which is the struct of the error, much like the cfcatch struct. To get good use out of this information, I do two things; I log and I send myself an email with the dump. Then of course I display a pleasant message for the user to see, because who wants to see the big stack trace except the programmer and those who would like to do harm to you and your system. So, I log, I use cflog which is the built in logging utility.
<cflog file=”#this.name#” type=”error” text=”Event name: #arguments.eventName#” >
<cflog file=”#this.name#” type=”error” text=”Message: #arguments.exception.message# DETAIL: #arguments.exception.detail#”>
<cfif structkeyexists(arguments.exception,’TagContext’) and arraylen(arguments.exception.TagContext) gte 1 >
<cflog file=”#this.name#” type=”error” text=”Context: #arguments.exception.TagContext.template# LINE: #arguments.exception.TagContext.line#”>
<cflog file=”#This.Name#” type=”error”
text=”Root Cause Message: #Arguments.Exception.rootcause.message#”>
The file that then is written is actually a special log file that is made up of the application name. Since we have several websites on our server this makes it much easier than having to wade through the sometimes huge standard ColdFusion error logs. Then of course I pass through any information I might find useful in debugging. Once I write to the logs, I then dump out all the variables and the stack trace into a variable using cfsavecontent, and send it off in an html mail, to myself. This way I am notified immediately if there is an issue, rather than waiting for the Monday log check to take place.
Once all the error processings have taken place, I can use the onError to cflocate to a basic error page that just shows a nice message to the end user.