UPDATED ANSWER :
Sorry I didn't get the question right. My previous answer was completly wrong;
So your problem here is that you are instanciating the logger (with LoggerFactory
) with always the same name ApplicationLogger
.
There is no way from this logger to find the calling class of the AppLog
object because there is not reference to it. As you say, you can inject the logger name in each call but it will be hard to maintain.
What you can try is wrapping the Logger
class.
public final class AppLogger {
private Logger logger;
protected AppLogger(String loggerName) {
this.logger = LoggerFactory.getLogger(loggerName);
}
public static AppLogger getAppLogger(String loggerName) {
return new AppLogger(loggerName);
}
public void info(String... args) {
this.logger.info(LogsUtils.generateLogString(args));
}
...
}
In each class, do not use the LoggerFactory
but your own class.
private static final AppLogger APP_LOGGER = AppLogger.getLogger(CallingClass.class)
and then log like this
APP_LOGGER.info("message");
Then in your pattern layout use %c (and not %C) and it should work as expected !