Log4j doesn't seem to work in Spring Boot

I tried adding Spring and Maven to one of my existing projects, and I found that no matter how I set up, logging seems to be out of my control.

I tried putting log4j.properties in src/main/java and src/main/resources (actually I'm not sure where to put it).

But when I use Log4j for registration, the log is displayed only in the console, although I configure it to a file.

My log4j.properties looks like this:

 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'm not sure if I will miss something or Spring is overriding some settings, as I am new to Maven and Spring .

PS: before adding the Log4j dependencies to the pom .xml compilation errors, although I use org.apache.log4j.Logger

This is what my application.java application 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 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(); } } 

enter image description here

+5
source share
3 answers

UPDATE

By default, if you use "Starting POMs", Logback will be used for logging.

(From: Spring Download Link, Chapter 25 Registration )

So, either you configure your logging through the logback.xml , or you enable the logback.xml libraries. (when you need additional help with enabling lib, please write your pom.xml)

I recommend using logback (and slf4j)


OLD:

  • put the log4j.properties file in src\main\resources (not in ...\java )
  • make sure it's called log4j.properties (in your question you named the log4j.propertie file)
  • add these lines to 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? )

0
source

I ran into this exact problem, it turned out that spring boot includes spring boot Starter Logging and ignores everything you add while it is still there. I was able to fix this by double-clicking on my pom and clicking on Spring-Boot-Starter-Logging, and then selecting "edit starters" and then deleting the spring registration of the starter launch.

If you are using some other dependency management system, the idea is the same, just carefully review everything that spring loads in your project and make sure that log4J is the only logging system.

0
source

If you use log4j with spring-boot, you need to add a dependency with "exceptions" 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. This will solve your problem.

http://www.atechref.com/blog/maven/spring-boot-using-log4j-logging/

-1
source

Source: https://habr.com/ru/post/1205701/


All Articles