hi ,i want to print e.printStackTrace in File using log 4j but its giving error ........
these r my files

web1.xml
<context-param>
<param-name>driverName</param-name>
<param-value>com.microsoft.jdbc.sqlserver.SQLServerDriver</param-value>
</context-param>
<context-param>
<param-name>log4j1</param-name>
<param-value>WEB-INF/classes/log4j.properties</param-value>

</context-param>


MyListener.java


publicclass MyListener implements ServletContextListener{
Connection con;
public void contextInitialized(ServletContextEvent sce)
{
try{
ServletContext sctx=sce.getServletContext();
String log1=sctx.getInitParameter("log4j1");
sctx.setAttribute("log1",log1);
}
catch(SQLException e)
{
e.printStackTrace();
}
}
public void contextDestroyed(ServletContextEvent sce1)
{
try
{
ServletContext ctx1=sce1.getServletContext();
con=(Connection)ctx1.getAttribute("Connection");
con.close();
}
catch(Exception e1)
{
e1.printStackTrace();
}
}

}

Servlet:
-------------
----------------


----------------------

package Student;
import java.text.SimpleDateFormat;
import java.util.*;

import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.lang.Object;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
publicclass AddTimeTableBean extends Object {
Connection con;
ResultSet rs;
Statement st;
String b;
boolean flag;
private String UserID;
private static Logger logger;
public AddTimeTableBean()
{}

public AddTimeTableBean(Connection con)
{
PropertyConfigurator.configure("log4j.properties");
logger= Logger.getLogger(GetHeaderBean.class.getName());

try
{
this.con = con;
st=con.createStatement();
}
catch (Exception e)
{
e.printStackTrace();
}
}

public void doProcess()
{
try
{
//select stmts

}
catch(Exception e)
{
e.printStackTrace();
logger.error("e",e.printStackTrace();//main error

}

log4j.properties


log4j.rootLogger=ERROR,stdout,R
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=crmngt.log
log4j.appender.R.MaxFileSize=1000KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=10
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

just a quick scan revealed at least one syntax error in the Java code.
Didn't bother to try to make sense of the rest after that, if you can't get it to compile there's little sense going further.

Anyway, it's a bad idea to have your web app keep its own log4j config (and I suppose jar file). Have the appserver handle logging for you.

I am also working on similar project.

Note the following documentation:

public void error(java.lang.Object message, java.lang.Throwable t)

But you are passing a null object instead of throwable.

logger.error("e",e.printStackTrace();//main error

You may simplify this by:

logger.error(e.getMessage());

Let me know if it works for you. It will also help me in my project.
Thanks.

This article has been dead for over six months. Start a new discussion instead.