Quick Tip - CFLOG and JSON

This post is more than 2 years old.

As my friends will tell me, I'm something of a cflog fiend. I use logging as my primary debugging tool. It's not perfect, but I find it to be especially effective in Model-Glue applications where one request bounces all over the place from controllers to model files to views. The only problem with cflog though is that it can only log text, not complex data. You can always switch to cfdump for that - don't forget that ColdFusion 8 added the ability to dump to a file. While this works, I prefer the "slimness" of a line of text. So when I need to log some complex data, I just use JSON. For example:

var userData = maf.login(arguments.username, arguments.password); writeLog(file='picard',text='login good for #arguments.username#'); writeLog(file='picard',text=serializeJSON(userData)); return userData;
Raymond Camden's Picture

About Raymond Camden

Raymond is a senior developer evangelist for Adobe. He focuses on document services, JavaScript, and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Archived Comments

Comment 1 by JC posted on 11/20/2009 at 1:33 AM

probably useless, but wouldn't wddx let you log complex data?

Comment 2 by Raymond Camden posted on 11/20/2009 at 4:26 AM

I find JSON easier to read than WDDX.

Comment 3 by Ben Nadel posted on 11/21/2009 at 1:13 AM

Now that CFDump can be written to a file, I feel like it's functionality is complete :) I think it even appends to files (during the dump process).

The one thing that I never liked about the CFLog (and I may be off base here) is that you could not specify a location for the log file, only the name, right?

Comment 4 by Raymond Camden posted on 11/21/2009 at 1:34 AM

Yep, cflog only logs to files within the CF logs folder. Obviously you can use cffile instead.

Comment 5 by Budd Wright posted on 11/21/2009 at 9:43 AM

This might be slightly off topic, so I apologize ahead of time...

While I write to the CF logs, I sometimes opt for CFDump because of one reason: CF Log cleanup... I have the hardest time deleting log files through CF Administrator. Click delete, say OK, and get back the Log Files screen showing my log file still there, undeleted! A lot of times, during development, I want to start fresh with the log files after doing something that caused a lot of entries, and the server seems to lock the files and prevent deletion of the log files. (Yes, you can shut down the CF service and delete the files, but that's a worse pain.)

Often times, I've figured out that if you just sit there and continuously click the delete button, eventually you can get lucky and have it deleted, but this is a problem I've noticed from CF7 all the way to CF9!

Any thoughts on this, Ray?

Comment 6 by Raymond Camden posted on 11/21/2009 at 6:58 PM

I've seen that too. :( It seems mainly to be an issue with the default logs - so I'm wondering if its just hard to get a clean lock on the file to perform the delete.

Comment 7 by Brian H. posted on 11/21/2009 at 9:16 PM

I am surprised Raymond. You are the maintainer of Coldfire Firefox plugin. I would have expected that you use <cftrace/> to debug text/complex data with ColdFire? True that you don't get the disk logging that you can review at a later date, but being able to sprinkle in <cftrace var="somevar"/> and then see it expanded at the bottom of my screen is awfully nice.

Cheers!

Comment 8 by Raymond Camden posted on 11/21/2009 at 9:22 PM

I was one of the two creators of ColdFire, but Nathan Mische is now the maintainer.

Nothing at all wrong with cftrace - I'm just digging cflog more now. ;)

Comment 9 by Don posted on 2/2/2010 at 9:55 AM

on an only slightly related issue. I have found that in CF8 if you use trace in a cfc which is called via Ajax it throws an exception "Debugger not found" I don't know if this is a known issue or if I have configured something incorrectly.

Comment 10 by Raymond Camden posted on 2/2/2010 at 5:11 PM

Have you tried it with CF9? I'd suggest doing so - and if you replicate it - file a bug report.

Comment 11 by Don posted on 2/3/2010 at 8:24 AM

Tested trace in cf9, no error thrown but trace is ignored and not written to trace.log. writelog appears to work fine.

Comment 12 by Don posted on 2/3/2010 at 8:57 AM

Spoke too soon. If you have "enable request debugging on" in cfadmin the ajax call fails when you have a trace even if inline is false. If you have enable request debugging off the ajax call works but the trace is not written to the tracelog.

Comment 13 by Don posted on 2/3/2010 at 9:53 AM

I have explained this in a bit more detail: http://blog.vawterconsultin...

Comment 14 by Ramakrishna posted on 2/11/2014 at 3:21 PM

Hi,

I am facing a problem in log file, in our application. Logs are deleting itself, is there any size constraint for log files. The deleted logs were important for tracing a issue, is there any way to get back logs which are deleted

Comment 15 by Raymond Camden posted on 2/11/2014 at 9:22 PM

There is a feature in CF to delete logs. See the logging settings in the admin.

Comment 16 by Misty posted on 3/2/2014 at 11:33 AM

Hi ray, I am using CFLOG and Coldfsuion10, I want to Log the logs in my external file specified by me the location.

It is unable to doing it, How can i do that, I am using the file attribute to it and using expandpath to locate the folder and log the issues there, but it is not doing that

Comment 17 by Raymond Camden posted on 3/2/2014 at 8:56 PM

CFLOG only logs to the main CF logs directory. If you want similar behavior outside of that dir, you need to use cffile.