在 spring 引导应用程序中从不同于类路径的不同位置配置 log4j.xml
configuring log4j.xml from different location other than classpath in spring boot application
我在文件中使用 sl4j 记录器打印日志,我配置了以下 log4j.xml 文件,因为我在 JBOSS 上部署我的 spring 应用程序,它不会创建目录结构像 tomcat 所以我无法配置日志的调试级别,我希望我的应用程序从 d:\configuration 等不同位置选择 log4j.xml 以便我可以为我的应用程序配置调试级别如何我可以做吗 ?我没有 web.xml。 我试过使用 PropertyPlaceholderConfigurer class 但它给出错误,因为文件存在但找不到文件
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="file" value="/home/client/webApp.log"/>
<param name="maxFileSize" value="5MB" />
<param name="maxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="file" />
</root>
首先在 application.properties
中设置一个 属性 即。 server.context_parameters.log4jConfigLocation=path/to/log4j.xml
。
然后实现一个监听器class Log4jConfigListener
如下
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.LogManager;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class Log4jConfigListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent servletcontextevent) {
LogManager.shutdown();
}
@Override
public void contextInitialized(ServletContextEvent servletcontextevent) {
ServletContext context = servletcontextevent.getServletContext();
String path = null;
path = context.getInitParameter("log4jConfigLocation");
PathMatchingResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = null;
try {
resources = pathResolver.getResources(path);
for (Resource resource : resources) {
File file = resource.getFile();
path = file.getAbsolutePath();
break; // read only the first configuration
}
} catch (IOException e) {
context.log("Unable to load log4j configuration file", e);
}
LogManager.resetConfiguration();
DOMConfigurator.configure(path);
}
}
接下来在配置 class 中将监听器注册为 @Bean
@Bean
public Log4jConfigListener log4jConfigListener() {
return new Log4jConfigListener();
}
更新 pom.xml
以排除默认的 logback 配置并包含如下的 log4j
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
在评论中让我们知道更多信息。
P.S.:对于非 Spring 引导( 或使用传统 web.xml
)参考我的回答
我在文件中使用 sl4j 记录器打印日志,我配置了以下 log4j.xml 文件,因为我在 JBOSS 上部署我的 spring 应用程序,它不会创建目录结构像 tomcat 所以我无法配置日志的调试级别,我希望我的应用程序从 d:\configuration 等不同位置选择 log4j.xml 以便我可以为我的应用程序配置调试级别如何我可以做吗 ?我没有 web.xml。 我试过使用 PropertyPlaceholderConfigurer class 但它给出错误,因为文件存在但找不到文件
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="file" class="org.apache.log4j.RollingFileAppender">
<param name="append" value="false" />
<param name="file" value="/home/client/webApp.log"/>
<param name="maxFileSize" value="5MB" />
<param name="maxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="DEBUG" />
<appender-ref ref="file" />
</root>
首先在 application.properties
中设置一个 属性 即。 server.context_parameters.log4jConfigLocation=path/to/log4j.xml
。
然后实现一个监听器class Log4jConfigListener
如下
import java.io.File;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.LogManager;
import org.apache.log4j.xml.DOMConfigurator;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
public class Log4jConfigListener implements ServletContextListener {
@Override
public void contextDestroyed(ServletContextEvent servletcontextevent) {
LogManager.shutdown();
}
@Override
public void contextInitialized(ServletContextEvent servletcontextevent) {
ServletContext context = servletcontextevent.getServletContext();
String path = null;
path = context.getInitParameter("log4jConfigLocation");
PathMatchingResourcePatternResolver pathResolver = new PathMatchingResourcePatternResolver();
Resource[] resources = null;
try {
resources = pathResolver.getResources(path);
for (Resource resource : resources) {
File file = resource.getFile();
path = file.getAbsolutePath();
break; // read only the first configuration
}
} catch (IOException e) {
context.log("Unable to load log4j configuration file", e);
}
LogManager.resetConfiguration();
DOMConfigurator.configure(path);
}
}
接下来在配置 class 中将监听器注册为 @Bean
@Bean
public Log4jConfigListener log4jConfigListener() {
return new Log4jConfigListener();
}
更新 pom.xml
以排除默认的 logback 配置并包含如下的 log4j
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
在评论中让我们知道更多信息。
P.S.:对于非 Spring 引导( 或使用传统 web.xml
)参考我的回答