No CFADMIN access means no logging, right?

This post is more than 2 years old.

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>

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

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 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 Scott Stroz posted on 7/24/2009 at 6:55 PM

<shamelessPlug>

Also, you can use <cflog> and then use Flogr http://flogr.riaforge.org . There is no need for CFADMIN access, and you can drop Flogr into any directory in any CF8 app and it will just work.

</shamelessPlug>

Comment 2 by Tom Chiverton posted on 7/24/2009 at 7:04 PM

In Railo you could replace the default CFLOG implementation with your own one :-)

P.S. the new theme looks nice, but everything is shifted half a column left in konqueror so all cut off.

Comment 3 by Raymond Camden posted on 7/24/2009 at 7:07 PM

@Scott: I'd be worried about logging to the public dir, but as you say (well you did on IM) folks on shared hosting shouldn't expect too much privacy.

@Tom: It's your fault for using Linux. You aren't contributing to the economy. Get off my site you OS FAN BOI!!! (Sorry....)

Comment 4 by JC posted on 7/25/2009 at 12:04 AM

Don't you need to use a transaction or something to lock the file? I've had file lock issues in the past trying to write text files.

Comment 5 by Raymond Camden posted on 7/25/2009 at 12:06 AM

Not that I know of - and you mean cflock. If you were worried though, you could always CFLOCK the call.

Comment 6 by zac spitzer posted on 7/25/2009 at 9:02 AM

i've seen bad locking issues with self rolled loggers too

checkout coldbox's LogBox, can be used standalone from coldbox

http://blog.coldbox.org/arc...
http://ortus.svnrepository....

Comment 7 by Raymond Camden posted on 7/25/2009 at 11:08 PM

Can you clarify more about 'bad issues'? Also, I'm going to ask some Adobe engineers about file/append, and what's used underneath (to see if we need locking).