Spring Boot - 没有写日志文件(logging.file未被识别)

81

我使用Spring Boot,希望将其日志输出到文件中。

根据文档,只需设置:

logging.file=filename.log
虽然控制台输出正常,但filename.log未被创建。另外,如果我手动创建文件,则不会向其中写入任何内容。我错过了什么?

1
你把 filename.log 文件放在哪里了? - Ali Dehghani
请展示你的:application.properties文件。 - AchillesVan
默认情况下,Spring Boot 通过 Logback(http://logback.qos.ch)配置日志记录,以 INFO 级别记录到控制台。但是,如果您决定使用 Log4j 或 Log4j2,则需要更改依赖项以包括所需的启动器以使用所需的日志记录实现,并排除 Logback。 - AchillesVan
这可能不会有什么帮助,但当我回到日志文件夹时,那里没有文件。我在 /var/log/spring 中创建了它并验证了它的存在,但是当我回去时它就消失了。很奇怪。当我重新创建它时,Spring 就记录到它里面了。 - milosmns
10
2.2及以上版本请使用 logging.file.name ,不再支持使用 logging.file (在2.2中已废弃,在2.3中完全删除)。 - barryku
显示剩余3条评论
24个回答

95

请使用logging.file.name代替logging.file

spring-boot-parent(版本2.2.0及以上)中,属性logging.file已被废弃。


2
我已经升级到最新的Springboot版本,但日志文件没有被创建。 谢谢回答。 - nandeesh
1
它对我起作用了。我正在使用2.3.1版本。非常感谢。这真的很有帮助。 - Goose
1
文档链接(选择您的Spring Boot版本):https://docs.spring.io/spring-boot/docs/ - hello_earth

32

我找到了一个解决方案,但我并不是很满意,因为它仍然没有回答我的原始问题,即为什么logging.file属性不被尊重。

我在与application.properties同一目录下创建了logback-spring.xml,内容来自于George的答案。根据文档,Spring Boot将从该位置选择它。然而,在我的情况下,这显然没有发生。

为了让Spring正确识别,我需要额外添加logging.config=classpath:logback-spring.xml。现在,我application.properties文件的相关部分如下:

logging.config=classpath:logback-spring.xml
logging.file=logs/logfile.log

(我手动创建了logs目录。)


+1:如果有人正在尝试将Spring Batch Admin UI与Spring Boot批处理作业集成,并且已经覆盖了来自“spring-batch-admin-manager”项目的“env-context.xml”,则需要在“application.properties”中使用“logging.config”。 - Sabir Khan
你的项目中的资源文件夹里是否已经包含了 logback.xml 文件? - niaomingjian
logging.file is respected for sure, depends on the fact how you specify the path, file will be created there. As a sample, in my project I have following configuration of logging in the application.yml - logging: file: logs/dictoro-livescore-api.log level: INFO and it is creating log file in file system (I'm using Linux Ubuntu) in the folder /logs - BigGinDaHouse
这不是必需的:logging.config=classpath:logback-spring.xml。但如果您想为不同的配置文件(prod vs dev)使用不同的logback配置,则可能会有用。此外,您无需创建文件夹。 - ACV
如何在我的 Spring Boot 项目目录中创建日志文件?我想为错误和信息消息创建单独的文件。我正在使用 Spring Boot v.2.5。谢谢。 - Waseem

24

在我的情况下,我在应用程序属性文件中使用了以下内容:

logging.file

但实际上我需要使用以下内容:

logging.file.name

这样,我就可以将日志记录到指定的路径文件中了。


11

我遇到了同样的问题。很可能是文件系统上的文件权限导致的。我将应用程序文件夹的所有者设置为root,但"./logs"的所有者为进程所有者。因此,以下操作无法正常工作:

logging.file=my.log

但是这确实做到了

logging.file=/opt/myapp/logs/my.log

正如 Vivek 所指出的那样,logging.file 现在已经被弃用了,因此除非您使用的是旧版本的 Spring,请遵循该指南,而不是我的指南,因为我的指南现在已经过时了。 - beaudet

8

Spring Boot: 版本 2.4.3

application.properties文件中,应使用以下任一项:logging.file.namelogging.file.path

例如:

logging.file.name=logs/myapp.log
logging.file.path=logs

不需要手动创建 logs 目录,它会在类路径中自动创建。
要查看其他弃用的属性,请阅读此类文件 ~/.m2/repository/org/springframework/boot/spring-boot/2.4.3/spring-boot-2.4.3.jar!/org/springframework/boot/logging/LoggingSystemProperties.class

对我没有用:logging.file.path=../tmp - likejudo

6
这是我成功将输出写入本地文件的方法。要禁用控制台日志记录并仅将输出写入文件,您需要一个自定义的logback-spring.xml(将其命名为logback-spring.xml以利用Boot提供的模板特性(日期格式等)),它导入file-appender.xml而不是console-appender.xml。为了实现这一点,您必须将下面的代码粘贴到您的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> 

您还需要将以下内容添加到application.properties文件中:
logging.file=myapplication.log

注意,这个日志文件myapplication.log将由Spring Boot生成。
这是我的应用程序结构树的样子:

enter image description here

如果你想更有趣的话,你可以像这样在你的maven依赖中找到base.xml文件:

enter image description here


1
抱歉,对我来说这也行不通。我改变了我的application.properties文件,使唯一与日志相关的行是logging.file=logfile.log,这将禁用控制台日志记录。 然而,没有任何日志文件被写入。(是的,我正在使用Spring Boot 1.3.6,是的,logback配置文件位于“资源”下)。 - Christoph Möbius
你运行应用程序后刷新了吗? - AchillesVan
你可以下载我的代码并进行测试:https://drive.google.com/file/d/0B_EVyl90ivXwQ3NpVHllVWpQZmM/view?usp=sharing - AchillesVan
logging.config=classpath:logback-spring.xml 做到了这个。 - Christoph Möbius

5

检查Springboot父级的版本。

如果版本是2.3.x+,则属性应为logging.file.name=yourapplog.log。


5

我不知道这是否对你有帮助,但我也在我的Spring-Boot项目中使用Logback,结构如下:

enter image description here

文件:logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="logback.xsd">

    <property resource="\application.properties"/>

    <appender name="FILE"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${app.logPathPrefix}/myproject.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${app.logPathPrefix}/myproject.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>50MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
            <maxHistory>30</maxHistory>
        </rollingPolicy>

        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%logger:%line] %msg%n
            </pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%thread] [%logger:%line] %msg%n
            </pattern>
        </encoder>
    </appender>


    <logger name="org.springframework" level="INFO" />
    <logger name="com.mycompany" level="INFO" />
    <logger name="org.hibernate" level="DEBUG" />


    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="FILE" />
    </root>

</configuration>

文件名:application.properties

app.logPathPrefix=/var/log/myproject

3
如果您正在使用Maven,请添加以下依赖项:
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.6</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
    <version>1.7.5</version>
</dependency>

现在你需要指定一个名为'log4j.properties'的文件,该文件必须放置在特定目录“src/main/resources/log4j.properties”中。
以下是示例文件格式:
# Root logger option
log4j.rootLogger=INFO, file, stdout
log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# log4j.appender.springlog.Threshold=INFO
log4j.appender.springlog.layout=org.apache.log4j.PatternLayout
log4j.appender.springlog.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# Direct log messages to a log file
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:/example/filename.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

现在导入这些内容:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

声明一个日志变量,可以像这样:

final static Logger logger = Logger.getLogger(TheClassYourIn.class);

然后在类中像这样使用:

logger.info("Well hello world then ");

这是我的做法。希望我的回答能对你有所帮助。祝你好运!

PS: log4j.appender.file.File='directory' 是指定日志存储位置的方法。如果你没有指定目录,只是以文件名.log命名,那么这个文件会自动创建在项目目录中。


在我看来,这是一个有效的答案。有时候答案可能不起作用,因为用户环境或用户应该登录的包没有指定。我会给这个答案点赞。 - Ravindran Kanniah
谢谢您提供详细的示例。不过,我会使用logback,因为它与Spring Boot捆绑在一起。 - Christoph Möbius

3
如果您想将日志文件放在特定目录中(例如 D 盘),则可以在 Spring Boot 项目中进行以下更改:
1. 创建 logback-spring.xml 配置文件,并提及您想要创建日志的包名称。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml" />
    
        <logger name="com.example.demo" 
        level="DEBUG" >
           
        </logger>
</configuration>

2. 还需在 application.properties 文件中进行以下添加。

logging.config=classpath:logback-spring.xml
logging.file.name=F:/Springbootlogs/filename.log

注意:所提到的更改是针对版本2.4.3的。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接