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

Discussion in 'Java' started by harryos, Dec 15, 2008.

  1. harryos

    harryos Guest

    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
    harryos, Dec 15, 2008
    #1
    1. Advertising

  2. harryos

    Jan Thomä Guest

    On 2008-12-15 08:08:23 +0100, harryos <> said:

    > 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
    Jan Thomä, Dec 15, 2008
    #2
    1. Advertising

  3. harryos

    Lew Guest

    harryos said:
    >> 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


    Jan Thomä wrote:
    > 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.

    --
    Lew
    Lew, Dec 15, 2008
    #3
  4. harryos

    harryos Guest

    thanks for the help!
    harry
    harryos, Dec 15, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    429
    S. Justin Gengo [MCP]
    Feb 10, 2006
  2. jAnO!
    Replies:
    2
    Views:
    547
    Son KwonNam
    Jul 23, 2004
  3. willirl
    Replies:
    3
    Views:
    703
    Sebastian Millies
    Feb 24, 2005
  4. Replies:
    1
    Views:
    442
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=
    Jun 3, 2007
  5. Andrew
    Replies:
    7
    Views:
    415
    Andrew
    Jun 28, 2009
Loading...

Share This Page