How to read ColdFusion log files directly from your app
Written on 16 October 2011, 01:41am
Tagged with: coldfusion, logging
CFLOG
is a very useful tool for debugging. Along with CFDUMP
, it is probably one most used tags in development 🙂
Logging with 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: 277)
- Likes (1)
-
Share
- Comments (2)
Comments (2)
After lots of research surrounding the onMissingTemplate function, cflog, and creating a custom error log, it became very event that the information you have would be great except for the fact that you must be on a server that is not shared. Hosting companies do not seem to allow for log file reading by the cffile tag.
This is unfortunate since I am using the cflog tag in my application.cfc file and it does not allow me to read the .log file created during error logging. So my solution was to just write the errors to different errorlog files depending on the type of error and then outputting the error to the corresponding page such as 400, 401, 403, 404, 500 etc…
This was the only solution I could create for getting around the issue concerning a shared hosting environment.
It also seals the blood vessels to reduce any bleeding that might occur.
Some respond to certain types of treatment, while others are notoriously difficult to treat.
In any case, the treatment of warts is necessary
regardless of whether this is a cosmetic or much more
dangerous problem.