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!

Archived Comments

Comment 1 by Beavis & Butthead posted on 6/1/2007 at 5:55 PM

huh... huh...
Ray said "dump"
huh... huh...

Comment 2 by Raymond Camden posted on 6/1/2007 at 5:59 PM

How dare you. Do you think I was trying to be silly with my title?

Oh wait.... I was. ;)

Comment 3 by Tom Mollerus posted on 6/1/2007 at 6:03 PM

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?

Comment 4 by Raymond Camden posted on 6/1/2007 at 6:12 PM

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.

Comment 5 by Matt Turner posted on 6/1/2007 at 6:21 PM

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 ...

Comment 6 by Rick Osborne posted on 6/1/2007 at 6:21 PM

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.)

Comment 7 by Ben Nadel posted on 6/1/2007 at 7:03 PM

CFDump is awesome. And now, it is even awesomer :) I love the query column selection and the file output.

Comment 8 by Marc Esher posted on 6/1/2007 at 7:15 PM

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.

Comment 9 by Raymond Camden posted on 6/1/2007 at 7:31 PM

It isn't a "standard" cf log Marc. By "standard" I mean CSV. It is more a dump type log if that makes sense.

Comment 10 by Michael Long posted on 6/1/2007 at 8:55 PM

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.

Comment 11 by Raymond Camden posted on 6/1/2007 at 9:01 PM

Michael - you do know there is an EXPAND attribute, right? It has been in there since cf6 I think.

Comment 12 by Michael Long posted on 6/1/2007 at 9:15 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.

Comment 13 by Raymond Camden posted on 6/1/2007 at 10:32 PM

True dat. Sorry I misread you. Well, a dump is a dump. :)

Comment 14 by sal posted on 6/3/2007 at 10:00 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.

Comment 15 by Justin Carter posted on 6/4/2007 at 5:34 AM

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/bl...

It's dump'o'rific :)

Comment 16 by MrBuzzy posted on 6/4/2007 at 11:14 AM

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.

Comment 17 by Raymond Camden posted on 6/4/2007 at 3:46 PM

Nicde tip there, MyBuzzy!

Comment 18 by Mary Jo posted on 6/8/2007 at 6:02 PM

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.

Comment 19 by Paul Dynan posted on 9/14/2009 at 10:47 PM

Is there a way to put the results of a CFDump into a variable? I want to insert the contents into a database (building on the error handling I was asking about last week). I'd like to keep the formatting that comes in CFDump when we retrieve the results later.

Comment 20 by Paul Dynan posted on 9/14/2009 at 11:25 PM

Never mind...looks like my idea light is florecent, and it needed to flicker on:

<CFSAVECONTENT VARIABLE="varApplication">
<CFDUMP VAR="#Application#" LABEL="Application">
</CFSAVECONTENT>

Comment 21 by Raymond Camden posted on 9/14/2009 at 11:43 PM

Just be warned - when you cfdump, a request variable is used to determine the CSS should be output. The 2nd-N time you run cfdump then the CSS is not used. In order to ensure you wrap the CSS, you would need to check for the existence of the Request variable and struct delete it before running the cfdump.

Comment 22 by Paul Dynan posted on 9/14/2009 at 11:59 PM

It seemed to work fine in my initial tests.

I used the above to put the results in a SQL text column using CFQueryParam. I then do a simple SELECT query, and output using
<CFOUTPUT>#myQuery.colApplication#</CFOUTPUT>.

The results come out looking identical to original CFDump, and still collapse on click, too.

Comment 23 by Raymond Camden posted on 9/15/2009 at 12:01 AM

Did you test with a cfdump somewhere earlier in the request, _before_ the one you wrap/save?

Comment 24 by Paul Dynan posted on 9/15/2009 at 12:09 AM

I just tested, and it still seems fine. I do the inserts on my site-wide error page. I have debugging off, no outptu dumps, and nothing other than a text message on the screen ("Sorry for the error...").

On a separate page, in a separate browser, I used the select and output, and it works great.

Comment 25 by Raymond Camden posted on 9/15/2009 at 12:12 AM

Ah, maybe they changed it to always dump the CSS.

Tested - yep. What's odd is that they still add the request variable, cfdumpinited, but they don't use it. I"ll file a bug report on it.

Comment 26 by Paul Dynan posted on 9/15/2009 at 12:16 AM

As long as the bug fix doesn't fix how great it's working now ;)

Btw, all the of alerts I've gotten from this post today come back as "Delievery Failure" ("Reason: Recipient spam or content filter rejected the message").

Comment 27 by Raymond Camden posted on 9/15/2009 at 12:17 AM

BlogCFC does comment emails by sending TO everyone who subscribed FROM the subscriber. Someone here has a bad email address (this is an old entry) so that's why you get the bounce backs.