Examples Using JLog

Now that we've covered the basic concepts, it's time to demonstrate them with some examples. Since we have a lot of configuration detail to deal with, we have chosen to use the Ant build utility to run all our tasks. This way, we can set all necessary system, compilation, location, classpath, and run-time properties once and for all, and illustrate how to build the examples using simple calls to Ant. We will show here all the necessary target commands within a single build.xml script that enable us to build and run the examples. All operating system specifics should be abstracted into the build.xml file.

Note 

Ant is a Java program that automates the process of compiling and manipulating source code structures. To use these scripts, as well as the framework itself, you must obtain a copy of Ant from the Jakarta Apache Project (http://jakarta.apache.org/ant/index.html).

The first example shows how a LogEventListener implementation (LogWriter) is created and used in connection with a LogEventProducer, in this case, a Log object.

Before proceeding, download and install the code distribution for this chapter. To obtain the code distribution visit http://www.wrox.com. After installing the code you're ready to proceed and build the example.

Setting Build Properties

After installing Ant there are a couple of steps that are needed to use it with the build script build.xml included in the chapter. You need to change the <property> variable tags to match the appropriate settings and directories for your machine.

Our base directory will be C:\ProJMS\Chapter09, but the actual value of this is irrelevant, since everything we do will be relative to this base directory:

     <project name="jms_logging" default="framework" basedir="."> 

We will give the full set of properties here for the chapter. First we assign some values for the WebLogic server that we'll be using later:

       <property name="appserver" value="C:/bea/wlserver6.0/config/mydomain" />       <property name="apphome" value="${appserver}/applications" />       <property name="confighome" value="${appserver}/logconfig" />       <property name="serverclasses" value="${appserver}/serverclasses" />       <property name="docs" value="./docs" /> 

Note how previous values can be substituted into new values. These are our principle directories:

       <property name="framework_build" value="./build" />       <property name="framework_src" value="./src/framework" />       <property name="jlog_examples_src" value="./src/examples/jlogexamples" />       <property name="jlog_examples_build" value="./build" />       <property name="framework_servlet_src"          value="./src/examples/frameworkServlet" />       <property name="framework_servlet_build" value="./servletbuild" />       <property name="framework_ejb_src" value="./src/examples/frameworkejb" />       <property name="framework_ejb_build" value="./ejbbuild" />       <property name="descriptorhome" value="./src" /> 

Now we set up the classpaths used for compiling and running the examples. You will need to check the values carefully corresponding to the location of the appropriate JAR files on your machine:

       <property name="jdbcapth" value="C:/Oracle/Ora81/jdbc/classes111.zip"/>       <property name="xmlparser"           value="C:/jaxp-1.1ea2/jaxp.jar;C:/jaxp-1.1ea2/crimson.jar"/>       <property name="jlogpath" value="C:/jlog/jlog_1_01.jar"/>       <property name="antpath" value="C:/ant/lib/ant.jar"/>       <property name="weblogicjar" value="C:/bea/wlserver6.0/lib/weblogic.jar"/>       <property name="framework_classpath"          value="${xmlparser};${jlogpath} ; ${weblogicjar};${jdbcpath}" />       <property name="framework_examples_classpath"          value="${framework_build};${framework_classpath}" />       <property name="framework_servlet_classpath"          value="${framework_build};${framework_classpath}" />       <property name="framework_ejb_classpath"          value="${framework_build};${framework_classpath}" /> 

You will need a recent copy of the JAXP release obtainable from http://java.sun.com/xml, which contains the jaxp.jar and crimson.jar libraries for XML parsing. Note that we are using Oracle database driver classes here. This is not essential. If you substitute the classpath for another suitable JDBC driver, you will be able to run the database logging service later, but the use of a database is an incidental part of the chapter, and merely illustrates further uses of the logging service.

We will continue showing sections of the build.xml script as we need them.

Now let's take a look at the source code for a simple logging example:

    package examples.jlogexamples.simple;    import com.jtrack.examples.AbstractExample;    import com.jtrack.jlog.implementation.Log;    import com.jtrack.jlog.implementation.LogOutputStream;    import com.jtrack.jlog.implementation.LongLogFormatter;    /**     * This example shows how to log to System.out using     * a LogOutputStream and the Log class.     */    public class SimpleLogExample {       public static void main(String[] args) {          new SimpleLogExample().runExample();       } 

Use the static global log provided by the Log class to log messages and add a LogOutputStream to the global log so log messages are printed out to System.out:

       protected void runExample() {          // create a LogOutputStream which listens to the global          // log and prints out to System.out in a long format          LogOutputStream logListener = new LogOutputStream(             Log.global, System.out, new LongLogFormatter());          // log a few messages          Log.global.debug( "A debug message", "From SimpleLogExample" );          Log.global.warn ( "A warning message", "From SimpleLogExample" );          Log.global.trace( "A trace message", "From SimpleLogExample" );       }    } 

Looking at the runExample() method, the first thing we do is to create a new LogOutputStream, which is our listener, and also a type of LogWriter. There are three arguments we need to pass to the LogOutputStream constructor. The first parameter we pass is our LogEventProducer, in this instance Log.global.

Log.global is a globally available static Log instance that can be used for logging. It is for convenience and we will use it in our example so that we don't need to create a Log instance ourselves, although there is nothing stopping you from creating a Log instance yourself and using it. This LogWriter implementation is one that is used to write to output streams, so the next parameter we pass is an output stream, System.out, for it to log messages to. Finally a Formatter object is needed so that LogOutputStream knows how to format the messages when it logs them to the output stream.

There are a few different types of formats that you can choose from when logging messages; in our example we will use the "Long" format that is part of the JLog package. If you desire a format that doesn't exist within JLog you can create a formatter of your own simply by implementing the ILogFormatter interface. For more information about the types of formats that are available, refer to the JLog JavaDocs.

Compiling and Running

To run the code, we first need to compile it. The following dependency chain of <target> commands achieves this. First we create the necessary target directories for the compiled code:

       <target name="frameworkprepare">         <mkdir dir="${framework_build}" />         <mkdir dir="${framework_servlet_build}" />         <mkdir dir="${framework_ejb_build}" />         <mkdir dir="${confighome}" />         <copy todir="${confighome}">           <fileset dir="config"/>         </copy>       </target> 

Now we compile the source code for the logging framework into the build directory:

       <target name="framework" depends="frameworkprepare">         <javac           srcdir="${framework_src}"           destdir="${framework_build}"           classpath="${framework_classpath}"         />       </target> 

Note the classpath we specified above in the attributes of the compile instruction. This will compile our example classes into the build\examples directory:

       <target name="examples" depends="framework">         <javac           srcdir="${jlog_examples_src}"           destdir="${jlog_examples_build}"           classpath="${framework_classpath}"         />       </target> 

Finally, we can run the SimpleLogExample class:

       <target name="SimpleLogExample" depends="examples">         <java           classname="examples.jlogexamples.simple.SimpleLogExample"           fork="yes"           classpath="${framework_examples_classpath}"         />       </target> 

Bring up a command window, set your current directory to base of the code installation, and execute ant SimpleLogExample. You should see an output similar to that below:

click to expand

As you can see, the simple example printed out three separate log messages to System.out. Each log entry contains the Log name (Global System Log), Entry name (none), Timestamp for the message, Severity (Debug, Warning, or Trace in this example), Title, executing thread (main), the Source code line that logged the message, and the text contents of the message.



Professional JMS
Professional JMS
ISBN: 1861004931
EAN: 2147483647
Year: 2000
Pages: 154

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net