Logging Errors to DB using LogBox

247 views
Skip to first unread message

Anuj Gakhar

unread,
Sep 23, 2011, 12:03:58 PM9/23/11
to col...@googlegroups.com
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. :)

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

Luis Majano

unread,
Sep 23, 2011, 12:08:40 PM9/23/11
to col...@googlegroups.com
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}"
--
You received this message because you are subscribed to the Google Groups "ColdBox Platform" group.
To post to this group, send email to col...@googlegroups.com
To unsubscribe from this group, send email to coldbox-u...@googlegroups.com
For more options, visit this group at http://groups-beta.google.com/group/coldbox
For News, visit http://blog.coldbox.org
For Documentation, visit http://wiki.coldbox.org

Anuj Gakhar

unread,
Sep 23, 2011, 12:16:00 PM9/23/11
to col...@googlegroups.com
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 ? 

Luis Majano

unread,
Sep 23, 2011, 12:39:39 PM9/23/11
to col...@googlegroups.com
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"

Anuj Gakhar

unread,
Sep 23, 2011, 1:06:53 PM9/23/11
to col...@googlegroups.com
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'

Anuj Gakhar

unread,
Sep 23, 2011, 1:14:39 PM9/23/11
to Anuj Gakhar, col...@googlegroups.com
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. 

Anuj Gakhar

unread,
Sep 23, 2011, 1:27:20 PM9/23/11
to Anuj Gakhar, col...@googlegroups.com
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" ? 

Nathan Stanford Sr

unread,
Nov 2, 2011, 4:19:19 PM11/2/11
to col...@googlegroups.com
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.

br...@bradwood.com

unread,
Nov 2, 2011, 5:40:41 PM11/2/11
to col...@googlegroups.com
Here are the docs for LogBox

Essentially, LogBox is a generic system by which any portion of your application can log messages of varying severity (debug, info, warn, error, fatal) to any number of appenders.  Appenders can do whatever you want with the message from writing them to a file, inserting them into a database, or adding them to an array in-memory.  More than one appender can be at play at once and any given appender is only obligated to deal with the message severities and/or the portions of your application that it feels like.

The snippet of code you have below configures a single appender which logs to the ColdBox debugging information at the bottom of each page (assuming you have it turned on).  Your code snippet then tells LogBox to use that appender to log messages of severity INFO or lower.  (That excludes DEBUG and includes everything else).

If you want to sent that message other places, (E-mail, text file) then configure additional appenders.  Check out the docs and look for the dbAppender and emailAppender for more info.

Thanks!

~Brad



Anuj Gakhar

unread,
Dec 1, 2011, 8:21:04 AM12/1/11
to col...@googlegroups.com
Finally managed to get the time to write up a blog post on this stuff

http://www.anujgakhar.com/2011/12/01/using-coldbox-and-logbox-for-error-logging/

Hope someone finds it useful.

Luis Majano

unread,
Dec 1, 2011, 1:04:39 PM12/1/11
to col...@googlegroups.com
Great stuff Anuj!
--
Reply all
Reply to author
Forward
0 new messages