Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Thiti V. Sintopchai http://thitiv.blogspot.com

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Java: log4j Initialization Example

// Initialization for Basic Console Output
// Ref: http://logging.apache.org/log4j/docs/manual.html

   1  
   2   import com.foo.Bar;
   3  
   4   // Import log4j classes.
   5   import org.apache.log4j.Logger;
   6   import org.apache.log4j.BasicConfigurator;
   7  
   8   public class MyApp {
   9  
  10     // Define a static logger variable so that it references the
  11     // Logger instance named "MyApp".
  12     static Logger logger = Logger.getLogger(MyApp.class);
  13  
  14     public static void main(String[] args) {
  15  
  16       // Set up a simple configuration that logs on the console.
  17       BasicConfigurator.configure();
  18  
  19       logger.setLevel(Level.DEBUG); // optional if log4j.properties file not used
  20       // Possible levels: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL
  21  
  22       logger.info("Entering application.");
  23       Bar bar = new Bar();
  24       bar.doIt();
  25       logger.info("Exiting application.");
  26     }
  27   }

Java DOM: Printing the Content of a Node

   1  
   2      try
   3      {
   4        // Set up the output transformer
   5        TransformerFactory transfac = TransformerFactory.newInstance();
   6        Transformer trans = transfac.newTransformer();
   7        trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
   8        trans.setOutputProperty(OutputKeys.INDENT, "yes");
   9  
  10        // Print the DOM node
  11  
  12        StringWriter sw = new StringWriter();
  13        StreamResult result = new StreamResult(sw);
  14        DOMSource source = new DOMSource(node);
  15        trans.transform(source, result);
  16        String xmlString = sw.toString();
  17  
  18        System.out.println(xmlString);
  19      }
  20      catch (TransformerException e)
  21      {
  22        e.printStackTrace();
  23      }
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS