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

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

Java: log4j Initialization Example

// 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.");
   }
 }

Log4j 101

// Once you have included the Log4j jar into the classpath of your
// application, you can use the following code within each class to
// get a logger for that class. That is, all log messages issued from
// within that class will be tagged with the classname to make it
// easier to locate a problem. See my tutorial on Log4j,
// Log4j in 30 Minutes or Less (http://www.johnmunsch.com/projects/Presentations/)
// for a better introduction.

private static Logger log = Logger.getLogger(<class name>.class);


// When you need to specify the location (or a different name) for
// the log.properties file, you can set a specific System variable
// which Log4j will be guaranteed to read as soon as any part of
// your code tries to use it. You do that by passing the following
// to the JVM as you start your Java application.
-Dlog4j.configuration=file:<path to log.properties file>

Configure log4j to log info level to xml file



 <!-- log info level into log.xml -->
 <appender name="XMLOut" class="org.apache.log4j.FileAppender"> 
        <param name="File" value="log.xml"/>  
        <param name="Threshold" value="info"/> 
        <layout class="org.apache.log4j.xml.XMLLayout"/>
    </appender> 

log4j.properties: eclipse log4j HOWTO in 10 minutes

// How to use log4j within eclipse in 10 minutes


# HOW TO USE LOG4J WITHIN ECLIPSE IN 10 MINUTES
# by Daniel Gonzalez Gasull gasull[at]gmail[dot]com
#
# 1) Download log4j http://logging.apache.org/site/binindex.cgi
# 2) Unpack the .zip file in your Java folder (In Windows it is usually
# C:\Program Files\Java\)
# 3) In Eclipse: Window - Preferences... - Java - Build Path - User Libraries -
# New - write "log4j" - OK - Add JARs... - navigate to find your log4j .jar you just
# unpacked in the Java Folder - OK
# 4) right click on your project in the Package Explorer - New - Folder -
# in "Folder name" write "log4j" - click Advanced - select "Link to a folder in the
# file system" - create a new folder "log4j" in your project folder in the file system
# 5) Place this file you are reading right now in in the folder you just created. Name
# the file as log4j.properties
# 6) In Eclipse: Run - Run... - In the navigation bar on the left select either the
# server, or the runnable class, or the JUnit test you want to log with log4j -
# select the Classpath tab - User Entries - Advanced... - Add folders - OK - select
# the "log4j" folder you created in your project - OK
# 7) Repeat step 6 for other servers, runnable classes or JUnit tests you want to log
# 8) Change in the following line the "org.example.foo.bar" with whatever you want.
log4j.category.org.example.foo.bar=DEBUG
# 9) Add the following import to the Java Class you want to log:
# import org.apache.log4j.Logger;
# 10) Add this lines to the Java Class you want to log:
# /**
# * Log4j logger
# */
# static Logger log4j = Logger.getLogger("org.example.foo.bar");
# 11) Substitute in the code above "org.example.foo.bar" with whatever your wrote in
# in the step 8.
# 12) Add something like the following code in your Class whenever you want to log:
# log4j.debug("WTF?");
# 13) Repeat steps 9, 10, 11 and 12 for every Java Class you want to log
# 14) Enjoy!


log4j.rootCategory=DEBUG, R, O

# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender

# File
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log

# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB

# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout=org.apache.log4j.PatternLayout

log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n

Configure Log4J in a WebObjects app

(new DOMConfigurator()).doConfigure(resourceManager().inputStreamForResourceNamed("log4j.xml", "app", null), LogManager.getLoggerRepository());
« Newer Snippets
Older Snippets »
Showing 1-5 of 5 total  RSS