Spring Boot - 没有写入日志文件(logging.file 不受尊重)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38527175/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - no log file written (logging.file is not respected)
提问by Christoph M?bius
I use Spring Boot and want it to write log output to a file.
我使用 Spring Boot 并希望它将日志输出写入文件。
According to the docs, this is simply done by setting
根据文档,这只需通过设置即可完成
logging.file=filename.log
While the console output works fine, filename.log
is not created. Also, if I create the file manually, nothing is written to it. What do I miss?
虽然控制台输出工作正常,filename.log
但未创建。此外,如果我手动创建文件,则不会写入任何内容。我想念什么?
采纳答案by Christoph M?bius
I found a solution. I am not very happy with it since it still does not answer my original question why the logging.file
property is not respected.
我找到了解决方案。我对它不是很满意,因为它仍然没有回答我最初的问题,为什么该logging.file
财产不受尊重。
I created the logback-spring.xml
from Georges' answerin the same directory where application.properties
resides. According to the documentationSpring Boot will pick it up from there. Apparently, this does not happen in my case.
我在所在的同一目录中创建了logback-spring.xml
来自 Georges 的回答application.properties
。根据文档Spring Boot 将从那里获取它。显然,这不会发生在我的情况下。
I need to additionally add logging.config=classpath:logback-spring.xml
in order it is picked up by Spring. The relevant parts of my application.properties
are now
我需要另外添加logging.config=classpath:logback-spring.xml
以便它被 Spring 接收。我的相关部分application.properties
现在是
logging.config=classpath:logback-spring.xml
logging.file=logs/logfile.log
(I created the logs
directory manually.)
(我logs
手动创建了目录。)
回答by Ravindran Kanniah
I don't know whether this would help you but I am also using Logback in my Spring-Boot
project and the structure is as below
我不知道这是否对您有帮助,但我也在我的Spring-Boot
项目中使用了 Logback ,其结构如下
File:logback.xml
文件: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>
File:application.properties
文件:application.properties
app.logPathPrefix=/var/log/myproject
回答by Achille_vanhoutte
Here is how i managed to write output to a local file file. To disable console logging and write output only to a file you need a custom logback-spring.xml( call it logback-spring.xmlso you ll take advantage of the templating features (date formatting etc..) provided by Boot)that imports file-appender.xmlinstead of console-appender.xml. In order to achieve this, you must paste this code below into your logback-spring.xml file.
这是我设法将输出写入本地文件的方法。要禁用控制台记录和写输出只需要自定义文件的logback-spring.xml (称之为的logback-spring.xml所以你会采取的模板功能优势(日期格式等)的启动提供)是进口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>
You also need to add the following to your application.properties:
您还需要将以下内容添加到您的 application.properties 中:
logging.file=myapplication.log
Notice that this log file myapplication.logwill be generated by springboot.
请注意,此日志文件myapplication.log将由 springboot 生成。
This is how my application structure tree looks like:
这是我的应用程序结构树的样子:
If you want to have more fun, you can locate the base.xml in your maven dependencies like this:
如果你想玩得更开心,你可以在你的 maven 依赖中找到 base.xml,如下所示:
回答by beaudet
I had the same problem. It's more than likely due to file permissions on the file system. I had the application folder owned by root, but ./logs owned by the process owner. As such, the following didn't work:
我有同样的问题。这很可能是由于文件系统上的文件权限所致。我的应用程序文件夹归 root 所有,但 ./logs 归进程所有者所有。因此,以下方法不起作用:
logging.file=my.log
logging.file=my.log
but this did
但这确实
logging.file=/opt/myapp/logs/my.log
logging.file=/opt/myapp/logs/my.log
回答by Lazar Lazarov
If you are using Maven add the dependency :
如果您使用 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>
Now you have to specify a file that is called 'log4j.properties' which you have to put in the specific directory : ' src/main/resources/log4j.properties '
现在您必须指定一个名为“log4j.properties”的文件,您必须将其放在特定目录中:“src/main/resources/log4j.properties”
Here is how the file should look for example :
以下是文件的外观,例如:
# 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
Now import these :
现在导入这些:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Declare a logger variable like this :
像这样声明一个记录器变量:
final static Logger logger = Logger.getLogger(TheClassYourIn.class);
And use it in the class like this :
并在类中使用它,如下所示:
logger.info("Well hello world then ");
This way it works for me. I hope that this answer will help you . Good luck !
这样它对我有用。我希望这个答案对你有所帮助。祝你好运 !
PS: log4j.appender.file.File='directory' is how you specify where the logs to be stored. If you don't specify a directory and just leave it as filename.log this file will be automaticly created in the project dir.
PS:log4j.appender.file.File='directory' 是您指定要存储日志的位置的方式。如果您不指定目录而将其保留为 filename.log,则该文件将在项目目录中自动创建。
回答by Punit
I just used the Spring-boot provided logging mechanism. I wrote below in my 'logback.xml' file :
我只是使用了 Spring-boot 提供的日志记录机制。我在“logback.xml”文件中写道:
<?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>
I put the both application.properties and logback.xml file under same package 'src/main/resources'. In application.properties file just added one parameter :
我将 application.properties 和 logback.xml 文件放在同一个包“src/main/resources”下。在 application.properties 文件中只添加了一个参数:
logging.file = xyz.log
It worked perfectly fine for me.
它对我来说非常好。
回答by July
In my case, I added a file "logback.xml" in one of my sub module by mistake which caused this issue, remove it and everything will be fine.
就我而言,我错误地在我的一个子模块中添加了一个文件“logback.xml”,这导致了这个问题,将其删除,一切都会好起来的。
回答by Hardik Patel
If you are on Spring Boot then you can directly add following properties in application.propertiesfile to set logging level, customize logging pattern and to store logs in the external file.
如果您使用的是 Spring Boot,那么您可以直接在 application.properties文件中添加以下属性来设置日志级别、自定义日志模式并将日志存储在外部文件中。
These are different logging levels and its order from minimum << maximum.
这些是不同的日志记录级别及其从最小值 << 最大值的顺序。
OFF << FATAL << ERROR << WARN << INFO << DEBUG << TRACE << ALL
OFF << 致命 << 错误 << 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"
Please pass through this link to customize your logs more vividly.
请通过此链接更生动地自定义您的日志。
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
回答by Mani
Sorry for the late reply. It seems spring's logger reads the property from its own classpath.Due to precedence,it's not respecting the properties supplied.
这么晚才回复很抱歉。似乎 spring 的记录器从它自己的类路径中读取属性。由于优先级,它不尊重提供的属性。
Some tricks to get around:
一些绕过的技巧:
- In main class set the property variable using
springApplication.setDefaultProperties(properties);
like this
- 在主类中使用
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);
}
- Pass the property as JVM parameter
-Dlogging.file=/location/output.log
.
- 将属性作为 JVM 参数传递
-Dlogging.file=/location/output.log
。
Both of the above are not the best ones as in order to define other logging properties they also should follow the same way.
以上两个都不是最好的,因为为了定义其他日志属性,它们也应该遵循相同的方式。
Solution
解决方案
Define a property file and put all you logging configurations in that and specify the file in -Dspring.config.location
. This is a derivation of my other problemand this is how I resolved that. check that out in order to know other solutions that I've tried and their challenges.
定义一个属性文件并将所有日志配置放在其中,并在-Dspring.config.location
. 这是一个派生我的其他问题和我这是怎么解决了。检查一下,以了解我尝试过的其他解决方案及其面临的挑战。
回答by Bhupendra Kumar
I was also facing same issues, since i just copied the path as windows(uses "\" in path) provide.
我也面临同样的问题,因为我只是复制了 Windows(在路径中使用“\”)提供的路径。
Fixed just by changing : back slash ("\") to forward slash ("/") in path.
只需将路径中的 : 反斜杠 ("\") 更改为正斜杠 ("/") 即可修复。
Note: Strictly forward slash ("/") should be used in path, OS is not the constraint.
注意:路径中应严格使用正斜杠(“/”),操作系统不是约束。
ex:- logging.file.name=D:/Logs/server.log
例如:- logging.file.name=D:/Logs/server.log