User's Manual

46 Chapter 7. Developing with WAF
package foo.bar;
import org.apache.log4j.Logger;
public class Baz {
private final static Logger s_log = Logger.getLogger(Baz.class);
}
Example 7-7. Standard way of instantiating loggers
This instantiates a singleton logger named foo.bar.Baz. Note, however, that assigning the logger
to a private static variable is merely a matter of syntactic convenience. If you ever need to, you can
obtain the same logger from anywhere else in your code.
package foo.frob;
import org.apache.log4j.Logger;
class Nosey {
void doStuff() {
Logger logger = Logger.getLogger("foo.bar.Baz");
logger.debug("got the logger used by the Baz class.");
}
}
Example 7-8. Random access to loggers
There is a couple reasons why this might be useful.
1. This allows you to adjust the logging level of any logger at runtime. If you have a production
system that starts exhibiting erratic behavior, you can turn up the logging level in selected classes
without shutting down and restarting the system. This is what Developer Support allows you
to do.
2. There are times when you want to see what goes on in the class Foo when you call a method
reliant on Foo elsewhere in your code.
To elaborate on the last point: Consider the logging output generated by the
com.arsdigita.db.PreparedStatement logger. If the logging level for this logger is set to
info, it will log every executed query and its bind variable values. If you are interested in seeing the
query your code generates, you can set this logger’s level to info in your config file. Note, however,
that this will result in all queries being logged system-wide. That may be many more than you
require. The alternative is to adjust the logging level at runtime:
// assume all the necessary imports
class Frobnivator {
void frobnivate() {
Logger logger = Logger.getLogger("com.arsdigita.db.PreparedStatement");
Level old = logger.getLevel();
logger.setLevel(Level.INFO);
doSomething();
logger.setLevel(old);
}