最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【已解决】tinylog中文件的FileWriter传入带路径的文件名无效

Log crifan 34浏览 0评论
折腾:
【记录】IntelliJ IDEA中使用Java的日志库tinylog记录日志
期间,另外发现FileWriter中设置的文件名,如果带路径了:
"../log/EmulateLoginBaidu.log"
好像不起效果?
难道是此处log目录没有创建就不行?
那去在src上一级创建log目录试试
再去运行试试
还是无法生成对应log文件到对应目录:
FileWriter (tinylog 1.3.5 API)
中对于文件名中包含路径也没有解释
无意间看到别处:
https://dzone.com/articles/how-tinylog-12-will-simplify-your-logging
配置文件中的:
tinylog.writer.filename = ${home}/logs/dummy.log
感觉是:
可以借用项目根目录的
-》那要去找找,此写法,能否直接传递到FileWriter的filename中
tinylog file writer log designate
java – using tinylog to write loggings into tomcat’s log-folder – Stack Overflow
没太懂,用什么:tinylog-jul artifact
tinylog: Download
How to write specific logs to different files ? · Issue #56 · pmwmedia/tinylog
Make it possible to define into which file a log entry should go, allowing multiple files at the same time · Issue #6 · pmwmedia/tinylog
tinylog  log path
java – How to write specific logs to different files using tinylog library? – Stack Overflow
java – How to create config properties for tinylog at runtime with User preferences – Stack Overflow
https://github.com/pmwmedia/tinylog/wiki/Configuration#environment-variables
就一个:${HOME}
Java Code Examples org.pmw.tinylog.Configurator
可以借鉴:
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);
也是可以起到同样效果的。

转载请注明:在路上 » 【已解决】tinylog中文件的FileWriter传入带路径的文件名无效

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
20 queries in 0.070 seconds, using 9.20MB memory