How to read ColdFusion log files directly from your app
Written on 16 October 2011, 01:41am
CFLOG is a very useful tool for debugging. Along with
CFDUMP, it is probably one most used tags in development 🙂
CFLOG is extremely simple:
<cflog file="myLogFile" text="#TimeFormat(Now(),'hh:mm:ss.l')#: log message" />
I am usually prepending the timestamp to get the milliseconds (by default, CF only logs the time down to the seconds):
"Information","web-261","10/13/11","06:03:21","APP","06:03:21.319: log message"
About the other attribute of the CFLOG: file, the CF documentation says:
Message file. Specify only the main part of the filename. For example, to log to the Testing.log file, specify “Testing”.
The file must be located in the default log directory. You cannot specify a directory path. If the file does not exist, it is created automatically, with the extension .log.
The log file is created inside the ColdFusion default log directory. You can see the content of this file using the ‘Log files’ screen of your CF Admin interface:
But what do you do when you don’t have access to the CF Admin interface? Luckily, you can still see the contents of your log file. You just have to locate the log folder contents and then output the log file contens, as the following script shows:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <title>Read log</title> </head> <body> <cfsetting showdebugoutput="no" requesttimeout="200" /> <cfoutput> Server.ColdFusion.RootDir=#Server.ColdFusion.RootDir# <!--- quick and dirty way of getting the file separator ---> <cfif find('/',Server.ColdFusion.RootDir)> <cfset s='/'> <cfelse> <cfset s='\'> </cfif> > <cfset logsFile = Server.ColdFusion.RootDir & "#s#logs#s#yourFile.log"> <cfif not fileExists(logsFile)> <h3>Error: log file #logsFile# does not exist</h3> <cfabort /> <cfelse> <h3>Showing contents of #logsFile#</h3> </cfif> </cfoutput> <pre> <cfscript> myfile = FileRead(logsFile); WriteOutput("#myfile#"); </cfscript> </pre> </body> </html>
Written by Dorin Moise (Published articles: 220)