无法将应用程序日志保存到文件

时间:2019-05-10 05:05:40

标签: java spring-boot

我正在创建一个简单的Spring Boot应用程序,该应用程序会在API调用上产生结果。 该应用程序按预期工作,但是我无法将日志打印到文件中。 日志仅在控制台中显示。 这是我的pom文件:

<?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>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.project.simple</groupId>
    <artifactId>SampleApplication</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>SampleApplication</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>

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

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

</project>

这是我的application.properties:

# H2
spring.h2.console.enabled=true
spring.h2.console.path=/h2
# Datasource
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
#Logging
logging.level.org.springframework.web=ERROR
logging.level.com.project.simple.SampleApplication=INFO

# Logging pattern for the console
logging.pattern.console= %d{yyyy-MM-dd HH:mm:ss} - %msg%n

# Logging pattern for file
logging.pattern.file= %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%

#output to a file
logging.file=/Users/application.log

有人想让我知道吗?

1 个答案:

答案 0 :(得分:0)

尝试将以下依赖项添加到pom:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
    <version>2.1.4.RELEASE</version>
</dependency>
  

使用 spring-boot启动器时, Logback 用于记录日志   默认情况下。 Logger的默认日志记录级别预设为INFO,   表示TRACE和DEBUG消息不可见。

     

Spring Boot没有强制性的日志依赖,除了    Commons Logging API ,通常由Spring Framework的spring-jcl模块提供。要使用登录,您需要   在类路径中包含它和spring-jcl。最简单的方法   这是通过启动器,这一切都取决于   spring-boot-starter-logging

春季启动中的自定义日志配置 Spring自动识别并获取以下文件进行配置:  -logback-spring.xml  -logback.xml  -logback-spring.groovy  -logback.groovy

或者,如果这不起作用,请尝试配置spring-boot以仅使用以下配置将日志写入文件: logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

供参考,请阅读here