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
huh... huh...
Ray said "dump"
huh... huh...
How dare you. Do you think I was trying to be silly with my title?
Oh wait.... I was. ;)
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?
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.
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 ...
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.)
CFDump is awesome. And now, it is even awesomer :) I love the query column selection and the file output.
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.
It isn't a "standard" cf log Marc. By "standard" I mean CSV. It is more a dump type log if that makes sense.
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.
Michael - you do know there is an EXPAND attribute, right? It has been in there since cf6 I think.
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.
True dat. Sorry I misread you. Well, a dump is a dump. :)
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.
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 :)
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.
Nicde tip there, MyBuzzy!
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.
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.
Never mind...looks like my idea light is florecent, and it needed to flicker on:
<CFSAVECONTENT VARIABLE="varApplication">
<CFDUMP VAR="#Application#" LABEL="Application">
</CFSAVECONTENT>
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.
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.
Did you test with a cfdump somewhere earlier in the request, _before_ the one you wrap/save?
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.
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.
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").
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.