Jas*_*key 8 java spring log4j maven spring-boot
我尝试添加Spring和Maven我现有的项目之一,我发现,无论我怎么配置,日志记录似乎是在我的掌握.
我试图把log4j.properties在src/main/java和src/main/resources(其实我不知道放在哪里).
但是当我使用Log4j日志时,日志只显示在控制台中,尽管我将其配置为文件.
我的意思log4j.properties是:
log4j.rootLogger=DEBUG, A1
log4j.appender.A1=org.apache.log4j.FileAppender
log4j.appender.A1.encoding=utf-8
log4j.appender.A1.File=E:\Programminglog\debug.log
log4j.appender.A1.Threshold = DEBUG
log4j.appender.A1.Append=true
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n
Run Code Online (Sandbox Code Playgroud)
我不确定我是否遗漏了某些内容或Spring覆盖了某些设置,因为我是新手Maven和Spring.
PS:之前我加依赖Log4j于pom.XML,虽然没有我使用的编译错误org.apache.log4j.Logger
这就是我的application.java的样子:
@Configuration
@EnableAutoConfiguration
@ComponentScan({"hello","wodinow.weixin.jaskey"})
public class Application extends WebMvcConfigurerAdapter {
public static void main(String[] args) {
ApplicationContext ctx = SpringApplication.run(Application.class, args);
System.out.println("Let's inspect the beans provided by Spring Boot:");
String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}
LogUtil.info("Application Boots!");// here this line does not show in the file
}
@Bean
public CommandService commandService(){
return CommandService.getInstance();
}
}
Run Code Online (Sandbox Code Playgroud)

如果您将log4j与spring-boot结合使用,则必须在pom.xml中添加“ exclusions”相关性
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<version>1.3.3.RELEASE</version>
**<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>
<version>1.2.5.RELEASE</version>
</dependency>**
Run Code Online (Sandbox Code Playgroud)
请遵循此。它将解决您的问题。
http://www.atechref.com/blog/maven/spring-boot-using-log4j-logging/
更新
\n\n\n\n\n默认情况下,如果使用 \xe2\x80\x98Starter POMs\xe2\x80\x99,Logback 将用于记录日志
\n
(来自:Spring Boot 参考,第 25 章日志记录)
\n\n因此,要么通过 logback 配置日志记录logback.xml,要么包含 log4j 库。(当您需要更多帮助来包含 lib 时,请发布您的 pom.xml)
我推荐使用 logback (和 slf4j)
\n\n老的:
\n\nlog4j.properties文件放入src\\main\\resources(而不是放入...\\java)log4j.properties(在您的问题中您命名了该文件log4j.propertie)web.xml网络.xml:
\n\n <context-param>\n <param-name>log4jConfigLocation</param-name>\n <param-value>/WEB-INF/classes/log4j.properties</param-value>\n </context-param>\n\n <listener>\n <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>\n </listener>\nRun Code Online (Sandbox Code Playgroud)\n\n(@参见使用 Spring 初始化 Log4J?)
\n| 归档时间: |
|
| 查看次数: |
12772 次 |
| 最近记录: |