Java 如何使用SLF4J和SimpleLogger(Windows)写入子目录中的日志文件
我在Java应用程序中使用了SLF4JJava 如何使用SLF4J和SimpleLogger(Windows)写入子目录中的日志文件,java,path,slf4j,Java,Path,Slf4j,我在Java应用程序中使用了SLF4JSimpleLogger,我想将其配置为输出到当前工作目录中名为log的子目录中的文件 E.g. <cwd>/log/log-file.log 在OS X上,这与预期的一样工作-即在目录/log中创建名为log file.log的文件 但是,在Windows上,调用getLogger时会引发异常 Could not open [log/log-file.log]. Defaulting to System.err Reported except
SimpleLogger
,我想将其配置为输出到当前工作目录中名为log
的子目录中的文件
E.g. <cwd>/log/log-file.log
在OS X上,这与预期的一样工作-即在目录/log
中创建名为log file.log
的文件
但是,在Windows上,调用getLogger
时会引发异常
Could not open [log/log-file.log]. Defaulting to System.err
Reported exception:
java.io.FileNotFoundException: log\log-file.log (The system cannot find the path specified)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at org.slf4j.impl.SimpleLogger.computeTargetStream(SimpleLogger.java:219)
at org.slf4j.impl.SimpleLogger.init(SimpleLogger.java:201)
at org.slf4j.impl.SimpleLogger.<init>(SimpleLogger.java:262)
at org.slf4j.impl.SimpleLoggerFactory.getLogger(SimpleLoggerFactory.java:55)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:301)
Javadoc说,logfile
属性是“输出目标,可以是文件的路径”
如何在Windows上实现与在OS X上相同的行为—即将日志文件写入当前工作目录的子目录?我已经解决了这个问题。问题实际上与
SimpleLogger
无关,而是与FileOutputStream
有关。在Windows上,如果目录不存在,则需要先创建它。这在OS X上是不必要的。只是猜测。。。尝试在Windows上手动创建log
子目录。“log/log file.log”不是Windows路径。您需要在windows上使用windows表示法。尝试使用FileSystems.getDefault().getPath(“logs”,“log file.log”).toString()代码>我本以为两个平台上的行为都是一样的。也许我太天真了。
Could not open [log/log-file.log]. Defaulting to System.err
Reported exception:
java.io.FileNotFoundException: log\log-file.log (The system cannot find the path specified)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(FileOutputStream.java:270)
at java.io.FileOutputStream.<init>(FileOutputStream.java:213)
at java.io.FileOutputStream.<init>(FileOutputStream.java:101)
at org.slf4j.impl.SimpleLogger.computeTargetStream(SimpleLogger.java:219)
at org.slf4j.impl.SimpleLogger.init(SimpleLogger.java:201)
at org.slf4j.impl.SimpleLogger.<init>(SimpleLogger.java:262)
at org.slf4j.impl.SimpleLoggerFactory.getLogger(SimpleLoggerFactory.java:55)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)
at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:301)
System.setProperty(org.slf4j.impl.SimpleLogger.LOG_FILE_KEY, "log-file.log");