2014. 5. 19. 13:42ㆍEtc../log
* Log4j 설정방법
1. log4j.jar 를 Download 받아 lib 폴더에 붙혀 넣는다.
2. log4j.properties 파일을 아래와 같이 작성해준다.
( 옵션에 대한 설명은 생략 )
###############################################################################
#
# log4j
#
###############################################################################
log4j.rootLogger = DEBUG, stdout, dailyfile
# Console Appender
log4j.appender.console.Threshold = DEBUG
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %p [%c] - %m%n
# Console output...
log4j.appender.stdout.Threshold = DEBUG
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%d{MMdd HHmmss}] %5p %F:%L:%M - %m%n
# Daily file log
log4j.appender.dailyfile.Threshold = DEBUG
log4j.appender.dailyfile = org.apache.log4j.DailyRollingFileAppender
log4j.appender.dailyfile.DatePattern = '.'yyyy-MM-dd
log4j.appender.dailyfile.layout = org.apache.log4j.PatternLayout
log4j.appender.dailyfile.layout.ConversionPattern =[%d{MMdd HHmmss}] %5p %F:%L:%M - %m%n
log4j.appender.dailyfile.File = ${catalina.home}/logs/testLog.log <- 로그파일 경로
#절대경로혹은 상대경로를 사용
3. log4j.properties 파일을 설정된 Classpath(보통 : WEB-INF/classes/)안에 넣어준 후
/WEB-INF/web.xml 에 아래를 추가한다.
<servlet>
<servlet-name>log4j-init</servlet-name>
<servlet-class>com.Log4jInit</servlet-class>
<init-param>
<param-name>log4j-init-file</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
4. Log4jInit.java 추가
package com;
import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
public class Log4jInit extends HttpServlet {
public void init()
{
String prefix = getServletContext().getRealPath("/");
String file = getInitParameter("log4j-init-file");
// if the log4j-init-file context parameter is not set, then no point in trying
if(file != null){
PropertyConfigurator.configure(prefix+file);
System.out.println("Log4J Logging started: " + prefix+file);
}
else{
System.out.println("Log4J Is not configured for your Application: " + prefix + file);
}
}
}
5. 로그가 찍히는 지 확인
TestServlet.java
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("/testServlet")
public class testServlet extends HttpServlet {
private Logger log = Logger.getLogger(testServlet.class);
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("test");
log.debug("test logger " );
}
}
'Etc.. > log' 카테고리의 다른 글
[log4j] log 파라미터 추가 (0) | 2014.03.07 |
---|