1. 下載log4j的jar檔 (log4j.1.12.17.jar) 2. 放到C:\Users\1409035\workspace\NewsstandWS\web\WEB-INF\lib 底下 3. 新增一個log4j_WS.properites檔放到 C:\Users\1409035\workspace\NewsstandWS\web\WEB-INF\properties 底下 設定內容直接參考code 4. 新增 Log4JInitServlet.java 到 C:\Users\1409035\workspace\NewsstandWS\src\java\com\ws\controller 做為初始化用 5. 修改 web.xml 新增如下6. 使用範例 final Logger log = Logger.getLogger(GetZinioDataJobs.class); log.info("orderBook info message"); 參考: http://www.programcreek.com/2009/06/a-simple-example-to-show-how-to-use-java-properties-file/ [^] http://www.avajava.com/tutorials/lessons/how-do-i-initialize-log4j-in-a-web-application.html [^] http://www.codejava.net/coding/how-to-initialize-log4j-for-java-web-application [^] http://toyangel.pixnet.net/blog/post/32953401-eclipse-%E4%BD%BF%E7%94%A8-log4j-%E7%9A%84%E5%BF%AB%E9%80%9F%E8%A8%AD%E5%AE%9A [^] Log4JInitServlet com.ws.controller.Log4JInitServlet log4j-properties-location /WEB-INF/properties/log4j_WS.properties 1
簡單版:
log4j.properties 直接放到src/java下面 web.xml 也可拿掉上述所新添加的部分。 =========================================== 原因: tomcat 啟動時的順序--> tomcat --> AP(newsstands) -->看底下web-inf 裡面的classes jar, 因為log4j 已經有default , 且我又將 properties 命名為 logj4.properties 所以他default找的到這隻, 如果我不命名為 logj4.properties, 就要遵循 本篇一開始的步驟 ============================
將log4j改為 大於 10M 就產生新的檔案
# A2 is set to be a file # produce log record document every day #log4j.appender.A2 = org.apache.log4j.FileAppender log4j.appender.A2 = org.apache.log4j.RollingFileAppender log4j.appender.A2.layout = org.apache.log4j.PatternLayout log4j.appender.A2.layout.ConversionPattern = [%d{yyyy-MM-dd HH:mm}][%p][%C-%L] %m%n log4j.appender.A2.File =/volume1/homes/tomcatupload/logs/system_notice.log log4j.appender.A2.MaxFileSize=10MB log4j.appender.A2.DatePattern = '.'yyyyMMdd-HHmm
log4j寫到不同file ======================================================================= log4j.rootLogger=TRACE, stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.appender.debugLog=org.apache.log4j.FileAppender log4j.appender.debugLog.File=logs/debug.log log4j.appender.debugLog.layout=org.apache.log4j.PatternLayout log4j.appender.debugLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.appender.reportsLog=org.apache.log4j.FileAppender log4j.appender.reportsLog.File=logs/reports.log log4j.appender.reportsLog.layout=org.apache.log4j.PatternLayout log4j.appender.reportsLog.layout.ConversionPattern=%d [%24F:%t:%L] - %m%n log4j.category.debugLogger=TRACE, debugLog log4j.additivty.debugLogger=false log4j.category.reportsLogger=DEBUG, reportsLog 可自訂 可自訂 log4j.additivty.reportsLogger=false [The configure the loggers in the Java code thusly:] static final Logger debugLog = Logger.getLogger("debugLogger"); static final Logger resultLog = Logger.getLogger("reportsLogger"); ========================================================================= [Do you want output to go to stdout? If not, change the first line of log4j.properties to:] log4j.rootLogger=OFF and get rid of the stdout lines. 參考http://stackoverflow.com/questions/9652032/how-can-i-create-2-separate-log-files-with-one-log4j-config-file [^]
沒有留言:
張貼留言