View Single Post
  #9 (permalink)  
Old 02-18-2008, 09:55 PM
prasannavigneshr prasannavigneshr is offline
D-Web Incredible
 
Join Date: Feb 2007
Posts: 1,321
prasannavigneshr is on a distinguished road
Send a message via MSN to prasannavigneshr
Thumbs up ColdFusion Tips & Tricks - Creating a logfile of a programs execution

Creating a logfile of a programs execution


If you want to create a log file of a programs execution (ideal for programs that run via the Scheduler), here is a tag that I wrote to perform the task for you. All you need to do is to copy-n-paste the code below into a file called "DumpQuery.cfm" located in your \CFUSION\CustomTags directory.

Note that the program traps any file errors so that if you fubar something with the file name, it doesn't stop the program from executing. So if you run your program and get no log file, check your file path to ensure that it is valid.

Example HTML/CFML code:

Code:
<cfsetting enablecfoutputonly="Yes">

<!---

Creates a log file

Passed Attributes
Action:   Open        Kills any existing log file allowing a fresh log file to be created
                      Initilizes session variable
          Close       Writes any pending information to the log file
          <none>      Append to the specified log file
file:                 Full path information and file name of the log file.  If not specified, it
                      will check to see if a variable "logfile" was defined in the calling template.
                      If so, it will use the file information from the logfile variable.  Otherwise,
                      it will create a log file as the same as the template calling it with an 
                      ".log" appended to the end of the cfm.  Ie: C:\InetPub\www\index.cfm.log
timestamp:  Y/N       If Y, the current date and time is prepended to the text to record
text:                 The text to append to the log file
loghistorysize:       A number in bytes to cache.  Once the logfile history exceedes this amount, it
                      is appended to the log file.  Defaults to 10k.

You can only write to one log file at a time.  If you start to write to a 2nd log file, be sure to close the first one first.

If you wish to append to a already existing log file without deleting it first, do not specify the "open" action.

If you do not close a log file, any pending information to be written will be lost.

Examples:
Delete any existing logfile and open a fresh file:
<CF_Log file="C:\Inetput\WWW\History.log" action="open">

Append a string of text to a logfile defined with "logfile" with a timestamp
<CF_Log text="User viewed home page">

Same as above but without a timestamp
<CF_Log text="User viewed home page" timestamp="N">

Done with program, want to flush out the log cache
<CF_Log action="close">

--->

<CFPARAM name="attributes.action" default="">
<CFPARAM name="attributes.file" default="">
<CFIF attributes.file NEQ "">
  <CFSET caller.logfile=attributes.file>
</CFIF>
<CFPARAM name="caller.logfile" default="#CF_TEMPLATE_PATH#.log">
<CFPARAM name="file" default="#caller.logfile#">
<CFPARAM name="attributes.timestamp" default="Y">
<CFPARAM name="attributes.text" default="">
<CFPARAM name="attributes.loghistorysize" default="10240">
<CFPARAM name="caller.loghistory" default="">

<CFSET attributes.action=LCase(attributes.action)>

<CFIF attributes.timestamp EQUAL "Y">
  <CFSET ts=DateFormat(Now(),"mm-dd-yyyy") & " " & TimeFormat(Now(),"HH:mm:ss") & " ">
<CFELSE>
  <CFSET ts="">
</CFIF>

<CFTRY>
<CFIF attributes.action EQUAL "open">
  <cffile action="DELETE"
          file="#file#">
  <cffile action="WRITE" file="#file#" output="#ts#Log Opened" mode="777" addnewline="Yes">
  <CFSET caller.loghistory="">
</CFIF>
<CFCATCH type="any">
  <!--- Eat any file errors --->
</CFCATCH>
</CFTRY>

<CFTRY>
<CFIF attributes.action EQUAL "close">
  <CFSET caller.loghistory=caller.loghistory & Chr(13) & Chr(10) & ts & "Log Closed">
  <cffile action="APPEND"
          file="#file#"
          output="#caller.loghistory#"
          addnewline="Yes"
          mode="777">
  <CFSET caller.loghistory="">
</CFIF>
<CFCATCH type="any">
  <!--- Eat any file errors --->
</CFCATCH>
</CFTRY>


<CFTRY>
<CFIF Trim(attributes.action) NOT EQUAL "open" AND Trim(attributes.action) NOT EQUAL "close">
<!---   <CFOUTPUT>#ts##attributes.text#<br></CFOUTPUT> --->
  <CFIF caller.loghistory EQUAL "">
    <CFSET caller.loghistory=ts & attributes.text>
  <CFELSE>
    <CFSET caller.loghistory=caller.loghistory & Chr(13) & Chr(10) & ts & attributes.text>
  </CFIF>
  <CFIF Len(caller.loghistory) GREATER THAN attributes.loghistorysize>
    <cffile action="APPEND"
            file="#file#"
            output="#caller.loghistory#"
            addnewline="Yes"
            mode="777">
    <CFSET caller.loghistory="">
  </CFIF>
</CFIF>
<CFCATCH type="any">
  <!--- Eat any file errors --->
</CFCATCH>
</CFTRY>
__________________
Prasanna Vignesh
MCPD | Web Developer
Reply With Quote