1. 程式人生 > 程式設計 >springboot使JUL實現日誌管理功能

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

第四步:日誌檔案檢視相應的結果

springboot使JUL實現日誌管理功能

到此這篇關於springboot使JUL實現日誌管理功能的文章就介紹到這了,更多相關springboot日誌管理內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!