簡明log4j配置教程
阿新 • • 發佈:2017-09-13
開發 and led eap string ref str spa 級別
先準備好log4j需要對應的開發包:
apache-log4j-extras-1.2.17.jar slf4j-api-1.6.1.jar slf4j-log4j12-1.6.1.jar
然後就是在項目中準備一個log4j.xml開發文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j=‘http://jakarta.apache.org/log4j/‘> <!--appender --> <!-- STDOUT --> <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %p [%c] - %m%n" /> </layout> </appender><!-- 設置按天滾動方式生成日誌文件 --> <!-- 將日誌寫到文件中 --> <appender name="biFormat" class="org.apache.log4j.rolling.RollingFileAppender"> <!-- 設置日誌編碼 --> <param name="Encoding" value="UTF-8" /> <!-- 設置按天滾動方式生成日誌文件 --> <rollingPolicy class="org.apache.log4j.rolling.TimeBasedRollingPolicy"> <param name="FileNamePattern" value="/opt/test/log/bi_%d{yyyyMMdd}.log" /> </rollingPolicy> <!-- 設置日誌內容格式 --> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d{yyyy/MM/dd HH:mm:ss.SSS} [%F.%L]: %m [%t]%n" /> </layout> </appender> <!-- 設置按天滾動方式生成日誌文件 --> <!-- 將日誌寫到Linux系統日誌中 --> <appender name="syslogger" class="org.apache.log4j.net.SyslogAppender"> <!-- 設置閾值為ERROR級別,低於ERROR級別的日誌將不會寫入到系統中 --> <param name="Threshold" value="ERROR" /> <param name="SyslogHost" value="192.168.172.140" /> <param name="Facility" value="USER" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="bitool[%F.%L]: %m[%t]%n" /> </layout> </appender> <!-- 設置logger日誌名稱,在程序中引用 --> <logger name="logfile"> <!-- 設置日誌級別為DEBUG級別 --> <level value="debug" /> <appender-ref ref="biFormat" /> <appender-ref ref="syslogger" /> </logger> </log4j:configuration>
關於上面日誌格式常用占位符解釋:
%d: 當前時間,如果要指定格式,則可以yyyy MM dd HH mm ss SSS 代表 年、月、日、時、分、秒、毫秒
%F: 日誌所在代碼所在文件
%L: 日誌所在的行數
%m: 消息主體
%t: 程序所在線程
%n: 換行
%p: 當前日誌級別
示例程序代碼:
package org.lyk.action; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/test/*") public class Test { private Logger logger = LoggerFactory.getLogger("logfile"); @RequestMapping("testDemo") public ModelAndView testDemo(String msg) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); logger.info("This is for log file " + sdf.format(new Date())); logger.error("This is for sys log file " + sdf.format(new Date())); return new ModelAndView("/index.jsp"); } }
輸出效果
日誌文件:
Linux syslog:
程序代碼:
package org.lyk.action; import java.text.SimpleDateFormat; import java.util.Date; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; @Controller @RequestMapping("/test/*") public class Test { private Logger logger = LoggerFactory.getLogger("logfile"); @RequestMapping("testDemo") public ModelAndView testDemo(String msg) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"); logger.info("This is for log file " + sdf.format(new Date())); logger.error("This is for sys log file " + sdf.format(new Date())); return new ModelAndView("/index.jsp"); } }
https://pan.baidu.com/s/1kVghs83
歡迎加入QQ群參與討論. 422683877
簡明log4j配置教程