Reputation: 15785
I tried to add Spring
and Maven
to one of my existing project, and I find that no matter how I configure, the logging seems to be out of my control.
I tried putting the log4j.properties
in src/main/java
and src/main/resources
(Actually I am not sure where to put).
But when I use Log4j
to log, the log displays in the console only, though I configure it into a file.
My log4j.properties
is like:
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
I am not sure if I miss something or Spring
overrides some settings, since I am new to Maven
and Spring
.
PS: Before I add dependencies of Log4j
in pom
.xml,no compile errors though I use org.apache.log4j.Logger
This is how my application.java looks like:
@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();
}
}
Upvotes: 8
Views: 19958
Reputation: 1
First create log4j2-apring.xml in resources dir. second exclude default sprint bood logback from starter in pom.xml then add log4j starter to po.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</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-log4j2</artifactId>
</dependency>
Upvotes: 0
Reputation: 409
If you are using log4j with spring-boot then you have to add dependency with "exclusions" in your pom.xml
<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>**
Please follow this. It will resolve your problem.
http://www.atechref.com/blog/maven/spring-boot-using-log4j-logging/
Upvotes: 7
Reputation: 39
I ran into this exact problem, it turned out that spring boot includes Spring Boot Starter Logging and will ignore anything you add while that is still there. I was able to fix it by double clicking on my pom and clicking on Spring-Boot-Starter-Logging and then selecting "edit starters" then removing the spring boot starter logging.
If you are using some other kind of dependency management system the idea is the same, just carefully review anything that spring boot included in your project and make sure that log4J is the only logging system included.
Upvotes: 1
Reputation: 120881
UPDATE
By default, If you use the ‘Starter POMs’, Logback will be used for logging
(From: Spring Boot Reference, Chapter 25 Logging)
So either you configure your logging via logback logback.xml
or you include the log4j libs. (when you need more help with including the lib then please post your pom.xml)
I reccomend to use logback (and slf4j)
OLD:
log4j.properties
file in src\main\resources
(not in ...\java
)log4j.properties
(in your question you named the file log4j.propertie
)web.xml
web.xml:
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
(@see Initializing Log4J with Spring?)
Upvotes: 1