If you are building a new system, stick with Webel's answer.
If you have an existing system you are migrating to log4j2, you should still probably run the generate methods (but I include a minimal working class below), and you can add this function which provides the old 1.2 callerFQCN way of doing things (sort of):
public void log(Class ignoreClassFQCN, Level level, Marker marker, String msg, Throwable throwable){
logger.logIfEnabled(ignoreClassFQCN.getName(), level, marker, msg, throwable);
}
Then, from your existing log-wrapping classes, you can do something like this:
// inside ThisClass.java, denoting an old logger such as one that used log4j 1.2 with the callerFQCN parameter, or just an old logger that's naive and custom built.
private static final MyLog4j2WrapperClass newLogger = MyLog4j2WrapperClass.create();
public static void debug(String message) {
// basic example
newLogger.log(ThisClass.class, Level.DEBUG, null, message, null);
}
public static void logFailure(String message) {
// example of using a custom log level
newLogger.log(ThisClass.class, MyLog4j2WrapperClass.FAILURE, null, message, null);
}
I happened to delete a bunch of other generated functions I am not using, since I planned to do a simple shim for a poorly designed (custom) logging system. I deleted a bunch of create() methods I didn't plan to use. So here is a working class (the parts worth sharing):
public final class MyLog4j2WrapperClass extends ExtendedLoggerWrapper {
private static final long serialVersionUID = 1L;
private final ExtendedLoggerWrapper logger;
private static final String FQCN = MyLog4j2WrapperClass.class.getName();
public static final Level FAILURE = Level.forName("FAILURE", 150);
public void log(Class ignoreClass, Level level, Marker marker, String msg, Throwable throwable){
logger.logIfEnabled(ignoreClass.getName(), level, marker, msg, throwable);
}
private MyLog4j2WrapperClass(final Logger logger) {
super((AbstractLogger) logger, logger.getName(), logger.getMessageFactory());
this.logger = this;
}
/**
* Returns a custom Logger with the name of the calling class.
*
* @return The custom Logger for the calling class.
*/
public static MyLog4j2WrapperClass create() {
final Logger wrapped = LogManager.getLogger();
return new MyLog4j2WrapperClass(wrapped);
}
I should note that I consider this a shim--it's something to get you by, but I would strongly recommend continuing to move away from the old logging system once you have the shim in place. This will let you do more incremental code changes without having to accomplish a full migration right away.