Logging Errors to DB using LogBox

I am on ColdBox 3.1.0 (Near James 4:8)

I just saw this recording about LogBox http://vimeo.com/26745005 - in order to get a better understanding about LogBox. Great Recording, thanks Brad and Luis. :slight_smile:

I am setting up All errors thrown by the app to a DB which I can then view using Relax Logs (as suggested by Luis in the recording)

Here is what my configuration looks like :-

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',
              autocreate = true
            },
            levelMax = "INFO",
            levelMin = "FATAL"
          }
                },
                root = {levelMax="INFO", appenders="*"}
            };

Now, this does log stuff to the DB properly but I think I haven't got the severity levels correct. How do I get my Handlers / Models to start logging as well?

Thanks in advance….

Anuj Gakhar

Your handlers all have a log object already configured for them, so it is easy just use it.http://wiki.coldbox.org/imagelibrary/ColdBoxMajorClasses.jpg

In your model objects you would inject a logger object:
property name=“log” inject=“logbox:logger:{this}”

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Great, thanks Luis.

So, in order to log only errors to the DB - what needs to be done?

In my handlers, I can do log.error() or log.warn() etc but how do I make it write the errors to the DB ? Can I use the onException() function to log stuff to the DB?

public void function onException(required Event)
{
var exceptionBean = Event.getValue(“ExceptionBean”);
if(controller.getSetting(‘environment’) neq “development”)
{
log.error(exceptionBean);
}
}

Is that how you would do it ?

You have a few choices.

  1. Decide that ALL error level messages will go to the database by configuring that in your log box config
  2. Create a custom category that logs to the DB only and then use that custom category to log.

The 2nd approach you would have to inject a logger according to that category name. So let’s say I create this category in my config: http://wiki.coldbox.org/wiki/LogBox.cfm#LogBox_DSL

categories = {
db = { levelMin=“FATAL”, levelMax= “ERROR”, appenders=“db”
}

then use that category:

property name=“log” inject=“logbox:category:db”

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano

Hmm, Looks like I am doing something wrong here :-

Here is how I am injecting in my Handler:-

property name=“dblogger” inject=“logBox:category:dblogger”;

Here is my Logbox Config:-

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’,
autocreate = true
},
levelMax = “INFO”,
levelMin = “FATAL”
}
},
root = {levelMax=“INFO”, appenders="*"},
categories = {
“dblogger” = { levelMin=“FATAL”, levelMax= “ERROR”, appenders=“dbLog” }
},
OFF = [“coldbox.system”]
};

And here is the error I get after ?fwreinit

Error autowiring handlers.main. The DSL Definition {REF={null}, SCOPE={variables}, DSL={logBox:category:dblogger}, NAME={dblogger}, VALUE={null}, JAVACAST={null}, REQUIRED={false}} did not produce any resulting dependency The target requesting the dependency is: ‘handlers.main’

Ah, figured it out. It needs to be

property name=“dblogger” inject=“logBox:logger:dblogger”;

instead of

property name=“dblogger” inject=“logBox:category:dblogger”;

Thanks for your help Luis.

On a side note, I have just started using relaxLogs to look at these DB Logs being written by LogBox. Which is all great….but I was wondering RelaxLogs is pretty much a Log Viewer specific to LogBox and there could be a separate module called “LogViewer” or something simply for looking at the Logs….for people who don’t want all of the “relax” functionality… or is there a reason this was put under “relax” ?

Trying to figure out the LogBox Yours looks like it might be storing the info in the database is that correct?

Also how does it compare to this…

//LogBox DSL
logBox = {
// Define Appenders
appenders = {
coldboxTracer = { class=“coldbox.system.logging.appenders.ColdboxTracerAppender” }
},
// Root Logger
root = { levelmax=“INFO”, appenders="*" },
// Implicit Level Categories
info = [ “coldbox.system” ]
};

What else should I be doing to use LogBox I am newbie to this.

Finally managed to get the time to write up a blog post on this stuff

Hope someone finds it useful.

Great stuff Anuj!

Luis F. Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Social: twitter.com/lmajano facebook.com/lmajano