XMLMill - convert xml to pdf with java. Generate PDF from xml/xsl.

XMLMill User Guide

Version: 3.00 Date: January 2nd, 2008
This tutorial is opened in a separate window in order to maximize the legibility of this tutorial.
To return to XMLMill, close this browser window

This page as PDFPrinter friendly pageThis guide (!) as PDF

XMLMill Logging

In order to use XMLMill in any environment, XMLMill uses Apache's commons-logging-1.1 package. This is an ultra-thin bridge between different logging implementations. This facilitates using XMLMill with any logging implementation at runtime.

This bridge is based on using a wrapper class around the logging implementation your environment uses so logmessages can be passed from XMLMill to the underlying logging implementation.

This wrapper class must implement the org.apache.commons.logging.Log interface.

XMLMill supports the loglevels as defined by Apache's commons-logging-1.1 package.

LogLevels

All messages have a loglevel, corresponding to 6 levels as defined in the org.apache.commons.logging.Log class:

  1. TRACE
  2. DEBUG
  3. INFO
  4. WARN
  5. ERROR
  6. FATAL

Following messages correspond to following loglevels:

DEBUG or TRACE

  • Comments between <comment>... </comment> elements.
  • StackTraces generated by javax.xml.transform.TransformerException or org.xml.sax.SAXException exceptions.

WARN

  • com.xmlmill.XMLMillWarning messages.
  • javax.xml.transform.TransformerException (warning) messages.
  • org.xml.sax.SAXException (warning) messages.

ERROR

  • com.xmlmill.XMLMillError messages.
  • javax.xml.transform.TransformerException (error) messages.
  • org.xml.sax.SAXException (error) messages.

FATALERROR

  • com.xmlmill.XMLMillFatalError messages.
  • javax.xml.transform.TransformerException (fatalerror) messages.
  • org.xml.sax.SAXException (fatalerror) messages.

INFO

  • All other messages.
  • The levels are hierarchical defined. Following rule applies: TRACE< DEBUG < INFO < WARN < ERROR < FATAL. Only output messages that are of a level greater than or equal to the level set will be logged. If the level is set at INFO, alle messages will be logged accept the messages with level DEBUG.

Default Logging behavior

If no logfile or logger instance is defined, the default logging will apply:

  • All logmessages are send to the standard error stream (System.err).
  • All logmessages with a level of at least INFO will logged.
  • A default layout will be used.

A .log file could look like this:

images/log.gif
Select to enlarge

The default logging will contain following information:

  1. The log level.
  2. The thread-name.
  3. The log message.

Logging implementations (examples)

The following examples shows how to use XMLMill with different logging implementations.

Following must be done:

1

Define the classes of the underlying logging implementation.

2

Define the Apache's commons logging class acting as a wrapper class against the underlying log implementation (not applicable if you use the com.xmlmill.log.FileLogger implementation).

3

Instantiate the wrapper class.

4

Define the log level.

5

Pass the wrapper to the Configurator, so XMLMill can use it.

Example: Using the com.xmlmill.log.FileLogger

The following example shows how to use the build-in com.xmlmill.log.FileLogger class.

[001] 
[002] package com.xmlmilltest;
[003] 
[004] import com.xmlmill.*;
[005] import com.xmlmill.config.Configurator;
[006] 
[007] // The FileLogger class needed 
[008] import com.xmlmill.log.FileLogger;
[009] 
[010] // The Apache commons logging class needed 
[011] // import org.apache.commons.logging.impl.Log4JLogger;
[012] 
[013] import java.io.*;
[014] 
[015] public class TestAppl extends Object {
[016] 
[017] public static void main(String[] args) {
[018]   JAXPTransformer transform;
[019]   FileLogger logger = null;
[020]   
[021]   try {
[022]     ////////////////////////////////////////////////////
[023]     // DEFINE FIRST THE LOGGER INSTANCE TO USE        //
[024]     // HERE DEFAULT com.xmlmill.log.FileLogger        //
[025]     // (you can use any class which implements the    //
[026]     // org.apache.commons.logging.Log interface)      //
[027]     ////////////////////////////////////////////////////
[028] 
[029]     // Use the default FileLogger class 
[030]     logger = new FileLogger("xmlmill");
[031] 
[032]     // Set logfile
[033]     logger.setFileName("file:/C:/xmlmill3.00/testtransform.txt", false);
[034] 
[035]     // Set the loglevel 
[036]     logger.setLevel("DEBUG");
[037] 
[038]     // Pass the logger to the XMLMill Configurator, so XMLMill can use 
          it.
[040]     Configurator.setLogger(logger); 
[041] 
[042]     //////////////////////////////////////////////
[043]     // INITIALIZE THE CONFIGURATOR              //
[044]     //////////////////////////////////////////////    
[045] 
[046]     // Set Configurator 
[047]     Configurator.configure();
[048] 
[049]     //////////////////////////////////////////////
[050]     // CONTINUE                                 //
[051]     //////////////////////////////////////////////
[052] 
[053]     
[054]     // Remaining code
[055]     ...
[056]     
[057]   } catch (Throwable e) {
[058]     System.out.println(e.toString());
[059]   } finally {
[060]     // Close the logger instance
[061]     logger.finalize();
[062]     
[063]     // Null out instances
[064]     transform = null;
[065]     logger    = null;
[066]     System.out.println("-- Done --");    
[067]   }
[068] }
[069] }
[070]  
[071]     

  • In case you want to use your own logging implementation, you need to write your own wrapper class implementing the org.apache.commons.logging.Log class.
  • In case no wrapper is passed to the Configurator before calling the Configurator.configure() method, all log messages are send to the System.err. stream.

Example: Using the log4j package: org.apache.log4j

The following example shows how to use the log4j logging mechanism.

[001] 
[002] package com.xmlmilltest;
[003] 
[004] import com.xmlmill.*; 
[005] import com.xmlmill.config.Configurator;
[006] 
[007] 
[008] // The Log4J classes needed 
[009] import org.apache.log4j.Level;
[010] import org.apache.log4j.Logger;
[011] import org.apache.log4j.PatternLayout;
[012] import org.apache.log4j.FileAppender;
[013] import org.apache.log4j.ConsoleAppender;
[014] 
[015] // The Apache commons logging class needed 
[016] import org.apache.commons.logging.impl.Log4JLogger;
[017] 
[018] import java.io.*;
[019] 
[020] public class TestAppl extends Object {
[021] 
[022] public static void main(String[] args) {
[023]   JAXPTransformer transform;
[024]   
[025]   try {
[026]     //////////////////////////////////////////////
[027]     // DEFINE FIRST THE LOGGER INSTANCE TO USE  //
[028]     //        HERE LOG4J LOGGER                 //
[029]     //////////////////////////////////////////////
[030] 
[031]     // Get a wrapper instance around a Log4J logger instance 
[032]     Log4JLogger wrapper = new Log4JLogger("xmlmill");
[033] 
[034]     // Set the loglevel 
[035]     wrapper.getLogger().setLevel(Level.INFO);
[036] 
[037]     // Define pattern/formatter 
[038]     PatternLayout  patternlayout = new PatternLayout("%-5p [%t]: %m%n");
[039] 
[040]     // Set Appender 
[041]     wrapper.getLogger().addAppender(new FileAppender(patternlayout, 
          ("%h/mylog.txt", append));
[043] 
[044]     // Pass the logger to the XMLMill Configurator, so XMLMill can use 
          it.
[046]     Configurator.setLogger(wrapper); 
[047] 
[048]     //////////////////////////////////////////////
[049]     // INITIALIZE THE CONFIGURATOR              //
[050]     //////////////////////////////////////////////
[051]       
[052]     // Define a configurator (optional)
[053]     Configurator.configure();
[054]     
[055]     // Remaining code
[056]     ...
[057]     
[058]   } catch (Throwable e) {
[059]     System.out.println(e.toString());
[060]   } finally {
[061]     transform = null;
[062]     System.out.println("-- Done --");    
[063]   }
[064] }
[065] }
[066]  
[067]     

  • Apache's commons.logging implementation has some wrapper classes defined for the most used log implementations. Check the org.apache.commons.logging.impl package for more information.
  • In case you want to use your own logging implementation, you need to write your own wrapper class implementing the org.apache.commons.logging.Log class.
  • Check also XMLMill's com.xmlmill.log package containing 2 wrapper classes (the FileLogger and SimpleLogger classes).
  • In case no wrapper is passed to the Configurator before calling the Configurator.configure() method, all log messages are send to the System.err. stream.

Example: Using the JDK 1.4 package: java.util.logging

The following example shows how to use the java.util.logging logging mechanism.

[001] 
[002] package com.xmlmilltest;
[003] 
[004] import com.xmlmill.*; 
[005] import com.xmlmill.config.Configurator;
[006] 
[007] 
[008] // The JDK 1.4 Logging classes needed
[009] import java.util.logging.Level;
[010] import java.util.logging.Logger;
[011] import java.util.logging.FileHandler;
[012] 
[013] // The Apache commons logging class needed
[014] import org.apache.commons.logging.impl.Jdk14Logger;
[015] 
[016] import java.io.*;
[017] 
[018] public class TestAppl extends Object {
[019] 
[020] public static void main(String[] args) {
[021]   JAXPTransformer transform;
[022]   
[023]   try {
[024]     //////////////////////////////////////////////
[025]     // DEFINE FIRST THE LOGGER INSTANCE TO USE  //
[026]     //        HERE JDK 1.4 LOGGER               //
[027]     //////////////////////////////////////////////
[028] 
[029]     // Get a wrapper instance around a JDK1.4 logger instance
[030]     Jdk14Logger wrapper = new Jdk14Logger("xmlmill");
[031] 
[032]     // Get the underlying JDK1.4 logger instance and set loglevel 
[033]     wrapper.getLogger().setLevel(Level.ALL);
[034]  
[035]     // Define a handler
[036]     FileHandler f2 = new 
          FileHandler("c:\\xmlmill3.00\\testtransform.txt");
[038]     f2.setFormatter(new SimpleFormatter());
[039]     wrapper.getLogger().addHandler(f2);
[040] 
[041]     // Pass the logger to the XMLMill Configurator, so XMLMill can use 
          it.
[043]     Configurator.setLogger(wrapper);
[044] 
[045]     //////////////////////////////////////////////
[046]     // INITIALIZE THE CONFIGURATOR              //
[047]     //////////////////////////////////////////////
[048]       
[049]     // Define a configurator (optional)
[050]     Configurator.configure();
[051]     
[052]     // Remaining code
[053]     ...
[054]     
[055]   } catch (Throwable e) {
[056]     System.out.println(e.toString());
[057]   } finally {
[058]     transform = null;
[059]     System.out.println("-- Done --");    
[060]   }
[061] }
[062] }
  • In case you want to use your own logging implementation, you need to write your own wrapper class implementing the org.apache.commons.logging.Log class.
  • Check also XMLMill's com.xmlmill.log package containing 2 wrapper classes (the FileLogger and SimpleLogger classes).
  • In case no wrapper is passed to the Configurator before calling the Configurator.configure() method, all log messages are send to the System.err. stream.
Copyright © 2001 - 2012. All rights reserved. XMLMill and XMLMill logo are trademarks of Pecunia Data Systems, bvba.
Powered by Apache CocoonPowered by XMLMill