2017-10-19 79 views
1

提交我有了伐木工人在这样SpringBoot 1.5.x的不是写在logging.file

private static final Logger LOGGER = 
LoggerFactory.getLogger(myclass.class); 

了很多地方,这些记录器正在写我的控制台,但不写入文件的服务我在application.properties文件中指定这样

logging.file=my-service.log 

我pom.xml文件

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.5.1.RELEASE</version> 
</parent> 
<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-security</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-actuator</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-starter-config</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-context</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.microsoft.sqlserver</groupId> 
     <artifactId>sqljdbc</artifactId> 
     <version>4.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.jasypt</groupId> 
     <artifactId>jasypt-spring31</artifactId> 
     <version>1.9.2</version> 
    </dependency> 
    <dependency> 
     <groupId>org.apache.commons</groupId> 
     <artifactId>commons-lang3</artifactId> 
     <version>3.5</version> 
    </dependency> 
    <dependency> 
     <groupId>com.h2database</groupId> 
     <artifactId>h2</artifactId> 
     <version>1.4.193</version> 
    </dependency> 
    <dependency> 
     <groupId>com.maxmind.geoip</groupId> 
     <artifactId>geoip-api</artifactId> 
     <version>1.3.1</version> 
    </dependency> 

    <!-- AWS Dependencies --> 
    <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-aws-autoconfigure</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-aws-context</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-aws-actuator</artifactId> 
    </dependency> 
    <!-- END of AWS Dependencies --> 

我已经TR ied添加我自己的logback-spring.xml文件,该文件只写入文件,似乎只停止打印到控制台,但仍不写入文件。这里描述Spring Boot - no log file written (logging.file is not respected)

我已经通过代码并注意到LoggingSystem类正确设置属性。另外,如果我排除这样

<dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-data-jpa</artifactId> 
     <exclusions> 
      <exclusion> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-starter-logging</artifactId> 
      </exclusion> 
     </exclusions> 
    </dependency> 

然后创建服务日志文件,但通过内部的Java处理程序启动日志记录依赖,它的格式不正确。我想使用Spring,因为它可以在将日志级别设置为TRACE或DEBUG时显示更多信息。

有没有其他人看到这个问题,并知道如何让春天写服务日志到该指定的字段?

UPDATE

看来问题是从这些依赖

<dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-starter-config</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>org.springframework.cloud</groupId> 
     <artifactId>spring-cloud-context</artifactId> 
    </dependency> 

似乎任何springframework.cloud依存度将导致项目不能创建logging.file文件来了。

回答

1

我发现问题和解决方案。 我需要把日志属性像

logging.file=my-service.log 

bootstrap.properties文件。该文件应位于application.properties文件所在的资源目录中。

看来,当使用springframework.cloud依赖性时,首先调用BootstrapApplicationListener来初始化LogbackLoggingSystem类。然后,由于这些属性被初始化,那么应用程序会在application.properties文件中忽略它们。

更多这里https://github.com/spring-projects/spring-boot/issues/7099