ColdBox used to have a BugTracerReport config element which you could configure to send error emails to yourself while showing a user friendly error page to the site users. Not quite sure of the exact version, but I think this feature was deprecated in version 3.1 in favour of the new LogBox which comes built-in with the ColdBox download. The BugTracerReports were configured in the config file like this :-

	<BugTracerReports>
		<!-- <BugEmail>myemail@gmail.com</BugEmail> -->
	</BugTracerReports>

LogBox is pretty cool and to achieve the same functionality in LogBox, you would need to setup an Email Appender with the proper settings that sends out emails whenever an error occurs. However, the only problem I found with that was the default format of the email that goes out with LogBox Email Appenders is nowhere close to what the BugTracerReports used to send out, which were pretty informative and useful. Although, this is very easy to change and get the same email back again. Here is what needs to happen :-

In you LogBox config, setup an email appender with a custom layout, something like this :-

logBox = {
	appenders = {
	emailLog = {
			class="coldbox.system.logging.appenders.EmailAppender",
			layout = "model.bugreports.BugReportEmailLayout",
			properties = 
			{
				from = 'errors@yoursite.com',
				to = 'you@yoursite.com',
				subject = 'Error Email',
				mailserver = 'smtp.gmail.com',
				mailusername = 'username',
				mailpassword = 'password',
				useTLS = 'true',
				mailport = '587'
			},
			levelMax = "INFO",
			levelMin = "FATAL"
		}		
    },	
    root = {levelMax="INFO", appenders="*"},
	categories = {
  		"emaillogger" = { levelMin="FATAL", levelMax= "INFO", appenders="emailLog" }
	},
	OFF = ["coldbox.system"]
};

Notice the custom layout, BugReportEmailLayout, which is infact pretty simple and looks like this :-

component extends="coldbox.system.logging.Layout"
{
	public function format(required logEvent)
	{
		return logEvent.getExtraInfo();
	}
	
	public function getSubject(required logEvent)
	{
		var subject = "";
		subject = "[#htmlEditFormat(cgi.http_host)#]";
		subject = subject & " " & left(logEvent.getMessage(),100);
		return subject;
	}
}

And then, finally, this is how you use it via the onException action (which in my case in main.onException) method in the config file…

	public void function onException(required Event)
	{
		var exceptionBean = Event.getValue("ExceptionBean");
		var Exception = exceptionBean;
		var bugreport= "";

		savecontent variable="bugreport" {
			include "/coldbox/system/includes/BugReport.cfm";
		}
		emaillogger.error(exceptionBean.getMessage(), bugreport);

	}

Notice, how I am rendering the BugReport template into the extraInfo of the Logger and using that in the Layout. I am also changing the subject of the email in the Layout.

Don’t forget to first inject the emaillogger in your handler or wherever else you are using this :-

property name="emaillogger" inject="logBox:logger:emaillogger";	

That’s all for this port, hope someone finds this useful.

Tagged with:  

6 Responses to Replacing BugTracer Email Functionality in ColdBox with LogBox Loggers

  1. John Whish says:

    I’ve actually started using Hoth ( http://aarongreenlee.com/share/hoth-coldfusion-errors-tracking-reporting/ ) for all my error reporting. It works really well and integrates with ColdBox or any other application easily.

  2. Great tip, Anuj! Thanks for the shout-out John.

  3. Finding it difficult to get my head around this. But thanks anyway.

  4. Yeah same here. I think I will keep reading this until it becomes clear.

  5. Great post Anuj. It was a great kick-starter.

Leave a Reply

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 292 other subscribers

© 2011 Anuj Gakhar
%d bloggers like this: