spring-boot - 关闭控制台日志记录

spring-boot - turn off console logging

想要配置 spring-boot (1.3.5) 应用程序以仅将日志输出发送到文件 -- 关闭控制台。

根据文档,看起来很简单: howto-logging.html -- 部分 § 72.1.1 为仅文件输出配置 logback

但我就是无法让它工作——它仍然将 both 记录到文件 and 控制台,无论我尝试什么。 谷歌搜索了几个小时,但找不到任何实际有效的建议。

知道问题出在哪里吗?

编辑:请不要将此标记为"duplicate"——我已经全部阅读了—— - 并且 none 的建议解决方案在这里有效。

只需在您的类路径根目录中添加一个 logback.xml,内容如下:

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

这会将所有日志写入 myApp.log 文件。查看 Spring Boot documentation 以获得更详细的讨论。

设置完成后,这些是我遵循的步骤:

创建演示 Spring 使用此 pom 文件启动 Web 项目:

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

正如所说 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 日志记录(完全没有必要):

@Service
public class MyService {

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

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

}

然后,我们创建一个 logback-spring.xml 文件,只配置文件附加器而不是控制台的附加器,如文档所示。将其保存在 src/main/resources 文件夹中,Maven 将其放在类路径的根目录中:

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

项目启动时,初始日志显示在控制台中,但不再显示输出,因为它被重定向到 target/spring.log 文件。

终于 --- 我找到了错误... 这是一个多模块构建,结果在一个子模块中,还有一个 logback.xml,显然优先于 logback- spring.xml 我正在摆弄。当我从构建中排除其他文件时,它最终按预期工作。呸....

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

这对我有用。

logging:
  pattern:
    console=:
  level
    root=OFF