我尝试为每个webapp使用不同的日志。我在同一台服务器中复用应用程序,我需要从属性文件中加载此目录。
每个网络应用加载一个包含此键/值的属性文件(X是应用程序的名称,所有人都不同):
app=APP.X
logPath=C://logs
我在每个webapp项目中添加了一个log4j.properties文件,除了log的路径(log4j.properties)之外我还需要所有参数
# Root logger option
log4j.rootLogger=INFO, stdout, file
# Redirect log messages to console
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%M:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.DailyRollingFileAppender
log4j.appender.file.encoding=UTF-8
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c:%M:%L - %m%n
#log4j.appender.file.MaxBackupIndex=8
log4j.appender.file.DatePattern='.'yyyy-MM
我在我的util jar中创建了一个Java类,它实现了ApplicationListener并添加了键log4j.appender.file.File(Log4jContextListener.java)
public class Log4jContextListener implements ApplicationListener<ContextStartedEvent>
{
@Value("${rutaLogs}")
private String rutaLogs;
@Value("${app}")
private String app;
private Logger log = Logger.getLogger(getClass());
@Override
public void onApplicationEvent(ContextStartedEvent arg0)
{
Properties props = new Properties();
InputStream strm = null;
try
{
System.out.println("LOADING APP: " + app);
strm = Log4jContextListener.class.getClassLoader().getResourceAsStream("log4j.properties");
props.load(strm);
if (props != null)
log.info("PROP: " + props.getProperty("log4j.appender.file.layout"));
props.put("log4j.appender.file.File", logPath+ app + File.separator + app + ".log");
}
catch (IOException propsLoadIOE)
{
throw new Error("can't load logging config file", propsLoadIOE);
}
finally
{
try
{
strm.close();
}
catch (IOException configCloseIOE)
{
throw new Error("error closing logging config file", configCloseIOE);
}
}
// props.put("webAppRoot", event.getServletContext().getRealPath("/"));
PropertyConfigurator.configure(props);
}
}
每个Web应用程序都会在applicationContext.xml
中添加一个带有此声明的bean<!-- Listener to initialize LOG4J -->
<bean id="log4jConfigurationListener" class="com.framework.listeners.Log4jContextListener" />
日志的文件夹和文件正在创建,但日志跟踪是混合的,每个webapp仅使用加载的最新位置。
我如何在每个wepapp上下文中做出不同的事情?
PD:我必须从属性中读取,我不能使用环境变量。答案 0 :(得分:0)
您好我设法使用以下内容为每个webapp上下文设置日志位置:
package ...
import org.apache.log4j.Logger;
import org.apache.log4j.lf5.LogLevel;
import org.springframework.web.WebApplicationInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import java.io.InputStream;
import java.util.Properties;
import java.util.TimeZone;
public class SystemPropertyDefaultsInitializer implements WebApplicationInitializer {
private static final Logger LOG = Logger.getLogger(SystemPropertyDefaultsInitializer.class);
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
String warName = new File(servletContext.getRealPath("/")).getName();
InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream("log4j.properties");
Properties properties = PropertiesHelper.loadApplicationProperties(resourceAsStream);
String logLocation = "/var/log/tomcat7/" + warName + ".log";
File file = new File(logLocation);
if (!file.exists()) {
FileOutputStream fos = new FileOutputStream(file);
fos.close();
}
if (!file.canWrite()) {
throw new IOException("Write permission denied for file \"" + logLocation + "\"");
}
LOG.info("Log location set to: \"" + logLocation + "\"");
properties.setProperty("log4j.appender.FILE.File", logLocation);
PropertyConfigurator.configure(properties);
}
}