String class_path = getClass().getResource("/").getPath();
PropertyConfigurator.configure(class_path+"log4j.properties");//获取 log4j 配置文件
Logger logger = Logger.getLogger(IndexServlet.class ); //获取log4j的实例,IndexServlet是当前java文件的名字
在web.xml中写
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>IndexServlet</servlet-name>
<servlet-class>com.test.yjw.IndexServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>IndexServlet</servlet-name>
<url-pattern>/indexservlet</url-pattern>
</servlet-mapping>
就会在系统启动时加载