ColdFusion 8: Dumping gets even better

If you ask most developers, I'd be willing to bet that they would say their favorite ColdFusion tag is cfdump. Without a doubt, cfdump is a developers dream. It can take any variable and display the values. ColdFusion 8 added some new features to this tag to make it even more powerful. Read on for details...

First off - have you ever dumped a query that had a lot of columns? This can make it a bit harder to find the data you are looking for. In ColdFusion 8, you can now use a show or hide attribute to tell ColdFusion what to show - or hide. (I bet you could have guessed that.) This works for both queries and structures. Here is a simple example:

<cfdump var="#myquery#" show="id,name">

If my query had 4 columns: id, name, age, gender, then this dump would only show the ID and name values. I could get the same result like so:

<cfdump var="#myquery#" hide="age,gender">

My old fdump custom tag did this, so I'm going to take some credit for these guys. ;) ColdFusion will even tell you that it has filtered the results, which is handy in case your memory is a bit like mine.

Related to this is the new keys attribute. This one lets you filter a dump by the number of keys. Since you have no control over what keys are picked, I'm not quite sure why someone would use this, but if you want, it is there. Consider:

<cfset s = {a=1,b=2,c=3,d=4,e=5,f=6}>
<cfdump var="#s#" keys="3">

The first line creates a structure with 6 keys using the new shorthand method. Then I dump the first 3 keys. Again - I'm not quite sure I'd use this often, but it is nice to have it there.

Another change is the showUDFs attribute. I'm not going to demo this one as it is so simple. If you are dumping an object that contains UDFs, you can set showUDFs to false to hide them. I can see this being kind of useful. Normally you want to see data and variables, not methods, except I dump CFCs all the time just for that reason.

Now for my favorite new feature. Have you ever needed to dump something and save the dump? You can wrap it in a mail and send the value, or wrap it in cfsavecontent and save it to a file, but ColdFusion 8 gives you new options.

There is now an output attribute for cfdump. The options for this attribute are:

  • browser - This is the default
  • console - Sends the dump to your console
  • filename - If the value is anything but browser or console, ColdFusion will assume it is a file name.

Let's talk more about the filename value. First - it has to be a full path. (I wish ColdFusion would allow for relative paths for all file based operations.) Using a filename will create a much slimmer, text based dump of the data. Consider:

<cfset foo = queryNew("id,name,age,gender")>
<cfset queryAddRow(foo)>
<cfset querySetCell(foo,"id",1)>
<cfset querySetCell(foo,"name","Ray")>
<cfset querySetCell(foo,"age",33)>
<cfset querySetCell(foo,"gender","male")>
<cfset queryAddRow(foo)>
<cfset querySetCell(foo,"id",2,2)>
<cfset querySetCell(foo,"name","Jacob",2)>
<cfset querySetCell(foo,"age",7,2)>
<cfset querySetCell(foo,"gender","male",2)>

<cfdump var="#foo#" output="#expandPath('./dump.txt')#">

This creates the following:

query


[Record # 1]
AGE: 33
GENDER: male
ID: 1
NAME: Ray

[Record # 2]
AGE: 7
GENDER: male
ID: 2
NAME: Jacob

************************************************************************************

Pretty handy!

Comments

huh... huh...
Ray said "dump"
huh... huh...
# Posted By Beavis & Butthead | 6/1/07 8:55 AM
How dare you. Do you think I was trying to be silly with my title?

Oh wait.... I was. ;)
# Posted By Raymond Camden | 6/1/07 8:59 AM
Ray, sorry I can't look this up myself, but I can't seem to find CF8 reference docs anywhere. What's meant by the "console" as an output for cfdump?
# Posted By Tom Mollerus | 6/1/07 9:03 AM
If you run CF as a service, then the console is a log file. But you can also run CF from the command line (console), and if you do, it will get output there.
# Posted By Raymond Camden | 6/1/07 9:12 AM
Definitely a step in the right direction, but I sure would like to see cfscript support added to cfdump, and all the tags for that matter. Well we can always wrap them ...
# Posted By Matt Turner | 6/1/07 9:21 AM
I could see a use for the keys="3" attribute for when you are using a struct as an index into something else, like a query. In cases where you don't know what the keys will be, then you can't specify which ones you want to see, but you might want to see just a few to ensure that you don't have some really dumb bug in your code.

For example:

<cfquery datasource="dsn" name="People">
SELECT FirstName, LastName, SSN, omg, wtf, bbq, foo, bar
FROM People
</cfquery>
<cfset PeopleIndex=StructNew()>
<cfloop query="People">
<cfset PeopleIndex[SSN]=CurrentRow>
</cfloop>
<cfdump var="#PeopleIndex#" keys="5">

If I had made some really bad mistake in my loop, I wouldn't know what the keys were to look for, but if I had 1000 people in my query, I also wouldn't want to dump the whole thing out. I just want to see enough to know that it looks generally correct.

(And before someone says that you can do this with a QoQ -- yes, you could, but this is faster.)
# Posted By Rick Osborne | 6/1/07 9:21 AM
CFDump is awesome. And now, it is even awesomer :) I love the query column selection and the file output.
# Posted By Ben Nadel | 6/1/07 10:03 AM
for console, it'll write it to C:\ColdFusion8\runtime\logs\coldfusion-out.log, assuming c:\coldfusion8 is your coldfusion root.

it doesn't appear that this file is readable in the new CF Log Viewer panel through eclipse, though.
# Posted By Marc Esher | 6/1/07 10:15 AM
It isn't a "standard" cf log Marc. By "standard" I mean CSV. It is more a dump type log if that makes sense.
# Posted By Raymond Camden | 6/1/07 10:31 AM
Too bad they didn't add an option to not expand nested object/cfc references. Many frameworks reference other objects, which in turn reference still other objects like a factory which may contain references to cached objects. Try dumping one of those.

And not to mention circular references where object A references object B which has a reference to object A.
# Posted By Michael Long | 6/1/07 11:55 AM
Michael - you do know there is an EXPAND attribute, right? It has been in there since cf6 I think.
# Posted By Raymond Camden | 6/1/07 12:01 PM
Unless I'm wrong, setting expand=no only hides the data, yes? All of the nested data is still generated and downloaded, waiting to be expanded manually using the javascript functionality.

As such, doing a cfdump on, say, a reactor framework object will still result in downloading half the framework.
# Posted By Michael Long | 6/1/07 12:15 PM
True dat. Sorry I misread you. Well, a dump is a dump. :)
# Posted By Raymond Camden | 6/1/07 1:32 PM
freakin wicked!! I'm diggin the show and hide attribute, thats fo sho! And as far as dumping into a file, damn I could have used that plenty of times, on previous projects using the reactor ORM.
# Posted By sal | 6/3/07 1:00 PM
I think we have more ways to dump debugging info than we can poke a stick at :P I did a quick post this morning on dumping JS debug info using the new Ajax Logger:
http://www.madfellas.com/blog/index.cfm/2007/6/3/C...

It's dump'o'rific :)
# Posted By Justin Carter | 6/3/07 8:34 PM
Ray, there is a way to view the live console output even when running CF as a service. Co-incidently I recently blogged about this here: http://blog.mrbuzzy.biz/?p=8
Cheers.
# Posted By MrBuzzy | 6/4/07 2:14 AM
Nicde tip there, MyBuzzy!
# Posted By Raymond Camden | 6/4/07 6:46 AM
Any chance they got around to fixing it to be XHTML compliant? It's really annoying when you use it on a page that has an XHTML strict doctype and all the styles disappear as a result.
# Posted By Mary Jo | 6/8/07 9:02 AM