使用準備 :
1. 請到Apache Log4j官網 download log4j的 library.
2. 將log4j-x.x.xx.jar 加入Classpath, 或由IDE進行設定.
使用說明 :
* 階層
* 輸出格式(Pattern)
簡單範例 :
* 將Log 訊息將會導到Console, 並且依照指定的Pattern列印出來.
- package gays.apache.log4j;
- import org.apache.log4j.ConsoleAppender;
- import org.apache.log4j.Level;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PatternLayout;
- /**
- * 簡介 : Here is a very simplistic example of a program implementing a SimpleLayout and FileAppender:
- * Log 訊息將會導到Console, 並且依照指定的Pattern列印出來.
- * 來源 : http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/log4j/log4j.html#LOG4J-Basics-Logger
- * @author JohnLee
- */
- public class Log4jConsoleAppender {
- public static void main(String args[]){
- Logger logger = Logger.getLogger(Log4jConsoleAppender.class);
- // Note, %n is newline
- String pattern = "Milliseconds since program start: %r %n";
- pattern += "Classname of caller: %C %n";
- pattern += "Date in ISO8601 format: %d{ISO8601} %n";
- pattern += "Location of log event: %l %n";
- pattern += "Message: %m %n %n";
- PatternLayout layout = new PatternLayout(pattern);
- ConsoleAppender appender = new ConsoleAppender(layout);
- logger.addAppender(appender);
- logger.setLevel((Level) Level.DEBUG);
- logger.debug("Here is some DEBUG");
- logger.info("Here is some INFO");
- logger.warn("Here is some WARN");
- logger.error("Here is some ERROR");
- logger.fatal("Here is some FATAL");
- }
- }
* 將Log訊息導到指定檔案
- package gays.apache.log4j;
- import org.apache.log4j.FileAppender;
- import org.apache.log4j.Level;
- import org.apache.log4j.Logger;
- import org.apache.log4j.SimpleLayout;
- /**
- * 簡介 : Here is a very simplistic example of a program implementing a SimpleLayout and FileAppender:
- * Log 訊息將會導到檔案 'output1.txt'裡, 預設該檔案會在Project目錄下.
- * 來源 : http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/log4j/log4j.html#LOG4J-Basics-Logger
- * @author JohnLee
- */
- public class Log4jSimpleFileAppender {
- public static void main(String args[]){
- Logger logger = Logger.getLogger(Log4jSimpleFileAppender.class);
- SimpleLayout layout = new SimpleLayout();
- FileAppender appender = null;
- try {
- appender = new FileAppender(layout, "output1.txt", false);
- } catch (Exception e) {
- }
- logger.addAppender(appender);
- logger.setLevel((Level) Level.DEBUG);
- logger.debug("Here is some DEBUG");
- logger.info("Here is some INFO");
- logger.warn("Here is some WARN");
- logger.error("Here is some ERROR");
- logger.fatal("Here is some FATAL");
- }
- }
* 利用Configuration 對Log4j進行設定Appender, Log level 與 Pattern
->Log4j.properties
->範例代碼 :
- package gays.apache.log4j;
- import org.apache.log4j.Logger;
- import org.apache.log4j.PropertyConfigurator;
- /**
- * 簡介 : 請參考以下相關Log4j教學, 在此會讀取Configuration獲取 Log4j的設定, 並跟據設定的Appender將訊息輸出.
- * * http://my.so-net.net.tw/idealist/Java/Log4j.html
- * * http://werdna1222coldcodes.blogspot.com/2009/11/javalog4j.html
- * * http://www.vaannila.com/log4j/log4j-configuration.html
- * * http://logging.apache.org/log4j/ ~ Apache log4j 官網
- * * http://supportweb.cs.bham.ac.uk/documentation/tutorials/docsystem/build/tutorials/log4j/log4j.html#LOG4J-Basics-Logger
- * * http://logging.apache.org/log4j/1.2/manual.html ~ Apache log4j 教學 (Short introduction to log4j)
- * @author JohnLee
- *
- */
- public class Log4jDemo {
- public static void main(String args[]){
- PropertyConfigurator.configure("bin/gays/apache/log4j/Log4j.properties");
- Logger logger = Logger.getLogger(Log4jDemo.class);
- // 對應的 Log4j.properties 設定要在等級 Info 之上才會顯示,所以logger.debug 不會出現
- logger.debug("Hello Log4j, this is debug message");
- // 以下的訊息會出現在 console 和 log file 中
- logger.info("Hi Log4j, this will appear in console and log file");
- logger.error("This is error message!!!");
- }
- }
補充說明 :
* http://my.so-net.net.tw/idealist/Java/Log4j.html
* http://werdna1222coldcodes.blogspot.com/2009/11/javalog4j.html
* http://www.vaannila.com/log4j/log4j-configuration.html
* http://logging.apache.org/log4j/ ~ Apache log4j 官網
* http://supportweb.cs.bham.ac.uk/documentation/tuto...log4j.html#LOG4J-Basics-Logger
* http://logging.apache.org/log4j/1.2/manual.html ~ Apache log4j 教學 (Short introduction to log4j)
謝謝您的範例說明
回覆刪除You are welcome! Hope this helps!
刪除感謝您的範例,非常清楚。
回覆刪除You are welcome and thanks for your feedback!
刪除