折腾:
【记录】IntelliJ IDEA中使用Java的日志库tinylog记录日志
期间,另外发现FileWriter中设置的文件名,如果带路径了:
"../log/EmulateLoginBaidu.log"
好像不起效果?
难道是此处log目录没有创建就不行?
那去在src上一级创建log目录试试
再去运行试试
还是无法生成对应log文件到对应目录:
中对于文件名中包含路径也没有解释
无意间看到别处:
配置文件中的:
tinylog.writer.filename = ${home}/logs/dummy.log
感觉是:
可以借用项目根目录的
-》那要去找找,此写法,能否直接传递到FileWriter的filename中
tinylog file writer log designate
没太懂,用什么:tinylog-jul artifact
tinylog log path
java – How to create config properties for tinylog at runtime with User preferences – Stack Overflow
就一个:${HOME}
可以借鉴:
public void init(String outputDir, RunnerTestResults runnerTestResults, File chromeExecutable) { this.runnerTestResults = runnerTestResults; this.chromeExecutable = chromeExecutable; this.outputDir = new File(outputDir); this.outputDir.mkdirs(); Configurator.defaultConfig() .writer(new FileWriter(new File(this.outputDir, "log.txt").getAbsolutePath())) .level(Level.INFO) .activate(); }
去实现自己的
然后用代码:
// String logFilename = "../log/EmulateLoginBaidu.log"; // FileWriter fileWriter = new FileWriter(logFilename); // File logFolder = new File("../log"); File logFolder = new File(".." + File.separator + "log"); logFolder.mkdirs(); String logFilename = "EmulateLoginBaidu.log"; File logFile = new File(logFolder, logFilename); String logFileFullPath = logFile.getAbsolutePath(); FileWriter fileWriter = new FileWriter(logFileFullPath);
调试发现:
即:
logFileFullPath竟然是:
/Users/crifan/dev/dev_root/java/EmulateLoginBaidu/../log/EmulateLoginBaidu.log
而不是:
/Users/crifan/dev/dev_root/java/EmulateLoginBaidu/src/log/EmulateLoginBaidu.log
所以看来是之前自己搞错了:
以为此处代码中的:
public static void main(String[] args) throws IOException {
中的代码去创建log文件,是以当前src目录为基准的
但是此处,至少是File是以项目根目录为准的
所以改为:
File logFolder = new File("log");
看起来是对的:
然后调试完毕,即可在项目根目录下的log中生成log文件:
然后再去试试:
String logFilename = "log/EmulateLoginBaidu.log"; FileWriter fileWriter = new FileWriter(logFilename);
结果也是正常的:
再去试试:
String logFilename = "{HOME}/logs/EmulateLoginBaidu.log"; FileWriter fileWriter = new FileWriter(logFilename);
结果:
不行,会多出{HOME}这个目录:
【总结】
此处,给tinylog的FileWriter传入的文件名,之前是:
"../log/EmulateLoginBaidu.log"
其实是以为:文件目录是以当前代码为基础的 -》即 EmulateLoginBaidu/src
但是实际上此处是:项目根目录为基础的-》即:EmulateLoginBaidu
所以此处,传入:
"log/EmulateLoginBaidu.log"
代码:
String logFilename = "log/EmulateLoginBaidu.log"; FileWriter fileWriter = new FileWriter(logFilename); ConsoleWriter consoleWriter = new ConsoleWriter(); Level fileWriterLevel = Level.TRACE; Level consoleWriterLevel = Level.DEBUG; String consoleFormat = "{date:YYMMdd HH:mm:ss} {class}.{method} {{level}|min-size=8}: {message}"; String fileFormat = "{date:yyyyMMdd HH:mm:ss} [{thread}] {class}.{method} {{level}|min-size=8}: {message}"; Configurator.currentConfig() .writer(consoleWriter, consoleWriterLevel, consoleFormat) .addWriter(fileWriter, fileWriterLevel, fileFormat) .activate(); Logger.trace("trace"); Logger.debug("debug"); Logger.info("info"); Logger.warn("warn"); Logger.error("error");
即可:
自动,在项目根目录下,创建对应log文件夹,且生成log日志文件:
log/EmulateLoginBaidu.log
注:
另外一种更加复杂的写法,是借用File对象去创建文件夹,再去传入log文件的绝对路径:
File logFolder = new File("log"); logFolder.mkdirs(); String logFilename = "EmulateLoginBaidu.log"; File logFile = new File(logFolder, logFilename); String logFileFullPath = logFile.getAbsolutePath(); FileWriter fileWriter = new FileWriter(logFileFullPath);
也是可以起到同样效果的。