Stav Alon
Stav Alon

Reputation: 61

SpringBoot 1.5.x not writing to file at logging.file

I have a service that has loggers in a lot of places like this

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

These loggers are writing to my console but are not writing to the file I specified in my application.properties file like this

logging.file=my-service.log

my pom.xml file

<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 -->

I have tried adding my own logback-spring.xml file that only writes to a file and that seems to only stop the the printing to the console but still it doesn't write to the file. Described here Spring Boot - no log file written (logging.file is not respected)

I have stepped through the code and noticed that the LoggingSystem class is setting the properties correctly. Also if I exclude starter-logging dependency like this

<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>

then the service log file is created but by the internal Java handler and it's in the wrong format. I want to use the Spring one because it can show more info when setting the log level to TRACE or DEBUG.

Has anyone else seen this problem and know how to make Spring write the service logs to that filed specified?

UPDATE

It seems the issue is coming from these dependencies

<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>

It seems any springframework.cloud dependency will cause the project to not create the logging.file file.

Upvotes: 2

Views: 1564

Answers (1)

Stav Alon
Stav Alon

Reputation: 61

I found the problem and the solution. I needed to put the logging properties like

logging.file=my-service.log

in a bootstrap.properties file. This file should be places in the resources directory where the application.properties file is.

It seems that when using the springframework.cloud dependency BootstrapApplicationListener is called first to initialize LogbackLoggingSystem class. Then since those properties are initialized then the application ignores them in the application.properties file.

More here https://github.com/spring-projects/spring-boot/issues/7099

Upvotes: 2

Related Questions