Eclipse + Tomcat + log4j (xml, properties) w/ slf4j
1. [web.xml] servlet 등록 (지정한 param-value 경로에 log4j.xml 또는 log4j.properties 있어야 함)
...
<servlet>
<servlet-name>Log4JInitServlet</servlet-name>
<servlet-class>lawmin.util.Log4JInitServlet</servlet-class>
<init-param>
<param-name>log4j-conf-location</param-name>
<param-value>WEB-INF/classes/log4j.xml</param-value>
</init-param>
<load-on-startup>4</load-on-startup>
</servlet>
...
2. [Log4JInitServlet] class, jar 생성 (xml 또는 properties 를 읽음)
package lawmin.util;
import java.io.File;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.xml.DOMConfigurator;
public class Log4JInitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init(ServletConfig config) throws ServletException {
System.out.println("[log4j] Initializing...");
String log4jLocation = config.getInitParameter("log4j-conf-location");
ServletContext sc = config.getServletContext();
if (log4jLocation == null) {
System.err.println("[log4j] log4j-conf-location parameter not specified. Initialize with BasicConfigurator");
BasicConfigurator.configure();
} else {
String webAppPath = sc.getRealPath("/");
String log4jConfPath = webAppPath + log4jLocation;
File file = new File(log4jConfPath);
if (file.exists()) {
System.out.println("Initializing log4j with: " + log4jConfPath);
if(log4jLocation.endsWith(".properties")) {
PropertyConfigurator.configure(log4jConfPath);
}
else if(log4jLocation.endsWith(".xml")) {
DOMConfigurator.configure(log4jConfPath);
}
else {
System.err.println("[log4j] no xml or properties file specified. Initialize with BasicConfigurator");
BasicConfigurator.configure();
}
}
else {
System.err.println("[log4j] " + log4jConfPath + " not found. Initialize with BasicConfigurator");
BasicConfigurator.configure();
}
}
super.init(config);
}
}
3. [log4j.xml] 샘플
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//LOGGER" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console-debug" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}] %5p [%c.%M(%L)] %m %n" />
</layout>
</appender>
<appender name="console-info" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss}] %5p (%F[%M]:%L) - %m%n" />
</layout>
</appender>
<!-- 3rd party Loggers -->
<logger name="lawmin.manager">
<level value="error" />
</logger>
<root>
<priority value="debug" />
<appender-ref ref="console-debug" />
</root>
</log4j:configuration>
4. [Log4JInitServlet.jar] 를 Eclipse - Run Configurations - Server - Apache Tomcat - 해당 서버 선택 - Classpath - User Entries - Add JARs 또는 Add External JARs 로 추가
5. 서버 시작하면 로그가 설정한 규칙대로 떨어지는 것을 확인할 수 있다.