As mentioned in my previous post it might be a bit overwhelming when you want to start logging with coldbox. So why not just appending some information to a file instead? There’s a few reasons for this:
- standards: Logbox has a standard way of sending information to a logging source, so you don’t have to rewrite your code if your loggin requirements change. There are several logging levels (severity), more on that later. And configuration can be done in one central place if you want.
- performance: Logbox has some standard async loggers. This is a performance benefit, your code will not wait until your logging is finished
- reusability. This has a a lot in common with the first bullet on standards. All coldbox framework code and many libraries are using logbox. So if you know how to configure Logbox, you can tap into all info which will be logged by framework and other library code.
- flexibility: Logbox allows you to enable debug logging or suppress other logging level for any part of your own code, the coldbox framework or installed modules. And it allows you to send debugging info to any repository you want. You can even create your own logging methods.
In this post I will show you how to add simple logging capabilities to a coldbox application. I will explain all concepts of Logbox in detail, but I will start with the short version:
- create a Logbox struct in your coldbox configuration file with keys for
appenders
(required), root
logger (also required), categories
(optional but very useful) and the related keys debug
, info
, warning
, error
, fatal
and off
, which are all used to set maximum logging levels for certain categories - add at least one appender to your appenders array. There are many appenders such as file appenders, database appenders, console appenders and more. Each appender has at least a
class
name and some properties
which may vary based on the type of appender - add a root logger. This root logger is some kind of default category. If you create a a named logger, which is a logger for a category it will try to find a logger configuration for this category and if it doesn’t find the category or a parent category it will use this root logger config. A root logger has an
appenders
property where you can specify to which appenders the message will be sent, and a levelMin
(default=FATAL) and levelMax
(default=DEBUG) property to specify which level of messages will be allowed for this category. - add some (named)
categories
. This is optional, but makes live easier if you want to modify logging behaviour for parts of your application, like sending it to other appenders or restricting your logging to certain levels. - add some impliciet categories. this is done by using the keys
debug
, info
, warning
, error
, fatal
and off
to specify a maximum level for certain categories.
Your config will look a bit like this ( only with some real values…. ) :
//LogBox DSL
logBox = {
// The configuration file without fileextension to use for operation, instead of using this structure
configFile = "config/LogBox",
// Appenders
appenders = {
appenderName = {
class="class.to.appender",
layout="class.to.layout",
levelMin=0,
levelMax=4,
properties={
name = value,
prop2 = value 2
}
},
// Root Logger
root = {levelMin="FATAL", levelMax="DEBUG", appenders="*"},
// Granular Categories
categories = {
"coldbox.system" = { levelMin="FATAL", levelMax="INFO", appenders="*"},
"model.security" = { levelMax="DEBUG", appenders="console"}
}
// Implicit categories
debug = ["coldbox.system.interceptors"],
info = ["model.class", "model2.class2"],
warn = ["model.class", "model2.class2"],
error = ["model.class", "model2.class2"],
fatal = ["model.class", "model2.class2"],
off = ["model.class", "model2.class2"]
};
Now you can start logging. In a model the most flexible way to do this is by wirebox injection, e.g
property name="log" inject="logbox:logger:{this}";
//or
property name="myLog" inject="logbox:logger:models.sub.myService";
//or
property name="perfLog" inject="logbox:logger:performance";
In a handler or interceptor it is even easier. You can do the same injections to create named loggers, but there is also a preconfigured logger called log
so you can just start logging by calling
log.info("Hi there");
log.fatal("Terrible crash!");
So now you know how to start logging I will explain some concepts and some stuff which is confusing or unclear in the Logbox manual.
Continue reading
Recent Comments