折腾:
【已解决】Java中好用的Log日志库
后,发现此处tinlylog貌似不错,去试试。
想要通过Maven方式安装tinylog,结果失败:
【未解决】Intelli IDEA中如何使用Maven管理依赖的Java库和包
放弃Maven,还是自己导入jar吧。
去导入到IDEA中
结果无意间发现:
【已解决】IntelliJ IDEA从Maven方式添加第三方库tinylog
然后参考:
去使用试试
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的
然后再去找如何配置
用:
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");
了。