public class SessionLogger {
private final String sessionId;
public SessionLogger(String sessionId) {
this.sessionId = sessionId;
}
public void info(Log log, String message, Object... args) {
log.info(formatMessage(message, args));
}
public void error(Log log, String message, Throwable t, Object... args) {
log.error(formatMessage(message, args), t);
}
private String formatMessage(String message, Object... args) {
for (int i = 0; i < args.length; i++) {
message = message.replaceFirst("\\{\\}", args[i].toString());
}
return String.format("SessionId [%s]: %s", sessionId, message);
}
}
What I want to do is to pass, Logger instance to the SessionLogger class and I would like to see the class name, where Logger was initialized.
public class A {
private static final Log log = LogFactory.getLog(A.class)
public void doIt() {
sessionLogger.info(log, "hello world");
}
}
I would expect to see class A in the log message instead of SessionLogger:
2013-10-07 00:29:27,328 INFO [main] (SessionLogger.java:17) - SessionId [123]: Hello world
I've commons-logging.jar and log4j-1.2.16.jar in classpath. Logger is an instance of org.apache.commons.logging.Log
Update
Just released that it is expected behavior, cause Logger
logs the line of the code, where log method was invoked. So it should be done another way somehow