看了很多篇文章, 都發現jboss 上面 要跑含 log4j 的code 像是會有點問題. 看很多網頁, 沒有真的去試試看還不知道會是怎樣. 是了一兩個在 jboss 5.0 jboss 4.0 的解決方案, 好像不是適用於 jboss 6.0 M3.
就硬著把 ${JBOSS_HOME}/server/default/deply 看一輪, 發現到有一個檔案 jboss-logging.xml 裡面有用到jboss 自己的 log , 還有幾個appended 是用 log4j. 想說那就來試試看吧.
想說不用太複雜可以跑就好, 就找 ${JBOSS_HOME}/server/standard/ 來改好了. 要把log4j.jar 放見 ${JBOSS_HOME}/server/stardand/lib 裡面, 發現哇勒 裡面已經有放 一個log4j.jar. 跟在default裡面的lib 不一樣, default/lib 裡面一個東西都沒有, 這邊居然有. 簡單寫一個 web app, 用到log4j 去放.
修改 ${JBOSS_HOME}/server/stardand/deploy/jboss-logging.xml
1. 我是選用 他原有的 FILE 下去修改 , 新增到 檔案裡頭
<size-rotating-file-handler
file-name="${jboss.server.log.dir}/app.log"
name="FILE-APP"
autoflush="true"
append="true"
rotate-size="500k"
max-backup-index="5">
<error-manager>
<only-once/>
</error-manager>
<formatter>
<pattern-formatter pattern="%d %-5p [%c] (%t) %m%n"/>
</formatter>
</size-rotating-file-handler>
2. 新增相對的 logger
<logger category="test.logger">
<level name="DEBUG"/>
<handlers>
<handler-ref name="CONSOLE"/>
<handler-ref name="FILE-APP"/>
</handlers>
</logger>
3. 撰寫測試 log4j 的code
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
@WebServlet(name = "Test", urlPatterns = {"/Test"})
public class Test extends HttpServlet {
private static Logger log = Logger.getLogger("test.logger");
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
log.debug("Log Debug");
log.info("Log Info");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
測試一下, 發現很正常, 可以在jboss container 下面, 正常的使用.不用像 jboss docs 裡面寫得, 得用 jboss 包過得 Logger 下去做事情.
ps: 測試的過程中, 我將 standard/lib/log4j.jar copy to default/lib/ 來使用