Java 如何为 spring-boot 应用程序设置 logging.path?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40913788/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:59:47  来源:igfitidea点击:

How to set logging.path for spring-boot apps?

javaspringspring-boot

提问by membersound

spring-bootprovides several logging.*settings that can be applied in application.properties, like:

spring-boot提供了几种logging.*可以应用于 的设置application.properties,例如:

logging.level.=DEBUG
logging.file=myfile.log
logging.path=d:/logs/

Problem: myfile.logis generated, BUT inside the classpath! Why isn't spring taking my absolute path into account?

问题:myfile.log生成,但在类路径中!为什么 spring 不考虑我的绝对路径?

采纳答案by davidxxx

The Spring Boot documentationstates

春季启动文档状态

By default, Spring Boot will only log to the console and will not write log files. If you want to write log files in addition to the console output you need to set a logging.file or logging.path property (for example in your application.properties).

默认情况下,Spring Boot 只会登录到控制台,不会写入日志文件。如果您想在控制台输出之外写入日志文件,您需要设置 logging.file 或 logging.path 属性(例如在您的 application.properties 中)。

and then describes how the logging.fileand logging.pathproperties work. You should only set one.

然后描述logging.filelogging.path属性是如何工作的。你应该只设置一个。

If logging.fileis set, it will write to that specific file. The documentation states

如果logging.file设置,它将写入该特定文件。该文件指出

Names can be an exact location or relative to the current directory.

名称可以是确切位置或相对于当前目录。

So you're likely writing to your current directory, which happens to be the same as your classpath.

因此,您可能正在写入当前目录,该目录恰好与您的类路径相同。

If you set logging.path, Spring Boot

如果你设置logging.path,Spring Boot

Writes spring.logto the specified directory. Names can be an exact location or relative to the current directory.

写入spring.log指定目录。名称可以是确切位置或相对于当前目录。

Check that your current directory isn't your classpath, if you don't want them to mix, and adapt one of the logging.fileand logging.pathaccordingly.

检查你的当前目录是不是你的classpath,如果你不想让他们混,适应的一个logging.filelogging.path相应。

回答by Pawana

I don't know if this is still needed, but you can set the absolute path with following code according to your example

我不知道这是否仍然需要,但是您可以根据您的示例使用以下代码设置绝对路径

logging.path=D:\logs\logfile.txt

You can alter the filename and the path like this. If the folder doesn't exist it gets created. On Windows you have to work with \ as seperator, while on Linux and Mac you need / as seperator.

您可以像这样更改文件名和路径。如果该文件夹不存在,则会创建它。在 Windows 上,您必须使用 \ 作为分隔符,而在 Linux 和 Mac 上,您需要使用 / 作为分隔符。

REMEMBER: You cannot have logging.file AND logging.path in your properties together. It is either .file OR .path ... in your case the path.

记住:您的属性中不能同时包含 logging.file 和 logging.path。它是 .file 或 .path ...在你的情况下是路径。

Tested 2 Minutes before posting

在发布前测试了 2 分钟

回答by Hernan Ramovecchi

You can also have this configuration on your app.properties. Thats how it is working in one of my projects.

您也可以在app.properties. 这就是它在我的一个项目中的工作方式。

logging.path=../logs

logging.file=${logging.path}/fileName.log

So, you can have both properties and one refers to the other.

因此,您可以同时拥有这两个属性,并且一个引用另一个。

回答by Dheeraj Madan

I have set logging.file=C:/usr/local/tomcat/logs/hib.login the application.propertiesand the setting like below in class

我已logging.file=C:/usr/local/tomcat/logs/hib.logapplication.properties以下在课堂上设置类似

private static final Logger logger = LogManager.getLogger(ChargeMasterController.class);

logger.info("Total time taken for  is ---------------------------" + time  + " ms");

Logs are getting printed fine in the path mentioned as logging.file.

日志在 logging.file 提到的路径中打印得很好。

Now I want to print my logs in 2 different files for 2 different classes(In same package), how can I set 2 logging.file in application.properties

现在我想在 2 个不同的文件中为 2 个不同的类(在同一个包中)打印我的日志,我如何在 application.properties 中设置 2 logging.file