We could generate and exception, get he stacktrace, and find the classloader of the calling class, hoping that would have logback.xml in it. But that class might be a utility class (hibernate, spring), not an application class, so might not work.
Maybe you just need to manually init logback from code.
I do this in a ServletContextListener which is registered in web.xml.
This is my version, but you could put any logic in there, like finding the full path of logback.xml...
import ch.qos.logback.classic.LoggerContext;
import org.slf4j.ILoggerFactory;
import org.slf4j.LoggerFactory;
import org.slf4j.bridge.SLF4JBridgeHandler;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
/**
* Initialise logging at application start up.
*/
public class InitLoggingListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent servletContextEvent) {
if (System.getProperty("logback.configurationFile") == null) {
System.setProperty("logback.configurationFile", "myapp.logback.xml");
}
// Optionally remove existing handlers attached to j.u.l root logger,
// including console handler
SLF4JBridgeHandler.removeHandlersForRootLogger();
// Bridge java.util.logging into SLF4J, and thus logback
SLF4JBridgeHandler.install();
}
@Override
public void contextDestroyed(ServletContextEvent servletContextEvent) {
shutdownLogging();
}
/**
* Flush and close all logging files
*/
privatestatic void shutdownLogging() {
// We do a logback specific check here, but shouldn't fail if// we ever move away from logback to another slf4j logging
// implementation
ILoggerFactory loggerFactory = LoggerFactory.getILoggerFactory();
if (loggerFactory instanceof LoggerContext) {
LoggerContext context = (LoggerContext) loggerFactory;
context.stop();
}
}
}
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira
ThisadeeP, how can this be fixed in logback?
We could generate and exception, get he stacktrace, and find the classloader of the calling class, hoping that would have logback.xml in it. But that class might be a utility class (hibernate, spring), not an application class, so might not work.
Maybe you just need to manually init logback from code.
I do this in a ServletContextListener which is registered in web.xml.
This is my version, but you could put any logic in there, like finding the full path of logback.xml...