http://blog.naver.com/sj99yang/140003731319
작성자 : 양승진
LastUpdate : 2004 / 06 / 30
메일 : sj99yang@naver.com
0.
OS : Windows 2000
WAS : Tomcat 4.1.30
파일경로 : WebApp = C:\Apache_Group\Tomcat4130\webapps\ROOT
1. Log4j 초기화에 필요한 서블릿 생성
파일경로 : %WebApp%/WEB-INF/classes/Log4jInit.class
------------------------------------------------------------------------------------
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;
public class Log4jInit extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=EUC-KR";
//Initialize global variables
public void init() throws ServletException {
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file is not set, then no point in trying
if(file != null) {
PropertyConfigurator.configure(prefix+file);
}//end if
}//end init
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
//Process the HTTP Post request
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
//Clean up resources
public void destroy() {
}
}
----------------------------------------------------------------------------
2. 톰켓 서버 작동시 초기화 서블릿 호출 web.xml 에 추가할 내용
파일 경로 : %WebApp%/WEB-INF/web.xml
------------------------------------------------------------------------------
<servlet>
<servlet-name>log4jinit</servlet-name>
<servlet-class>Log4jInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
<description>log4j.properties</description>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>log4jinit</servlet-name>
<url-pattern>/log4jinit</url-pattern>
</servlet-mapping>
-------------------------------------------------------------------------
3. log4j.properties 파일 ( 콘솔에서 로그보이기 셋팅 )
파일 경로 : %WebApp%/WEB-INF/classes/log4j.properties
-----------------------------------------------------------------------------
# For the general syntax of property based configuration files see the
# documenation of org.apache.log4j.PropertyConfigurator.
# The root category uses the appender called A1. Since no priority is
# specified, the root category assumes the default priority for root
# which is DEBUG in log4j. The root category is the only category that
# has a default priority. All other categories need not be assigned a
# priority in which case they inherit their priority from the
# hierarchy.
# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1
# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender
# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
-----------------------------------------------------------------------------
4. Test Application
파일 경로 : %WebApp%/WEB-INF/classes/InitUsingLog4JProperties.class
----------------------------------------------------------------------------
public class InitUsingLog4JProperties {
//--------------------------------------------------------------------------
// Constants:
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Protected Variables:
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Private Variables:
//--------------------------------------------------------------------------
private static Logger logger =
Logger.getLogger(InitUsingLog4JProperties.class);
//--------------------------------------------------------------------------
// Constructors:
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Public Methods:
//--------------------------------------------------------------------------
public static void main(String argv[]) {
// Add a bunch of logging statements ...
logger.debug("Hello, my name is Homer Simpson.");
logger.debug("Hello, my name is Lisa Simpson.");
logger.debug("Hello, my name is Marge Simpson.");
logger.debug("Hello, my name is Bart Simpson.");
logger.debug("Hello, my name is Maggie Simpson.");
logger.info("We are the Simpsons!");
logger.info("Mmmmmm .... Chocolate.");
logger.info("Homer likes chocolate");
logger.info("Doh!");
logger.info("We are the Simpsons!");
logger.warn("Bart: I am through with working! Working is for chumps!" +
"Homer: Son, I'm proud of you. I was twice your age before " +
"I figured that out.");
logger.warn("Mmm...forbidden donut.");
logger.warn("D'oh! A deer! A female deer!");
logger.warn("Truly, yours is a butt that won't quit." +
"- Bart, writing as Woodrow to Ms. Krabappel.");
logger.error("Dear Baby, Welcome to Dumpsville. Population: you.");
logger.error("Dear Baby, Welcome to Dumpsville. Population: you.",
new IOException("Dumpsville, USA"));
logger.error("Mr. Hutz, are you aware you're not wearing pants?");
logger.error("Mr. Hutz, are you aware you're not wearing pants?",
new IllegalStateException("Error !!"));
logger.fatal("Eep.");
logger.fatal("Mmm...forbidden donut.",
new SecurityException("Fatal Exception"));
logger.fatal("D'oh! A deer! A female deer!");
logger.fatal("Mmmmmm .... Chocolate.",
new SecurityException("Fatal Exception"));
}
//--------------------------------------------------------------------------
// Protected Methods:
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Private Methods:
//--------------------------------------------------------------------------
//--------------------------------------------------------------------------
// Nested Top-Level Classes or Interfaces:
//--------------------------------------------------------------------------
}
-------------------------------------------------------------------------------
작성자 : 양승진
LastUpdate : 2004 / 06 / 30
메일 : sj99yang@naver.com