User Guide Cancel

Trace

 

Description

A function equivalent of the <cftrace> tag and is used in the <cfscript> mode.

Note: This function differs from  cftrace  in that it picks the time at the beginning of the function execution instead of the end as is the case with  cftrace .

Parameters

Same as the <cftrace> tag.

Category

Debugging functions

Function syntax

trace (var, text, type, category, inline, abort)
trace (var, text, type, category, inline, abort)
trace (var, text, type, category, inline, abort)

For positional notations, the sequence must be followed exactly in the same manner as provided in the syntax. If you do not provide one of the parameters, use an empty string instead. This does not apply to Boolean values for which you must provide proper values even if you have to skip them.

History

ColdFusion 11: Parameters passed to this function needs to be in a comma-separated format.

ColdFusion 9: Added this function.

See also

cfscript cftrace

Usage

You can call this function as name=value pair or as positional argument.

Example

<cfscript>
function TotalInterest(principal, annualRate, months) {
var years = 0;
var interestRate = 0;
var totalInterest = 0;
principal = REReplace(trim(principal), "[\$]", "", "ALL");
annualRate = Replace(trim(annualRate), "%", "", "ALL");
if ((principal <= 0) OR (annualRate <= 0) OR (months <= 0)) {
Throw(type="InvalidData",message="All values must be greater than 0.");
}
interestRate = annualRate / 100;
years = months / 12;
totalInterest = principal * (((1 + interestRate) ^ years) - 1);
return DollarFormat(totalInterest);
}
try {
Trace(type="Information", inline="true", text="Calculating interest."); //Use of
trace function in cfsript
WriteOutput(TotalInterest("$2500.00", "5.5%", "12"));
Trace(type="Information", inline="true", text="Interest calculation done.");
}
catch(InvalidData ex) {
//Displayig exception details on screen
WriteOutput("<p>An InvalidData exception was thrown.</p>");
WriteOutput("<p>#ex.message#</p>");
//Writting the exception to log file under logs folder of web server.
WriteLog(type="Error", file="myapp.log", text="[#ex.type#] #ex.message#");
}
<cfscript> function TotalInterest(principal, annualRate, months) { var years = 0; var interestRate = 0; var totalInterest = 0; principal = REReplace(trim(principal), "[\$]", "", "ALL"); annualRate = Replace(trim(annualRate), "%", "", "ALL"); if ((principal <= 0) OR (annualRate <= 0) OR (months <= 0)) { Throw(type="InvalidData",message="All values must be greater than 0."); } interestRate = annualRate / 100; years = months / 12; totalInterest = principal * (((1 + interestRate) ^ years) - 1); return DollarFormat(totalInterest); } try { Trace(type="Information", inline="true", text="Calculating interest."); //Use of trace function in cfsript WriteOutput(TotalInterest("$2500.00", "5.5%", "12")); Trace(type="Information", inline="true", text="Interest calculation done."); } catch(InvalidData ex) { //Displayig exception details on screen WriteOutput("<p>An InvalidData exception was thrown.</p>"); WriteOutput("<p>#ex.message#</p>"); //Writting the exception to log file under logs folder of web server. WriteLog(type="Error", file="myapp.log", text="[#ex.type#] #ex.message#"); }
<cfscript> 
function TotalInterest(principal, annualRate, months) { 
var years = 0; 
var interestRate = 0; 
var totalInterest = 0; 
principal = REReplace(trim(principal), "[\$]", "", "ALL"); 
annualRate = Replace(trim(annualRate), "%", "", "ALL"); 

if ((principal <= 0) OR (annualRate <= 0) OR (months <= 0)) { 
Throw(type="InvalidData",message="All values must be greater than 0."); 
} 
interestRate = annualRate / 100; 
years = months / 12; 
totalInterest = principal * (((1 + interestRate) ^ years) - 1); 
return DollarFormat(totalInterest); 
} 
try { 
Trace(type="Information", inline="true", text="Calculating interest."); //Use of 
trace function in cfsript 
WriteOutput(TotalInterest("$2500.00", "5.5%", "12")); 
Trace(type="Information", inline="true", text="Interest calculation done."); 
} 

catch(InvalidData ex) { 
//Displayig exception details on screen 
WriteOutput("<p>An InvalidData exception was thrown.</p>"); 
WriteOutput("<p>#ex.message#</p>"); 
//Writting the exception to log file under logs folder of web server. 
WriteLog(type="Error", file="myapp.log", text="[#ex.type#] #ex.message#"); 
}

Get help faster and easier

New user?