2016-07-28 82 views
4

我创建了一个log4j.properties文件弹簧启动应用程序,我已经把它的资源文件夹中的文件application.properties,在这个文件中我有这样一行:logging.config=log4j.properties这表明名称我的log4j的属性文件,但是当我运行我的应用程序我得到我的控制台上的错误:放在哪里log4j.properties文件中

Logging system failed to initialize using configuration from 'log4j.properties' 
java.io.FileNotFoundException: C:\Users\***\Desktop\CapRecrute\log4j.properties (The system cannot find the file specified) 

所以我试图logging.config属性更改为src\\main\\resources\\log4j.properties,然后我得到了另一个错误:

java.lang.IllegalStateException: Could not initialize Logback logging from src\main\resources\log4j.properties ... Caused by: ch.qos.logback.core.LogbackException: Unexpected filename extension of file [file:/C:/Users/Aimad%20MAJDOU/Desktop/CapRecrute/src/main/resources/log4j.properties]. Should be either .groovy or .xml 

在我的POM文件我有这样的:

<dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-tomcat</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-log4j</artifactId> 
      <exclusions> 
       <exclusion> 
        <groupId>org.slf4j</groupId> 
        <artifactId>slf4j-log4j12</artifactId> 
       </exclusion> 
      </exclusions> 
     </dependency> 

我该如何解决这个问题?

+0

你有没有读过这个:http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html – cjstehno

回答

1

默认情况下,Spring引导期望一个logback文件,并且您提供了log4j配置。如果你添加log4j依赖,你会发现它的工作原理。如果您正在使用Maven你可以这样做是这样的:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-log4j2</artifactId> 
</dependency> 
+0

感谢您的帮助,但我已经在log4j依赖我的pom文件 –

0

两个问题:

  1. 是否需要SLF4J-log4j12排斥?
  2. 我认为排除spring-boot-starter-logging应该应用于spring-boot-starter。

这不是完整的聚甲醛。我假设包含spring-boot-starter,对吧?

如果您删除slf4j-log4j12排除并将spring-boot-starter-logging移动到spring-boot-starter,则您的文件应该看起来像文档中的示例。

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter</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-tomcat</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-log4j</artifactId> 
</dependency> 
0

最简单的事情就是使用Spring的的logback。你只需要这2行添加到application.properties

logging.level.org.springframework.web=DEBUG 
logging.level.org.hibernate=ERROR 

,并确保没有任何其他记录相关的线。

如果改为想log4j的,那么在你的pom.xml配置如此:

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-web</artifactId> 
</dependency> 
<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter</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> 

,然后在paplication.properties:

logging.path=/tmp/logs/ 
logging.file=/tmp/logs/myapplog.log 
logging.config=log4j.properties 

另外,还要确保log4j的是根你的项目(不在资源下)。

+0

嘿,我很棒。为我节省了相当一段时间 – ACV