我正在http://jooq.sourceforge.net维护一个用于数据库访问的库,我想使用log4j进行日志记录。但是日志记录确实是我库中非常可选的部分。如果log4j在客户端代码中不可用,那么我将不会进行日志记录。所以我创建了一个这样的记录器代理:
public final class JooqLogger {
// Initialise only once
private static boolean initialisationError = false;
// The log4j Logger reference
private Logger logger;
// Get the logger proxy for a class
public static JooqLogger getLogger(Class<?> clazz) {
JooqLogger result = new JooqLogger();
try {
result.logger = Logger.getLogger(clazz);
}
// Log4j is not found on the classpath, so ignore most of logging
catch (Throwable t) {
if (!initialisationError) {
initialisationError = true;
result.error("JooqLogger could not initialise log4j logger...");
}
}
return result;
}
// All methods from log4j Logger are available as well.
// They provide redirection and ignore all calls if log4j is not available.
public boolean isTraceEnabled() {
if (logger != null) {
return logger.isTraceEnabled();
}
else {
return false;
}
}
public void trace(Object message) {
if (logger != null) {
logger.trace(message);
}
}
// [... etc ...]
}
我的问题是: