I'm currently doing java/jsp project and I couldn't initialize log4j correctly. What I want is when Apache Tomcat server starts, servlet should run pointing to "init.jsp". Within "init.jsp" there is the location where "log4j.xml" path. I want initialize log4j when aparche tomcat starts. followings are what I have done.
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>Project01</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>init</servlet-name>
<jsp-file>/init.jsp</jsp-file>
<load-on-startup>1</load-on-startup>
</servlet>
</web-app>
init.jsp
<%@ page language="java" import="java.io.*,java.sql.*,javax.naming.*,java.util.*" errorPage="" buffer="none" %>
<%@page import="com.company.util.LogUtil"%>
<% System.out.println("+++++++++++++++++++");
%>
<%!
public void jspInit( javax.servlet.jsp.JspWriter out2)throws java.io.IOException{
System.out.println("+++++++++Init++++++++++++");
String rPath = getServletConfig().getServletContext().getRealPath("");
out2.println("rPath "+rPath);
//location of log4j.xml file
LogUtil.init(rPath + "/conf/log4j.xml");
LogUtil.getEventLog().debug("Log initialized");
}
%>
<%
jspInit(out);
%>
The problem is when I start the server, Server doesn't start "init.jsp". So console says
log4j:WARN No appenders could be found for logger (Login).
log4j:WARN Please initialize the log4j system properly.
But when I start "init.jsp" manually by right click on "init.jsp"page-> run as-> run on server, Then log4j works and logs are logging correctly.
Possible error point is "web.xml" dosen't call to "init.jsp" correctly. So "init.jsp" dosen't work when server starts. But I start "init.jsp" manually. All works fine.