2011年8月29日 星期一

[Java] Using Log4j in Eclipse

Step by step:
  1. Get Log4j: Download Apache log4j from http://logging.apache.org/log4j/. Apache log4j has three development branches: a stable branch, 1.2; a discontinued branch, 1.3; and an experimental branch, 2.0.

  2. Setup: Unzip the file in the dedicated directory. log4j-x.y.z.jar should be in log4j directory.

  3. Use Log4j: There are two examples in log4j directory:
    • Trivial.java for a trivial usage example.
    • Sort.java and SortAlgo.java to for a slightly more advanced example.
    We take Trivial.java for example. First create a new project. Then add log4j-x.y.z.jar into libraries:
    Right click the project in Package Explorer. Then click Properties > Java Build Path > Libraries > Add External JARs...
    Now we add Main.java and Trivial.java classes: In Main.java:
    public class Main {
        public static void main(String[] args) {
            Trivial.run(args);
        }
    }
    In Trivial.java, we change main() method's name to run(). Run:
    Trivial class should output four lines.
Enjoy!

沒有留言:

張貼留言