No CFADMIN access means no logging, right?

This week seems to be ‘Mr Obvious’ week for me, and I apologize for that, but hopefully some of these entries are useful to folks. I was looking over email from a reader who commented that she couldn’t use CFLOG because she didn’t have admin access. While that is technically true, it certainly does not mean you can’t use logging style features.

As long as you have access to CFFILE, it is trivial to append to a file, and you can even create a simple UDF wrapper for it: <cffunction name="mylog" access="public" returnType="void" output="true"> <cfargument name="file" type="string" required="true"> <cfargument name="message" type="string" required="true"> <cfargument name="type" type="string" required="false" default="information">

&lt;!--- change to your path ---&gt;
&lt;cfset var rootpath = "/Users/ray/Desktop/"&gt;

&lt;cfset var newlog = "[#dateformat(now(), "mm/dd/yy")# #timeFormat(now(), "h:mm:ss:l tt")#] [#arguments.type#] #arguments.message#"&gt;
&lt;cffile action="append" file="#rootpath##arguments.file#.log" output="#newlog#" addnewline="true"&gt;

</cffunction> </code>

This UDF takes in a file, message, and optional type. The file works much like CFLOG, where you specify just the name of the log, not a full path. Notice I’ve hard coded a root path. That’s bad. The OO Police are after me as we speak. So yes, I could write this UDF as a CFC where a root path is passed in during instantiation. I could also make the file argument be a full path, or simply add a new argument. However, I was going for quick and dirty here. Modify it as you see fit.

Anyway - based on rootpath plus the name of the file you want, the code automatically adds to the log file with a date+time stamp, your type value, and then your string. Calling it then becomes as simple as:

<cfset currentTime = getTickCount()> <cfset sleep(randRange(1,1000))> <cfset dur = getTickCount() - currentTime>

<cfset mylog(“slowthing”, “It took #dur# to run my slow process.”)>

Done… </code>

Notice I didn’t call it log. Log is a built in ColdFusion function for math geeks. That’s why in CF9 the script version is writeLog.

Lastly - let’s say you know you are deploying to a host where you won’t have CFLOG access and you make use of this UDF. If you use ColdFusion Builder, doesn’t forget the excellent TailView (which is available elsewhere too of course) which can nicely render your log entries. Check out the video…

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA