springboot使JUL实现日志管理功能

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot使JUL实现日志管理功能

xgysimida   2020-09-27 我要评论

本文着重讲解了springboot使JUL实现日志管理功能,本文分步骤为大家做详细介绍,希望能够帮助到您,欢迎大家阅读和收藏

第一步:设置logging.properties的内容(放在resource文件夹下面)

#输出两种方式
handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler
.level= FINE
#对日志的输出进行设置(主要是file类)
#java.util.logging.FileHandler.pattern = %h/java%u.log
#下面的是输出到制定的目录下
java.util.logging.FileHandler.pattern = D:\\software\\idea\\idealianxicode\\springboot1\\src\\main\\resources/java%u.log
#日志限制大小
java.util.logging.FileHandler.limit = 5000
java.util.logging.FileHandler.count = 1
#设置输出格式
java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter
#对文件设置输出编码格式(因为包含中文字符)
java.util.logging.FileHandler.encoding = UTF-8
#对日志进行追加
java.util.logging.FileHandler.append = true
 
 
#下面主要是为控制台设置输出格式
java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

第二步:编写测试程序

 @Test
  public void test() throws IOException {
    InputStream resourceAsStream = testMd5.class.getClassLoader().getResourceAsStream("logging.properties");
    LogManager logManager = LogManager.getLogManager();
    logManager.readConfiguration(resourceAsStream);
    Logger logger = Logger.getLogger("com.testMd5");
    int age = 3;
    String name ="myName";
    logger.info("你的姓名是:"+name+"你的年龄是:"+age);
    logger.fine("看看输出了吗");
    logger.info("this is a test data");
 
  }

第三步:控制台查看相应的输出结果

九月 27, 2020 12:15:59 上午 com.test.testMd5 test
信息: 你的姓名是:myName你的年龄是:3
九月 27, 2020 12:15:59 上午 com.test.testMd5 test
详细: 看看输出了吗
九月 27, 2020 12:15:59 上午 com.test.testMd5 test
信息: this is a test data

第四步:日志文件查看相应的结果

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们