最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【记录】IntelliJ IDEA中使用Java的日志库tinylog记录日志

Java crifan 1032浏览 0评论
折腾:
【已解决】Java中好用的Log日志库
后,发现此处tinlylog貌似不错,去试试。
想要通过Maven方式安装tinylog,结果失败:
【未解决】Intelli IDEA中如何使用Maven管理依赖的Java库和包
放弃Maven,还是自己导入jar吧。
tinylog: Download
-》
去导入到IDEA中
结果无意间发现:
【已解决】IntelliJ IDEA从Maven方式添加第三方库tinylog
然后参考:
How Tinylog 1.2 Simplifies Your Logging – DZone Java
tinylog: Lightweight Logging Framework for Java
去使用试试
tinylog: Documentation
tinylog: Logging
tinylog: Configuration
Configurator (tinylog 1.3.5 API)
        Logger.trace("trace");
        Logger.debug("debug");
        Logger.info("info");
        Logger.warn("warn");
        Logger.error("error");

        String baiduUrl = "
http://www.baidu.com";
//        Logger.info("baiduUrl=%s", baiduUrl);
        Logger.info("baiduUrl={}", baiduUrl);
然后输出是:
2018-11-10 11:52:28 [main] EmulateLoginBaidu.main()
INFO: info
2018-11-10 11:52:28 [main] EmulateLoginBaidu.main()
WARNING: warn
2018-11-10 11:52:28 [main] EmulateLoginBaidu.main()
ERROR: error
很明显,此处的低于debug的输出都没显示,
应该默认是debug的level的
然后再去找如何配置
FileWriter (tinylog 1.3.5 API)
Level (tinylog 1.3.5 API)
用:
String logFormat = "{date:yyyyMMdd HH:mm:ss} [{thread}] {class}.{method} {level}:\t{message}";
输出效果不够好,level的字符串,没有统一固定的宽度,即便是加了\t,效果还是不行:
然后换成:
import org.pmw.tinylog.Logger;
import org.pmw.tinylog.Level;
import org.pmw.tinylog.Configurator;
import org.pmw.tinylog.writers.ConsoleWriter;
import org.pmw.tinylog.writers.FileWriter;

        String logFilename = "EmulateLoginBaidu.log";
        FileWriter fileWriter = new FileWriter(logFilename);
        ConsoleWriter consoleWriter = new ConsoleWriter();

        Level fileWriterLevel = Level.TRACE;
        Level consoleWriterLevel = Level.DEBUG;
//        String logFormat = "{date:yyyyMMdd HH:mm:ss} [{thread}] {class}.{method} {level}:\t{message}";
        String logFormat = "{date:yyyyMMdd HH:mm:ss} [{thread}] {class}.{method} {{level}|min-size=8}: {message}";

        Configurator.currentConfig()
                .writer(consoleWriter, consoleWriterLevel)
                .addWriter(fileWriter, fileWriterLevel)
                .formatPattern(logFormat)
                .activate();

        Logger.trace("trace");
        Logger.debug("debug");
        Logger.info("info");
        Logger.warn("warn");
        Logger.error("error");
基本上达到要的效果:
但是:
【已解决】tinylog中如何初始化同时给console和file传入不同的level和format的配置
以及:
【已解决】tinylog中文件的FileWriter传入带路径的文件名无效
然后现在的相关代码是:
  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");
再去想办法在当前类的外部,新建独立的函数:
【无法解决】Java中如何实现类外部的全局函数
然后只能去建个其他类,放到static函数中了。
然后改为:
    public static void initLogger(String logFilename){
        System.out.println("logFilename=" + logFilename);

        FileWriter fileWriter = new FileWriter(logFilename);
        ConsoleWriter consoleWriter = new ConsoleWriter();

        Level fileWriterLevel = Level.TRACE;
//        Level consoleWriterLevel = Level.DEBUG;
        Level consoleWriterLevel = Level.INFO;
        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");
    }
}
调用:
String logFilename = "log/EmulateLoginBaidu.log";
CrifanUtil.initLogger(logFilename);
后续即可:
Logger.info("logFilename={}", logFilename);
Logger.info("Try Emulate Login Baidu");
了。

转载请注明:在路上 » 【记录】IntelliJ IDEA中使用Java的日志库tinylog记录日志

发表我的评论
取消评论

表情

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

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
83 queries in 0.175 seconds, using 22.01MB memory