java Log4j 似乎在 Spring Boot 中不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26609879/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:13:53  来源:igfitidea点击:

Log4j seems not to work in Spring Boot

javaspringmavenlog4jspring-boot

提问by Jaskey

I tried to add Springand Mavento one of my existing project, and I find that no matter how I configure, the logging seems to be out of my control.

我尝试将Spring和添加Maven到我现有的项目之一,我发现无论我如何配置,日志记录似乎都超出了我的控制。

I tried putting the log4j.propertiesin src/main/javaand src/main/resources(Actually I am not sure where to put).

我试图把log4j.propertiessrc/main/javasrc/main/resources(其实我不知道放在哪里)。

But when I use Log4jto log, the log displays in the console only, though I configure it into a file.

但是当我使用Log4j日志时,日志仅显示在控制台中,尽管我将其配置到文件中。

My log4j.propertiesis like:

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

I am not sure if I miss something or Springoverrides some settings, since I am new to Mavenand Spring.

我不确定我是否遗漏了某些内容或Spring覆盖了某些设置,因为我是Maven和 的新手Spring

PS: Before I add dependencies of Log4jin pom.xml,no compile errors though I use org.apache.log4j.Logger

PS:之前我加依赖Log4jpom.XML,没有编译错误,虽然我用org.apache.log4j.Logger

This is how my application.java looks like:

这是我的 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();
    }
}

enter image description here

在此处输入图片说明

回答by Rahul sharma

If you are using log4j with spring-boot then you have to add dependency with "exclusions" in your pom.xml

如果您在 spring-boot 中使用 log4j,则必须在 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/

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

回答by barnesjon

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.

我遇到了这个确切的问题,结果证明 spring boot 包含 Spring Boot Starter Logging,并且会忽略您添加的任何内容,而这些内容仍然存在。我可以通过双击我的 pom 并单击 Spring-Boot-Starter-Logging,然后选择“编辑启动器”然后删除 spring 启动启动器日志记录来修复它。

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.

如果您使用其他类型的依赖管理系统,想法是相同的,只需仔细检查项目中包含的 spring boot 任何内容,并确保 log4J 是唯一包含的日志系统。

回答by Ralph

UPDATE

更新

By default, If you use the ‘Starter POMs', Logback will be used for logging

默认情况下,如果您使用“Starter POMs”,Logback 将用于日志记录

(From: Spring Boot Reference, Chapter 25 Logging)

(来自:Spring Boot 参考,第 25 章日志记录

So either you configure your logging via logback logback.xmlor you include the log4j libs. (when you need more help with including the lib then please post your pom.xml)

因此,要么通过 logback 配置日志记录,logback.xml要么包含 log4j 库。(当您需要更多关于包含 lib 的帮助时,请发布您的 pom.xml)

I reccomend to use logback (and slf4j)

我建议使用 logback(和 slf4j)



OLD:

老的:

  • put the log4j.propertiesfile in src\main\resources(not in ...\java)
  • make sure that it is named log4j.properties(in your question you named the file log4j.propertie)
  • add this lines to your web.xml
  • log4j.properties文件放入src\main\resources(不放入...\java
  • 确保它被命名log4j.properties(在你的问题中你命名了文件log4j.propertie
  • 将此行添加到您的 web.xml

web.xml:

网页.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?)

(@see使用 Spring 初始化 Log4J?