Logging with Coldfusion is very simple, but in and of itself a little limiting. Generally the log itself is a string. So while logs are certainly useful for recording an action and when it happened the amount of data that is presented in the log file is fairly restrictive without doing a whole lot of work, and even then your log file may look something like: “Error occured on page #thispage.cfm# by user #john smith#. The error catch message is #an error message#. It occurred on line #230#. I’ve wrapped the dynamic content with hashes. On top of all that we’re really not saying that much here. The thing is Coldfusion gives us a ton of data nicely organized as a struct, if only we could log it.
This is just another place where JSON becomes very handy for the developer. By serializing the struct as JSON we can log the struct as a string. If we need to see the struct we simply need to grab the JSON and deserialize it in which case the struct will be dumpable again. Also for debugging purposes we developers often email ourselves errors when they occur. This is a good practice, one that I do for all my applications. Once an application goes into production the idea is that you shouldn’t be getting that many email errors, but during development (if you’re like me) you’ll get an error email about every 3 minutes. As you can imagine emailing a struct is a significantly larger file size than sending that same struct as JSON. I emailed the test struct below with coldfusion as both a struct and as JSON. The struct html was 14.4 kb. The JSON was 0.8 kb. That means that I can make 18 more errors for every one error that I can make now before filling up the server.

Test Struct
I created the above struct because it contains the main data types. You can see it is a struct that contains an array, an integer, another struct, a string, and a boolean. The struct can be created with the following code.
<cfset var data = structnew()> <cfset data.success = true> <cfset data.message = "This is a message."> <cfset data.anumber = 101> <cfset data.anarray = arrayNew(1)> <cfset data.astruct = structnew()> <cfloop from="1" to="10" index="VARIABLES.i"> <cfset arrayAppend(data.anarray, VARIABLES.i)> <cfset data.astruct[VARIABLES.i] = VARIABLES.i + 5> </cfloop>
At Monserrate Monastery in Bogotá, Colombia.