简体   繁体   English

如何更改SLF4J SimpleLogger文件名?

[英]How to change SLF4J SimpleLogger filename?

My Java application uses SLF4J which I have configured to use the SimpleLogger implementation and redirect log messages to a file. 我的Java应用程序使用SLF4J (已配置为使用SimpleLogger实现)并将日志消息重定向到文件。 That is working fine. 很好

How do I subsequently change the name of the log file? 随后如何更改日志文件的名称?

I have tried changing the LOG_FILE_KEY property but it seems to have no effect. 我尝试更改LOG_FILE_KEY属性,但似乎没有任何效果。 The log messages continue to be output to the original log file. 日志消息继续输出到原始日志文件。

This is what I've done: 这是我所做的:

System.setProperty(org.slf4j.impl.SimpleLogger.LOG_FILE_KEY, Paths.get("new-filename.txt").toString());

I think I figured out the answer with the help of this answer and looking at the source code for org.slf4j.impl.SimpleLogger . 我想我借助此答案找出了答案,并查看了org.slf4j.impl.SimpleLogger的源代码。

The answer is you can't create a new log file as the logger properties are loaded only once - upon construction of the first logger instance. 答案是您无法创建新的日志文件,因为在构造第一个记录器实例时,记录器属性仅加载一次。 Subsequent loggers will use the same properties as the first one. 随后的记录器将使用与第一个相同的属性。

To set the file path to your liking, one simply has to set the following property, either in a file named simplelogger.properties on the classpath (eg by placing such a file under the resources directory) or through a -D JVM startup option. 要根据您的喜好设置文件路径,只需在类路径上名为simplelogger.properties的文件中设置以下属性simplelogger.properties (例如,通过将此类文件放置在resources目录下)或通过-D JVM启动选项。

The property name and syntax is simply: 属性名称和语法很简单:

org.slf4j.simpleLogger.logFile=your-file-path

See in this related answer for examples of providing a property through either of the two mentioned methods. 请参阅此相关答案,以获取通过上述两种方法之一提供属性的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM