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

Java: log4j Initialization Example (See related posts)

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

 import com.foo.Bar;

 // Import log4j classes.
 import org.apache.log4j.Logger;
 import org.apache.log4j.BasicConfigurator;

 public class MyApp {

   // Define a static logger variable so that it references the
   // Logger instance named "MyApp".
   static Logger logger = Logger.getLogger(MyApp.class);

   public static void main(String[] args) {

     // Set up a simple configuration that logs on the console.
     BasicConfigurator.configure();

     logger.setLevel(Level.DEBUG); // optional if log4j.properties file not used
     // Possible levels: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL

     logger.info("Entering application.");
     Bar bar = new Bar();
     bar.doIt();
     logger.info("Exiting application.");
   }
 }

You need to create an account or log in to post comments to this site.


Click here to browse all 4860 code snippets

Related Posts