我已经设法用 tomcat/Spring logback 放置了一个外部配置文件
tomcat 的上下文文件:
<?xml version='1.0' encoding='utf-8'?>
<Context path="/appName">
<Environment name="pathCfg" value="${user.home}/applications/appName/cfg" type="java.lang.String" override="false" />
</Context>
网页.xml:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:/web-application-context.xml
</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Listener for external logback configuration file -->
<listener>
<listener-class>com.package.client.servlet.LogbackConfiguratorContextListener</listener-class>
</listener>
web-application-context.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">
<!-- Configuration -->
<jee:jndi-lookup id="configurationPath" jndi-name="pathCfg"></jee:jndi-lookup>
<bean id="configurationPathResource" class="org.springframework.core.io.FileSystemResource">
<constructor-arg ref="configurationPath" />
</bean>
<util:properties id="environnementProperties" location="file:${java:comp/env/pathCfg}/environnement.properties" />
</beans>
logback 配置器类:
package com.package.client.servlet;
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.web.context.support.WebApplicationContextUtils;
import ch.qos.logback.classic.BasicConfigurator;
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.util.ContextInitializer;
import ch.qos.logback.core.joran.spi.JoranException;
/**
* Servlet context listener in order to use an external configuration file for logback.
*
*/
public class LogbackConfiguratorContextListener implements ServletContextListener
{
private static final Logger LOGGER = LoggerFactory.getLogger(LogbackConfiguratorContextListener.class);
/**
* Logback configuration file uri.
*/
@Value("#{environnementProperties['logback.configuration.file.uri']}")
private String logbackConfigurationUri;
/**
* Configuration path resource for configuration files.
*/
@Value("#{configurationPathResource}")
private Resource configurationPathResource;
/**
* initialize logback with external configuration file.
*/
@Override
public void contextInitialized(ServletContextEvent servletContextEvent)
{
autowireThis(servletContextEvent.getServletContext());
Resource logbackConfigurationResource = null;
if (StringUtils.isNotBlank(logbackConfigurationUri))
{
if (StringUtils.containsAny(logbackConfigurationUri, new char[] { '/', '\\' }))
{
logbackConfigurationResource = new FileSystemResource(logbackConfigurationUri);
}
else if (configurationPathResource != null)
{
try
{
logbackConfigurationResource = new FileSystemResource(new File(configurationPathResource.getFile(),
logbackConfigurationUri));
}
catch (IOException exception)
{
LOGGER.error("Can't read resource " + configurationPathResource.getFilename(), exception);
}
}
}
if (logbackConfigurationResource != null)
{
if (logbackConfigurationResource.exists())
{
LOGGER.info("Found logback configuration " + logbackConfigurationResource.getDescription()
+ " - Overriding default configuration");
JoranConfigurator configurator = new JoranConfigurator();
LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
loggerContext.reset();
configurator.setContext(loggerContext);
try
{
configurator.doConfigure(logbackConfigurationResource.getFile());
LOGGER.info("default configuration overrided by logback configuration "
+ logbackConfigurationResource.getDescription());
}
catch (Exception exception)
{
try
{
new ContextInitializer(loggerContext).autoConfig();
}
catch (JoranException e)
{
BasicConfigurator.configureDefaultContext();
LOGGER.error("Can't configure default configuration", exception);
}
LOGGER.error(
"Can't configure logback with specified " + logbackConfigurationResource.getDescription()
+ " - Keep default configuration", exception);
}
}
else
{
LOGGER.error("Can't read logback configuration specified file at "
+ logbackConfigurationResource.getDescription() + " - Keep default configuration");
}
}
else
{
LOGGER.info("No logback configuration file specified - Keep default configuration");
}
}
/**
* Autowire this bean by spring
* @param servletContext le contexte de la servlet
*/
private void autowireThis(ServletContext servletContext)
{
WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext).getAutowireCapableBeanFactory()
.autowireBean(this);
}
@Override
public void contextDestroyed(ServletContextEvent sce)
{
}
}
最后是 environment.properties
# Configuration file
logback.configuration.file.uri=C:/Users/olejacques/applications/appName/cfg/log.xml