Accessibility

TechNote (Archived)

ColdFusion MX: How to log errors when using a site-wide error handler

Issue


When using the Site-wide Error Handler option in the ColdFusion MX Administrator, error messages are not logged to the Application log by design. However, the page you use for your site-wide error handler is designated to handle the errors and acts as ifcferror type="exception" had been called. Therefore, you have full access to error variables such as error.diagnostics and error.httpReferer. When used in combination with thecflog tag, you can write a custom entry to the Application.log or a custom log file and choose to output some, none, or all of the error.

Note: cflog can only write to .log files located in the cf_root\logs directory.

Solution


This example demonstrates how to log errors usingcflog and a site-wide error handler.

  1. Create the site-wide handler template.

    site_wide_error_handler.cfm:
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Error Cleanup</title></head><body><h2>Sorry, there's been an error.</h2><BR><cftry><cflog     text="#error.diagnostics#, #error.httpReferer#, #error.template##error.querystring#"     log="APPLICATION" type="Error" application="yes"><CFOUTPUT><FONT face="Arial" ><strong>Diagnostic Information:</strong><BR><FONT face="Arial" color="800000" >#error.diagnostics#</FONT><P><strong>Generated Content:</strong><BR><FONT face="Arial" color="800000" >#error.generatedContent#</FONT><P><strong>Referring Page:</strong><BR><FONT face="Arial" color="800000" >#error.httpReferer#</FONT><P><strong>Template in Error:</strong><BR><FONT face="Arial" color="800000" >#error.template#?#error.querystring#</FONT></FONT></CFOUTPUT><CFCATCH></CFCATCH></CFTRY></body></html> 
  2. In the ColdFusion MX Administrator ServerSettings/Settings section, specify the site_wide_error_handler.cfm file in the Site-Wide Error Handler field and click Submit.
  3. Create a template called make_error.cfm:

    make_error.cfm:
     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Make An Error</title></head><body><cfoutput><A href="#CGI.SCRIPT_NAME#?make_error=YES">Throw an error!</A></cfoutput><CFIF IsDefined("url.make_error")><!--- Output a nonexistent variable to force error. ---><CFOUTPUT>#variables.blah#</CFOUTPUT></CFIF></body></html> 
  4. Run make_error.cfm in your browser and click the link displayed. The browser should display:
     Sorry, there's been an error.Diagnostic Information:Element BLAH is undefined in VARIABLES. The error occurred on line 7.Generated Content:Throw an error!Referring Page:http://localhost:8500/make_error.cfmTemplate in Error:/make_error.cfm?make_error=YES 
  5. Check the cf_root\logs\application.log and you should see an entry similar to the following:
     "Error","web-3","02/20/04","09:39:03",,"Element BLAH is undefined in VARIABLES. The error occurred on line 7., http://localhost:8500/make_error.cfm, /make_error.cfm make_error=YES 


Additional Information


AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Creative Commons License

Search Support


Document Details

ID:tn_19198
Browser:Chrome
Internet Explorer
Netscape
Opera
Safari
Firefox
Database:DB2
Informix
MySQL
Oracle
SQL Server
Sybase
MS Access

Products Affected:

coldfusion