[logbox-1.7] Multiple targets

While I have learned so much, I feel I know so little. Here’s what I want to do that I cannot seem to.

When I think of how I want logbox to work, I think of what I want to log. A database error goes here, a code bug goes there, a notification of some activity goes over there…regardless of whether the message is info, debug, error, etc. For example, in some process where the user changes a field of data for a profile account…I might want to:

  1. if “debugging” of some sort is turned on, I want to log the fact that the user has accessed the save feature for a profile account to a log file
  2. if the user is not logged in when the request hits the save process (say something timed out), I want to log a piece of information that there was some invalid access made to the save process
  3. if the save is unsuccessful because a non-validated piece of data failed to be saved by the database, I want to log the database error to a table in the database
  4. if the save was successful, I want to log information about what changed to a different table in the database
  5. if the save was successful and “debugging” of some sort is turned on, I want to log the fact that the change was successful and the user exited the save process to a the same log file as #1 above.
    Most of those are probably “info”, some might be “debug” and one might be “error”…but that are all logging to different places and NOT duplicated across multiple locations.

What mechanism in logbox best accomplishes this.

Thanks,
Mike

Named loggers. By default when you get a logger in LogBox inside a CFC it looks like this:

logbox.getLogger(this);

That creates a logger whose name (or category) is “com.path.to.yourComponent”.

In your logbox config, you can apply completely different rules to different logger names. Better yet, you can “inherit” settings using the dot-delimited path as a hierarchy. All loggers inherit from the root logger. Each named logger can have it’s own list of appenders and it’s own max/min settings.

So, for example, if all your security-related classes start com.mysite.security, you can apply settings to that category (name) and com.mysite.security.login and com.mysite.security.oAuth will both use that setting. This is how you can turn off all debug logs for the entire ColdBox framework with this line of code in your config:

categories = {“coldbox.system” = { levelMax=“INFO”} };

Now, perhaps you’re not using object for everything, or you want to affect “handlers.security” and “com.mysite.security” at the same time-- you can name loggers anything you want. Just ask for a logger by the name you want and it will be created. You only need to declare it in the config if you want to apply custom settings to it.

logbox.getLogger(“security”);
logbox.getLogger(“foobar”);
logbox.getLogger(“I_like_getting_jiggy_with_logbox”);

Now, just use the above code to get those loggers where ever you want them in your application. Injection DSL would look like this:

property name=“securityLogger” inject=“logbox:logger:security”;
property name=“foobarLogger” inject=“logbox:logger:foobar”;
property name=“jiggyLogger” inject=“logbox:logger:I_like_getting_jiggy_with_logbox”;

Declare your named loggers as categories in the LogBox portion of your config and assign appenders as you see fit.

http://wiki.coldbox.org/wiki/LogBox.cfm#Logger_Category_Inheritance

Thanks!

~Brad

ColdBox Platform Evangelist
Ortus Solutions, Corp

E-mail: brad@coldbox.org
ColdBox Platform: http://www.coldbox.org
Blog: http://www.codersrevolution.com