简体   繁体   中英

spring-boot - turn off console logging

Want to configure a spring-boot (1.3.5) application to send log-output only to a file -- turn off the console.

It looks very easy, according to the docs: howto-logging.html -- section § 72.1.1 Configure logback for file only output

But I just cannot get this to work -- it still logs both to file and console, whatever I try. Been googling for hours, but cannot find any suggestion that actually works.

Any clue what the issue might be?

EDIT : Please dont mark this as "duplicate" -- I have read them all -- and none of the suggested solutions work here.

Just add a logback.xml in your classpath root with following content:

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>
    <appender name="FILE_APPENDER" class="ch.qos.logback.core.FileAppender">
        <file>myApp.log</file>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="INFO">
        <appender-ref ref="FILE_APPENDER" />
    </root>
</configuration>

This would write all logs to the myApp.log file. Checkout Spring Boot documentation for more detailed discussion.

在application.properties中设置logging.pattern.console= (值保留为空),对我有用。

Having it set up, these are the steps I've followed:

Create a demo Spring Boot web project with this pom file:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>demo2</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo2</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

As it's said in the docs , the logging starter is not necessary because it's already included in the web starter. Let's create a @Service with some slf4j logging (that's not necessary at all):

@Service
public class MyService {

    private Logger logger = LoggerFactory.getLogger(MyService.class);

    public MyService() {
        logger.info("Created!");
    }

}

Then, we create a logback-spring.xml file, only configuring the file appender and not the one for the console, as the documentation suggests. Save it in the src/main/resources folder, the one Maven places at the root of the classpath:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="target/spring.log" />
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

When project starts, the initial log is shown in console but no more output is shown as it gets redirected to the target/spring.log file.

Finally --- I found the bug... This is a multi-module build, and turned out in one sub-module, there was also a logback.xml , that apparently took priority over the logback-spring.xml that I was fiddling with. When I excluded the other file from the build, it finally worked as expected. Phew....

This worked for me.

logging:
  pattern:
    console=:
  level
    root=OFF

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM