Getting started
This page will describe the basic features of jLo.
1. Define the loggers
First of all we are going to define a set of loggers. In our example we will define 2 loggers. The first one is called "com.foo" and have all targets set. The second one is called "com.foo.application" and will have only the FATAL target set. Both loggers will share the same Generator called "LogFile". This generator will use the SimpleFormatter and will write everything to a file "/tmp/application.log".
<?xml version="1.0" ?>
<log-configuration>
<logger name="com.foo">
<targets>ALL</targets>
<generator-name>LogFile</generator-name>
</logger>
<logger name="com.foo.application">
<targets>FATAL</targets>
<generator-name>LogFile</generator-name>
</logger>
<generator name="LogFile">
<formatter class="SimpleFormatter"/>
<handler class="FileHandler">
<parameter name="file" value="/tmp/application.log" />
</handler>
</generator>
</log-configuration>
Save this file to a file called
jlo_logging.xml and make sure that it is in the classpath when you run the next little demo application.
2. Using a logger
In order to get a logger you have to call the method
getLogger(String loggerName)
from the LogManager. jLo will automatically search for a file called jlo_logging.xml in the classpath. If it cannot find the file it will use a default configuration with a SimpleFormatter and a ConsoleHandler.
You can also define a configuration name and use
getLogger(String loggerName,String configName)
jLo will try to find a file called ${configName}_logging.xml in the classpath otherwise return the default configuration. By this way you can have multiple log-configurations so that one application or part of an application will not overwrite some settings.
Here is a short code snippet that shows you how to log a statement:
import org.jzonic.jlo.*;
public class LoggingTest {
private static final Logger logger = LogManager.getLogger("com.foo");
public static main(String[] args) {
logger.info("Hello world!");
System.exit(0);
}
}
3. Defining the targets in the xml-file
jLo is different than other logging frameworks. Rather then defining a kind of threshold where a logger should start logging you define explicit which targets are on or off. There is one reserved keyword called "ALL". This means that all targets are on.
When you use the default jar file then jLo offers the following targets:
init,info,debug,warning,fatalIn order to switch off a target you put a ! in front of the target name.
Here are some examples:
<targets>ALL</targets> all targets are on
<targets>ALL,!INFO</targets> all targets except info are on
<targets>INIT,FATAL</targets> only the two targets init and fatal are on