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">
<!--- change to your path --->
<cfset var rootpath = "/Users/ray/Desktop/">
<cfset var newlog = "[#dateformat(now(), "mm/dd/yy")# #timeFormat(now(), "h:mm:ss:l tt")#] [#arguments.type#] #arguments.message#">
<cffile action="append" file="#rootpath##arguments.file#.log" output="#newlog#" addnewline="true">
</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...
Archived Comments
<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>
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.
@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....)
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.
Not that I know of - and you mean cflock. If you were worried though, you could always CFLOCK the call.
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....
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).