Using ColdBox and LogBox for Error Logging

On December 1, 2011, in ColdFusion, by Anuj Gakhar

If you are a ColdFusion developer and have not used ColdBox yet, well then, I must say you are missing something. Go ahead and try it out. This post won’t make much sense to anyone not familiar with ColdBox. For those who are familiar with ColdBox, I will be including an example of how you can use LogBox (an enterprise logging library) which comes bundled in with ColdBox, to log any errors to database and/or send emails to yourselves when errors occur on the site. I will be assuming that you are familiar with the ColdBox configuration file and how to setup your config values in this file.

Setup LogBox Config in ColdBox.cfc

[cf]
logBox = {
appenders = {
fileLog = {
class="coldbox.system.logging.appenders.AsyncRollingFileAppender",
properties =
{
filePath = "logs",
fileName = coldbox.appName,
autoExpand = true,
fileMaxSize = 2000,
fileMaxArchives = 3
}
},
dbLog = {
class="coldbox.system.logging.appenders.AsyncDBAppender",
properties =
{
dsn = ‘logging’,
table = ‘tbllogs_live_site’,
autocreate = true
},
levelMax = "INFO",
levelMin = "FATAL"
},
emailLog = {
class="coldbox.system.logging.appenders.EmailAppender",
properties =
{
from = ‘errors@yoursite.com’,
to = ‘recipient@yoursite.com’,
subject = ‘Error Email’,
mailserver = ‘smtp.gmail.com’,
mailusername = ‘errors@yoursite.com’,
mailpassword = ’email_password’,
useTLS = ‘true’,
mailport = ‘587’
},
levelMax = "INFO",
levelMin = "FATAL"
}
},
root = {levelMax="INFO", appenders="*"},
categories = {
"dblogger" = { levelMin="FATAL", levelMax= "INFO", appenders="dbLog" },
"emaillogger" = { levelMin="FATAL", levelMax= "INFO", appenders="emailLog" }
},
OFF = ["coldbox.system"]
};
[/cf]

OK, there is quite a bit going on here. I have defined 3 Appenders :-

  • fileLog – Logs to a file on the filesystem – it’s there for everything.
  • dbLog – Logs to the database – all you have to do is create the datasource “logging” or whatever you want to call it. And because I have turned on “autocreate” – it will create the tables for you. So just create the datasource and you are ready!
  • emailLog – This is obviously an email appender, I use gmail smtp to send out emails so I have used some extra properties to be able to send email using gmail. Particularly useTLS and the port – these are specific to smtp.gmail.com but you can choose any mailserver of your choice.

And then I have defined 2 categories “dblogger” and “emaillogger” which I will be using to log errors. You can adjust the level of logging you want by configuring the levelMin and levelMax.

Once LogBox is configured as above, you can turn on the exceptionHandler property in ColdBox.cfc like below :-

[cf]exceptionHandler = "main.onException" [/cf]

By doing so, you are telling ColdBox to use main.onException() action to handle any custom exceptions.

Inject the Loggers

In main.cfc, inject the 2 logger categories (using WireBox which is built in to ColdBox as well) to be able to use them :-
[cf]
component name="main" extends="coldbox.system.eventhandler"
{
property name="dblogger" inject="logBox:logger:dblogger";
property name="emaillogger" inject="logBox:logger:emaillogger";
}
[/cf]

Setup Custom Exception Handler

In you main.cfc, assuming you setup the exception handler to be main.onException, add the following function :-

[cf]
component name="main" extends="coldbox.system.eventhandler"
{
property name="dblogger" inject="logBox:logger:dblogger";
property name="emaillogger" inject="logBox:logger:emaillogger";

public void function onException(required Event)
{
var exceptionBean = Event.getValue("ExceptionBean");
dblogger.error("MESSAGE:#exceptionBean.getExceptionStruct().message#, DETAIL:#exceptionBean.getExceptionStruct().detail#",exceptionBean);
emaillogger.error("MESSAGE:#exceptionBean.getExceptionStruct().message#, DETAIL:#exceptionBean.getExceptionStruct().detail#",exceptionBean.getMemento());
}
}
[/cf]

As you can see, whenever an error occurs, the dblogger will log it to the database and the emailogger will send an email to the recipient specified in the config. How cool is that!

So, now whenver an error occurs on your ColdBox emabled site, you have it logged to 2 different sources – database and email. You can choose to use either or both. Infact, you can use any of the other appenders available to log stuff. There are all kinds of appenders available – TwitterAppender, SocketAppender etc….but I just think that DB and email are the 2 most important ones.

That’s it for this post. If you have a better way of logging errors, let me know!

P.S. I am using ColdBox 3.1.0 for the demo above. Have not tried in previous versions.

Tagged with:  

6 Responses to Using ColdBox and LogBox for Error Logging

  1. chris hough says:

    thank you for posting this, it really helped me configure our logging in my first cb application

  2. chris hough says:

    btw, if you are running cf 8.01 you have to adjust the categories as follows:

    //LogBox DSL => OFF,FATAL,ERROR,WARN,INFO,DEBUG
    logBox = {
    // Define Appenders
    appenders = {
    appLogFile = {
    class=”coldbox.system.logging.appenders.FileAppender”,
    properties = {
    filePath=GetAppShared().AppLogs.AppLogFilePath,
    fileName=’Application’,
    autoExpand=false,
    fileMaxArchives=1,
    fileMaxSize=3
    },
    levelMax = “DEBUG”,
    levelMin = “FATAL”
    },
    exceptionLogFile = {
    class=”coldbox.system.logging.appenders.FileAppender”,
    properties = {
    filePath=GetAppShared().AppLogs.AppLogFilePath,
    fileName=’Exception’,
    autoExpand=false,
    fileMaxArchives=1,
    fileMaxSize=3
    },
    levelMax = “DEBUG”,
    levelMin = “FATAL”
    }
    },
    // Root Logger
    root = {levelMax=”INFO”, appenders=”*”},
    OFF = [“coldbox.system”]
    };
    logbox.categories[“appLogger”] = {
    levelMin=”FATAL”,
    levelMax= “INFO”,
    appenders=”appLogFile”
    };
    logbox.categories[“exceptionLogger”] = {
    levelMin=”FATAL”,
    levelMax= “INFO”,
    appenders=”exceptionLogFile”
    };

    • websbygeorge says:

      I am using logbox and wirebox in a standalone instance. Wirebox is working great. I am almost to the point of creating my config file for logbox. I want to log the arguments passed into a method and the data returned by the method. I would like to write this to a database. What would my code look like in the method?

  3. Rajni Barvaliya says:

    hank you for posting this, it really helped me configure our logging in my first cf application

  4. Kurt Look says:

    This is still helping people.

Leave a Reply to chris hough Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

© 2011 Anuj Gakhar
%d bloggers like this: