public LoggerRepository getLoggerRepository()
{
final ClassLoader ccl = Thread.currentThread().getContextClassLoader();
LoggerRepository repository = this.repository.get(ccl == null ? NO_CCL_CLASSLOADER : ccl.hashCode());
if (repository == null)
{
final RootLogger root = new RootLogger(Level.INFO);
repository = new Hierarchy(root);
try
{
ConfigWatchDog configWatchDog = null;
if (ccl instanceof MuleApplicationClassLoader)
{
MuleApplicationClassLoader muleCL = (MuleApplicationClassLoader) ccl;
// check if there's an app-specific logging configuration available,
// scope the lookup to this classloader only, as getResource() will delegate to parents
// locate xml config first, fallback to properties format if not found
URL appLogConfig = muleCL.findResource("log4j.xml");
if (appLogConfig == null)
{
appLogConfig = muleCL.findResource("log4j.properties");
}
final String appName = muleCL.getAppName();
if (appLogConfig == null)
{
// fallback to defaults
String logName = String.format("mule-app-%s.log", appName);
File logDir = new File(MuleContainerBootstrapUtils.getMuleHome(), "logs");
File logFile = new File(logDir, logName);
DailyRollingFileAppender fileAppender = new DailyRollingFileAppender(new PatternLayout(PATTERN_LAYOUT), logFile.getAbsolutePath(), "'.'yyyy-MM-dd");
fileAppender.setAppend(true);
fileAppender.activateOptions();
root.addAppender(fileAppender);
}
else
{
configureFrom(appLogConfig, repository);
if (appLogConfig.toExternalForm().startsWith("file:"))
{
// if it's not a file, no sense in monitoring it for changes
configWatchDog = new ConfigWatchDog(muleCL, appLogConfig.getFile(), repository);
configWatchDog.setName(String.format("[%s].log4j.config.monitor", appName));
}
else
{
if (logger.isInfoEnabled())
{
logger.info(String.format("Logging config %s is not an external file, will not be monitored for changes", appLogConfig));
}
}
}
}
else
{
// this is not an app init, but a Mule container, use the top-level defaults
File defaultSystemLog = new File(MuleContainerBootstrapUtils.getMuleHome(), "conf/log4j.xml");
if (!defaultSystemLog.exists() && !defaultSystemLog.canRead())
{
defaultSystemLog = new File(MuleContainerBootstrapUtils.getMuleHome(), "conf/log4j.properties");
}
configureFrom(defaultSystemLog.toURL(), repository);
configWatchDog = new ConfigWatchDog(ccl, defaultSystemLog.getAbsolutePath(), repository);
configWatchDog.setName("Mule.system.log4j.config.monitor");
}
final LoggerRepository previous = this.repository.putIfAbsent(ccl == null ? NO_CCL_CLASSLOADER : ccl.hashCode(), repository);
if (previous != null)
{
repository = previous;
}