【问题标题】:Log4j seems not to work in Spring BootLog4j 似乎在 Spring Boot 中不起作用
【发布时间】:2014-12-23 22:46:31
【问题描述】:

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

我尝试将log4j.properties 放在src/main/javasrc/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

我不确定我是否遗漏了什么或Spring 覆盖了某些设置,因为我是MavenSpring 的新手。

PS:在我在pom.xml 中添加Log4j 的依赖项之前,虽然我使用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();
    }
}

【问题讨论】:

  • 你在什么服务器上运行这个?仅供参考,默认情况下,Maven 不会将任何内容从 src/main/java 复制到您的应用程序中。因此你应该在 src/main/resources 中保留 log4j.properties 等
  • 我使用 spring embed tomcat,我运行我的 Application.java 作为 java 应用程序来启动我的应用程序。顺便说一句,我也将该属性复制到 webapp/WEB-INF 中,但没有运气:(
  • log4j.properties 必须在类路径上可用。 WEB-INF 目录(通常)不在类路径上。如果您在命令行上使用-Dlog4j.debug 运行您的应用程序,您会发现 log4j 认为正在发生的事情(如果有的话)。
  • @SteveC,请给我完整的命令如何使用 -Dlog4j.debug 运行我的应用程序?顺便说一句,我使用 Spring boot,我不知道它做了什么..

标签: java spring maven log4j spring-boot


【解决方案1】:

更新

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

(来自:Spring Boot Reference, Chapter 25 Logging

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

我建议使用 logback(和 slf4j)


旧:

  • log4j.properties文件放入src\main\resources(不在...\java中)
  • 确保将其命名为 log4j.properties(在您的问题中,您将文件命名为 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>

(@见Initializing Log4J with Spring?)

【讨论】:

  • 抱歉问题中的拼写错误,我确实在我的 web.xml 中添加了您所指的内容,但没有运气。另外,我使用的是 spring boot 和 spring mvc,所以实际上,我没有在 web.xml 中添加任何特殊内容,我使用 Application.java 运行应用程序
  • @Jaskey 你明白了吗?我在使用 spring-boot 时遇到了同样的问题。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    如果您在 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>**
    

    请遵循此。它会解决你的问题。

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

    【讨论】:

    猜你喜欢
    • 2021-11-02
    • 1970-01-01
    • 2020-10-10
    • 2017-05-05
    • 1970-01-01
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-28
    相关资源
    最近更新 更多