log4j for webapp ..how to remove extra info getting printed

H

harryos

hi,
I tried log4j for logging in my web app which I deployed on tomcat6.I
wanted to replace all the System.out.println() statements in my
servlets and jsps which I previously used to track the control flow.
I created log4j.properties to write to mylog.log in my application
directory
------------
log4j.rootLogger=DEBUG,myLogFile
log4j.appender.myLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.myLogFile.File=mylog.log
log4j.appender.myLogFile.MaxFileSize=100KB
log4j.appender.myLogFile.MaxBackupIndex=2
log4j.appender.myLogFile.layout=org.apache.log4j.PatternLayout
--------------

and used the Logger instance in my servlet code as below
-------------------------------------------------------------
public class PostBean extends HttpServlet {
....
private Logger log;
public PostBean(){
log=Logger.getLogger("newwebapplog");
}
public void setUrl(String url){
if (url != null && !(url.equals(""))){
this.url=url;
log.debug("url set as :"+url);
}
}
....
}

I am getting these debug() statements printed to mylog.log file.
eg:
Dec 15, 2008 12:21:52 PM: DEBUG [http-8080-1] url set as
This is exactly what I wanted.
But a lot of other info is also being printed into it
eg:
Dec 15, 2008 12:21:53 PM: DEBUG [http-8080-1] Java version: 1.6.0_05
Dec 15, 2008 12:21:53 PM: DEBUG [http-8080-1] Java vendor: Sun
Microsystems Inc.
Dec 15, 2008 12:21:53 PM: DEBUG [http-8080-1] Java class path: E:\Java
\jdk1.6.0_05\lib\tools.jar;E:\tomcat\apache-tomcat-6.0.18\bin
\bootstrap.jar
Dec 15, 2008 12:21:53 PM: DEBUG [http-8080-1] Operating system name:
Windows XP
....

How can I tell the logger to log only those statements I give inside
the servlet code?
thanks
harry
 
J

Jan Thomä

How can I tell the logger to log only those statements I give inside
the servlet code?
log4j.rootLogger=DEBUG,myLogFile
log4j.appender.myLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.myLogFile.File=mylog.log
log4j.appender.myLogFile.MaxFileSize=100KB
log4j.appender.myLogFile.MaxBackupIndex=2
log4j.appender.myLogFile.layout=org.apache.log4j.PatternLayout


Change the first line to:

log4j.logger.newwebapplog=DEBUG,myLogFile

This should print only the statements for logger "newwebapplog" (which
is the one you created using getLogger) into the log file. You might
get a warning from log4j regarding the other statements from other
loggers (no appender found or something) so you might create another
appender (maybe a consoleappender) and attach this to the rootLogger.

Hope that helps.

Best regards,
Jan
 
L

Lew

harryos said:
Change the first line to:

log4j.logger.newwebapplog=DEBUG,myLogFile

This should print only the statements for logger "newwebapplog" (which
is the one you created using getLogger) into the log file. You might get
a warning from log4j regarding the other statements from other loggers
(no appender found or something) so you might create another appender
(maybe a consoleappender) and attach this to the rootLogger.

You can also set the rootLogger to WARN or ERROR and the 'newwebapplog' to
DEBUG. One of the advantages of log4j is fine-grained control over logging
levels.

harryos said:
private Logger log;
public PostBean(){
log=Logger.getLogger("newwebapplog");
}

If this is the only line in the constructor, you can use an initializer for
'log' and skip the constructor altogether.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top