•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Java section within the Software Development category of DaniWeb, a massive community of 456,509 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,661 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Java advertiser: Lunarpages Java Web Hosting
Views: 1293 | Replies: 3
![]() |
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
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
Nikki
•
•
Join Date: Nov 2004
Location: Netherlands
Posts: 5,752
Reputation:
Rep Power: 19
Solved Threads: 200
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.
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.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
•
•
Join Date: Apr 2006
Location: City of God
Posts: 163
Reputation:
Rep Power: 3
Solved Threads: 10
I am also working on similar project.
Note the following documentation:
But you are passing a null object instead of throwable.
You may simplify this by:
Let me know if it works for you. It will also help me in my project.
Thanks.
Note the following documentation:
public void error(java.lang.Object message, java.lang.Throwable t)
logger.error("e",e.printStackTrace();//main errorlogger.error(e.getMessage());
Thanks.
A Perfect World
![]() |
•
•
•
•
•
•
•
•
DaniWeb Java Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Installing Windows 98 On VMware. Floppy problem (Windows 9x / Me)
- Windows XP keeps restarting since a new video card (Windows NT / 2000 / XP / 2003)
- Redhat Linux 6.2 - ipop3d problem? (*nix Software)
- Problem with T720 (Gadgets and Gizmos)
- Connection Problems (Networking Hardware Configuration)
- Encoding (Unicode) problem in IE 6.0 (Web Browsers)
- .htaccess mod_rewrite problem (Linux Servers and Apache)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
- Problem with Windows Update and WinXP (Web Browsers)
Other Threads in the Java Forum
- Previous Thread: Help editing table
- Next Thread: wanted project titles



Linear Mode