User Guide Cancel

WriteLog

 

Description

A function equivalent of the cflog tag, which can be used in <cfscript>.

Parameters

Same as the <cflog> tag:

Parameter

Req/Opt

Default

Description

text

Required

 

Message text to log.

application

Optional

yes

  • yes: logs the application name, if it is specified in a cfapplication tag or Application.cfc file.
  • no

file

Optional

 

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.

log

Optional

 

If you omit the file parameter, writes messages to standard log file. Ignored, if you specify file parameter.

  • Application: writes to Application.log, normally used for application-specific messages.
  • Scheduler: writes to Scheduler.log, normally used to log the execution of scheduled tasks.

type

Optional

Information

Type (severity) of the message:

  • Information
  • Warning
  • Error
  • Fatal

Category

Data output functions

Function syntax

WriteLog (text, type, application, file, log)
WriteLog (text, type, application, file, log)
WriteLog (text, type, application, file, log)

For positional notation, 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.

See also

cfscript cflog

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.");
WriteOutput(TotalInterest("$2500.00", "5.5%", "12"));
Trace(type="Information", inline="true", text="Interest calculation done.");
}
catch(InvalidData ex) {
//Writing the exception to log file under logs folder of web server.
WriteLog(type="Error", file="myapp.log", text="[#ex.type#] #ex.message#");
}
</cfscript>
<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."); WriteOutput(TotalInterest("$2500.00", "5.5%", "12")); Trace(type="Information", inline="true", text="Interest calculation done."); } catch(InvalidData ex) { //Writing the exception to log file under logs folder of web server. WriteLog(type="Error", file="myapp.log", text="[#ex.type#] #ex.message#"); } </cfscript>
<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."); 
        WriteOutput(TotalInterest("$2500.00", "5.5%", "12")); 
        Trace(type="Information", inline="true", text="Interest calculation done."); 
    } 
 
    catch(InvalidData ex) {
        //Writing the exception to log file under logs folder of web server. 
        WriteLog(type="Error", file="myapp.log", text="[#ex.type#] #ex.message#"); 
    } 
</cfscript>

Output

$137.50

Get help faster and easier

New user?