A walk through all features


This page will walk through all features that jLo offers.

The basics


First of all we start with the basics of jLo. Create a file called demo_logging.xml and put in the following:

<?xml version="1.0" ?>
<log-configuration>
  
  <logger name="org.jzonic.jlo">    
    <targets>ALL</targets>
    <generator-name>TestGenerator</generator-name>
  </logger>    
  
  <generator name="TestGenerator">
    <formatter class="DefaultFormatter"/>
    <handler class="ConsoleHandler"/>    
  </generator>       
</log-configuration>

Make sure this file is in the classpath. We will use the log-configuration "demo" here to just demonstrate how easy it is to use multiple configurations. Now write the simple class that will log to all targets:

public class LoggingDemo {
        
    public LoggingDemo() {}
        
    public static void main(String[] args) {
        Logger logger = LogManager.getLogger("org.jzonic.jlo","demo");        
        logger.info("info output");
        logger.debug("debug output");            
        logger.trace("trace output");
        logger.warn("warn output");
        logger.error("error output");
        logger.fatal("fatal output");                    
    }    
}

When you run the demo you will get the following output:

2004/07/15 20:39:48 - [INFO] [org.jzonic.jlo.PermamentLogging] info output
2004/07/15 20:39:48 - [DEBUG] [org.jzonic.jlo.PermamentLogging] debug output
2004/07/15 20:39:48 - [TRACE] [org.jzonic.jlo.PermamentLogging] trace output
2004/07/15 20:39:48 - [WARN] [org.jzonic.jlo.PermamentLogging] warn output
2004/07/15 20:39:48 - [ERROR] [org.jzonic.jlo.PermamentLogging] error output
2004/07/15 20:39:48 - [FATAL] [org.jzonic.jlo.PermamentLogging] fatal output

Change the following line now in the demo_logging.xml to:

<targets>INFO,WARN,FATAL</targets>

Now you will get the following output:

2004/07/15 20:44:35 - [INFO] [org.jzonic.jlo.PermamentLogging] info output
2004/07/15 20:44:35 - [WARN] [org.jzonic.jlo.PermamentLogging] warn output
2004/07/15 20:44:35 - [FATAL] [org.jzonic.jlo.PermamentLogging] fatal output

Only the targets that are switched on will produce an output.
The last example for this is that we will switch off TRACE but switch on the rest:

<targets>ALL,!TRACE</targets>

When you run the demo you will get:

2004/07/15 20:46:49 - [INFO] [org.jzonic.jlo.PermamentLogging] info output
2004/07/15 20:46:49 - [DEBUG] [org.jzonic.jlo.PermamentLogging] debug output
2004/07/15 20:46:49 - [WARN] [org.jzonic.jlo.PermamentLogging] warn output
2004/07/15 20:46:49 - [ERROR] [org.jzonic.jlo.PermamentLogging] error output
2004/07/15 20:46:49 - [FATAL] [org.jzonic.jlo.PermamentLogging] fatal output

Everything but the trace outout.

One more logger


Now we will add another logger. You will see later on why. Here is the second one:

<logger name="org.jzonic">    
  <targets>ALL</targets>
  <generator-name>TestGenerator</generator-name>
</logger> 

This one will use the same generator. If you now change the following line:

Logger logger = LogManager.getLogger("org.jzonic.jlo","demo");        

to this:

Logger logger = LogManager.getLogger("org.jzonic","demo");        

and run the demo again you will get the same output as above. Not really suprising.

Adding channels


The next step is to include a channel which can extend the given targets for the loggers. Put in the following into the demo_logging.xml right beneath the logger definition:

<channel name="special">
  <mode>on</mode>
  <generator-name>TestGenerator</generator-name>
</channel> 

Now that we have the channel defined we can use it. Let us extend the demo from above:

public class LoggingDemo {
        
    public LoggingDemo() { }
    
    public static void main(String[] args) {        
        Channel channel = LogManager.getChannel("special","demo");        
        channel.log("channel output");
    }    
}

When you run this code now you will get the following output:

2004/07/15 20:51:21 - channel output

There are special channels build in. For every target there is also a channel defined which
is switched off by default. We will now switch on such a channel. Change the demo_logging.xml
so it will include a channel for the target "fatal":

<channel name="special">
  <mode>on</mode>
  <generator-name>TestGenerator</generator-name>
</channel> 
<channel name="fatal">
  <mode>on</mode>
  <generator-name>TestGenerator</generator-name>
</channel> 

In order to see something include the logger from the example above:

public class LoggingDemo {
        
    public LoggingDemo() { }
    
    public static void main(String[] args) {        
        Logger logger = LogManager.getLogger("org.jzonic","demo");       
        Logger logger2 = LogManager.getLogger("org.jzonic.jlo","demo");       
        Channel channel = LogManager.getChannel("special","demo");        
        logger.fatal("fatal output");
        logger2.fatal("fatal output");
        channel.log("channel output");
    }    
}

Now the output will be:

2004/07/15 21:11:02 - [FATAL] fatal output
2004/07/15 21:11:02 - [FATAL] [org.jzonic.jlo.PermamentLogging] fatal output
2004/07/15 21:11:02 - [FATAL] fatal output
2004/07/15 21:11:02 - [FATAL] [org.jzonic.jlo.PermamentLogging] fatal output
2004/07/15 21:11:02 - channel output

As you can see you get two entries which says fatal for every log request. The first one is from the logger and the second one is from the channel. Every log request with the target will be processed by this channel no matter which logger you are using.
This way you can for example write all error messages to a separate file using this channel.

The logpipe


The logpipe is in front of every logger or channel. Onc a logpipe is defined every log request will be processed by the logpipes. Here is the definition for such a logpipe:

<pipe>    
  <generator-name>PipeGenerator</generator-name>    
</pipe>

For the matter of difference we will use a different generator this time sou you actually see it. Put in
the following at the bottom of the file underneath the first generator:

<generator name="PipeGenerator">
  <formatter class="SimpleFormatter"/>
  <handler class="ConsoleHandler"/>    
</generator>    

We still write the log request to the console but we use a different formatter here.
Now let us run the same code as the very first example but with this log configuration. This will
produce the following output:

2004/07/15 21:01:32 - [INFO] info output
2004/07/15 21:01:32 - [INFO] [org.jzonic.jlo.PermamentLogging] info output
2004/07/15 21:01:32 - [DEBUG] debug output
2004/07/15 21:01:32 - [DEBUG] [org.jzonic.jlo.PermamentLogging] debug output
2004/07/15 21:01:32 - [TRACE] trace output
2004/07/15 21:01:32 - [TRACE] [org.jzonic.jlo.PermamentLogging] trace output
2004/07/15 21:01:32 - [WARN] warn output
2004/07/15 21:01:32 - [WARN] [org.jzonic.jlo.PermamentLogging] warn output
2004/07/15 21:01:32 - [ERROR] error output
2004/07/15 21:01:32 - [ERROR] [org.jzonic.jlo.PermamentLogging] error output
2004/07/15 21:01:32 - [FATAL] fatal output
2004/07/15 21:01:32 - [FATAL] [org.jzonic.jlo.PermamentLogging] fatal output