Spring Boot-没有写入日志文件(logging.file 不受尊重)

我使用 SpringBoot 并希望它将日志输出写入文件。

根据文档,这只需要通过设置

logging.file=filename.log

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

106009 次浏览

如果你正在使用 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;

像下面这样声明一个 logger 变量:

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

在课堂上这样用:

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

这个方法对我有用。我希望这个答案能帮助你。祝你好运!

PS: log4j.appender.file.File = ‘目录’是指定存储日志的位置的方式。如果您没有指定一个目录,只是将其保留为 filename.log,那么这个文件将自动在项目目录中创建。

我不知道这是否会帮助你,但我也在我的 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

下面是我如何设法将输出写入本地文件文件。 要禁用控制台日志记录并仅将输出写入文件,您需要一个自定义的 Logback-spring. xml < em > (称为 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将由 springboot 生成。

下面是我的应用程序结构树的样子:

enter image description here

如果希望获得更多乐趣,可以在 maven 依赖项中定位 base.xml,如下所示:

enter image description here

我找到了解决办法。我不是很高兴,因为它仍然没有回答我原来的问题为什么 logging.file的财产没有得到尊重。

我根据 Georges 的 回答application.properties所在的同一目录中创建了 logback-spring.xml。根据 文件的弹簧启动将从那里拿起。显然,这种事不会发生在我身上。

我需要另外添加 logging.config=classpath:logback-spring.xml,以便它是由春天拾取。我的 application.properties的相关部分现在是

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

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

我也有同样的问题。这很可能是由于文件系统上的文件权限。我有 root 用户所拥有的应用程序文件夹,但是。/流程所有者拥有的日志。因此,以下方法并不奏效:

logging.file=my.log

但这个可以

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

我只是使用了 Spring-boot 提供的日志记录机制:

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

我将 application.properties 和 logback.xml 文件放在相同的包‘ src/main/resources’下。 Properties 文件中只添加了一个参数:

logging.file = xyz.log

对我来说完全没问题。

在我的例子中,我错误地在我的一个子模块中添加了一个文件“ logback.xml”,这导致了这个问题,删除它,一切都会好起来的。

如果你在 Spring 引导,那么你可以直接 在 application.properties 中添加以下属性文件设置日志记录级别, 自定义日志模式并将日志存储在外部文件中。

这些是不同的日志记录级别及其从最小 < < max 的顺序。

OFF < < FATAL < < ERROR < < WARN < < INFO < < DEBUG < < TRACE < < ALL

# To set logs level as per your need.
logging.level.org.springframework = debug
logging.level.tech.hardik = trace


# To store logs to external file
# Here use strictly forward "/" slash for both Windows, Linux or any other os, otherwise, your logs it won't work.
logging.file=D:/spring_app_log_file.log


# To customize logging pattern.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"

请通过此链接更生动地自定义您的日志。

Https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html

抱歉这么晚才回复。Spring 的日志记录器似乎从它自己的类路径读取属性。 由于优先级, 没有遵守提供的属性。

这里有一些小窍门:

  1. 在 main 类中使用 springApplication.setDefaultProperties(properties);设置属性变量,如下所示
public static void main(String[] args) {
SpringApplication springApplication = new SpringApplication(MainClass.class);
Properties properties = new Properties();
properties.put("logging.file", logDirectory);
springApplication.setDefaultProperties(properties);
springApplication.run(args);
}
  1. 将属性作为 JVM 参数 -Dlogging.file=/location/output.log传递。

上述两种方法都不是最好的,因为为了定义其他日志属性,它们也应该遵循相同的方法。

解决方案

定义一个属性文件,将所有日志配置放入其中,并在 -Dspring.config.location中指定该文件。这是 我的另一个问题我就是这么解决的的推导。检查一下,以便了解其他解决方案,我已经尝试和他们的挑战。

我也面临同样的问题,因为我只是复制了窗口(使用“”在路径)提供的路径。

通过修改: 在路径中将反斜杠(“”)改为正斜杠(“/”)。

注意: 路径中应该使用严格的正斜杠(“/”) ,操作系统不是约束条件。

例如:- logging.file.name = D:/log/server.log

我使用命令行参数运行我的 Spring 启动服务,它工作得很好。所有 Spring 引导控制台日志都写入文件。我没有在 application.properties文件中配置任何日志记录。弹簧启动版本: 2.0.5.RELEASE

在窗口:

java -jar target\microservice-0.0.1.jar --logging.file=C:\\logs\\microservice.log

在 Linux 上

java -jar target\microservice-0.0.1.jar --logging.file=\var\log\microservice.log

在我的例子中,我粘贴了一些典型的配置,但是不知怎么搞错了我的日志模式(logging.patn.file)

注释它解决了我自己的问题(创建了文件,但是没有在其中写入任何东西,即使有控制台输出和根日志记录级别被设置为 DEBUG)——否则不会给出任何错误。

[edit] In other case (I always seem to bump into this problem), I was referencing a JAR file with classes stripped from a web application (WAR), which contained a logback.xml, not to mention AppInitializer - I suspect AppInitializer wouldn't be a problem, since it has a completely different package name and shouldn't be scanned by Spring auto config.. but logback.xml was being detected, I guess, in the classpath, and messed everything completely. I knew it was a hack to reference a WAR, but I was hoping for a quick fix - fixing that, breaking something else. Mani 的回答 is related.

在我的例子中,我在下面的应用程序属性文件中使用。

logging.file

相反,我需要使用下面的方法:

logging.file.name

从那时起,我就可以将日志导入到定向路径文件中。

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

在更高版本的 spring-boot-parent(from version 2.2.0)中,不推荐使用属性 logging.file。

检查 Springboot 父版本。

如果是2.3. x + ,那么属性应该是 logging.file.name = yourapplog.log

我在 windows 操作系统中也遇到了同样的问题,我只是将-> logging.file 改为 logging.file.name = D:/customer _ Projects/Flight _ reserve _ system/log/reserve. log

注意: 我使用的 Spring-boot 版本是2.4.1

在 Windows 操作系统中,当您从文件资源管理器中复制文件路径时,您将获得文件路径,如 D: customer _ Projects Flight _ reserve _ system log Flight _ reserve. log

以上文件路径将不起作用。所以您需要更改文件路径,比如 D:/customer _ Projects/Flight _ reserve _ system/logs/reserve. log,然后它会在我们指定的路径中创建一个日志文件。谢谢你。

如果你想放置日志文件在特定的目录,如 D 驱动器,然后下面的变化可以在春季启动项目中完成

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. 在 应用性能文件中也加入以下内容

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

注意: 上面提到的更改是针对2.4.3版本的

SpringBoot: 版本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

对于 Springboot 2.4.2,Application.yml 下面的配置可以工作:

logging:
file:
name: logpath

我的问题是文件没有被自动创建,即使我在项目根文件夹中创建它,它也没有被写入,所以最终,我尝试在 logs/logfile.lo中创建一个文件夹和一个文件,并设置 logging.file.name=scripts/logfile.log和 application.properties 文件,结果成功了

而不是 application.properties 中的以下内容

logging.file = logFileName.log

使用下面的一个

logging.file.name= appLogName.log

检查屏幕截图,以供参考。 enter image description here