using Log4j

Discussions

General J2EE: using Log4j

  1. using Log4j (4 messages)

    Hi, I'm new to log4j. I've been asked to go through an project already underway (100+ java files) and clean up the code and change System.out.println to logging statements using log4j. I've read about log4j, and can implement it on an individual class basis (e.g. create a static logger for each class, set level for each class), but I was wondering if there is a way to set the level for all loggers in a package, or the whole project? After reading about logger inheritence and configuration files, etc., it seems like it should be, but I'm not sure how to implement it. I've examined several examples, but the examples I found were for individual classes, not packages. I'm just learning log4j so any help, suggestions, or references would be very much appreciated. Thank you.

    Threaded Messages (4)

  2. using Log4j[ Go to top ]

    A common pattern for Log4J is to organize your logging categories by Java class. In fact, the Logger has a factory method supporting this:


    // In the MyClass class:
    Logger logger = Logger.getLogger(MyClass.class);

    // For non-static loggers:
    Logger logger = Logger.getLogger(this.getClass());


    If you consistently follow this approach, it is pretty easy to organize your logging around classes and the packages that contain them:


    # Logging settings for com.acme package:
    log4j.logger.com.acme=ERROR

    # Logging settings for com.acme.util package:
    log4j.logger.com.acme.util=WARN

    # Logging settings for com.acme.util.MyClass class:
    log4j.logger.com.acme.util.MyClass=DEBUG


    The most specific configuration takes precedence. Given the example above, all the classes in the com.acme.io package would use the settings for the com.acme package, and the com.acme.util.NonConfiguredClass would use the settings for the com.acme.util package. You can tweak your settings up and down, depending on which classes and packages you are currently debugging.
  3. static vs non-static loggers[ Go to top ]

    A common pattern for Log4J is to organize your logging categories by Java class. In fact, the Logger has a factory method supporting this:// In the MyClass class:Logger logger = Logger.getLogger(MyClass.class);// For non-static loggers:Logger logger = Logger.getLogger(this.getClass());
    I've read most people advice the use of a static logger for each class. Does it make much of a difference if loggers are static or not?
  4. static vs non-static loggers[ Go to top ]

    I've read most people advice the use of a static logger for each class. Does it make much of a difference if loggers are static or not?
    I agree with that advice. A static logger will use less resources, and therefore you will get better performance. The following syntax is generally preferable:

    Logger logger = Logger.getLogger(MyClass.class);

    There are some circumstances where you can do clever things with instance loggers (such as polymorphic logging), but as a rule, this is more for advanced users.
  5. RE:[ Go to top ]

    Thank you for your help! I appreciate it.

    Katie