Q1: I don't do much servlet stuff, so I can't give a great answer except to
say that I know a lot of people ask servlet questions all the time, so check
the mailing list archives.
(http://marc.theaimsgroup.com/?l=log4j-user&r=1&w=2)  I assume Benjamin's
right, though.

Q2: FileAppender (which is what I assume you're referring to) creates a
handle to the file when it's initialized, then simply writes to the stream
(using the handle) from that point on.  I DOESN'T recreate the file handle
every time it logs something, for obvious performance reasons.  It would
seem to me that it must be generating errors when trying to write to a
stream that has no file associated with it anymore, but I haven't tested it.

Q3: Benjamin is exactly right, though a little more clarifaction could help.
Take the following example:

public class A {
  protected static final Category LOG = Category.getInstance(A.class);
  public void method1() {
    LOG.debug("Hi");
  }
}

public class B extends A {
  public void method2() {
    LOG.debug("Hello");
  }
}

Then calling method1() then method2 will generate a line in your logs like:
  0    A DEBUG   - Hi
  10   A DEBUG   - Hello

That's probably not what you want, though.  For one thing, it's lieing about
where the log message came from.  Also, if you suspect a problem in class B,
you can't turn on debugging on just B, but on both A & B, which could give
you reams more messages than you'd want to sort through.  That's why I
always make my Categories private:

public class A {
  private static final Category LOG = Category.getInstance(A.class);
  public void method1() {
    LOG.debug("Hi");
  }
}

public class B extends A {
  private static final Category LOG = Category.getInstance(B.class);
  public void method2() {
    LOG.debug("Hello");
  }
}

Then calling method1() then method2 will generate a line in your logs like:
  0    A DEBUG   - Hi
  10   B DEBUG   - Hello

And of course, since it's a static final, there's a 1 <-> 1 between the
number of classes being logged and the number of Category instances there
are.  Categories are pretty light-weight, though, so unless you're really
tight on memory, it's not a problem; the benefits far outweigh the costs.
(Also, since it's "static final" the compiler does a straight substitution
for you, so there's no overhead in obtaining the reference to the Category.)

-Jim Moore

-----Original Message-----
From: Benjamin Russell Stocum [mailto:[EMAIL PROTECTED]]
Sent: Monday, June 25, 2001 1:49 PM
To: LOG4J Users Mailing List
Subject: Re: A few questions - tips for easy debugging?


I am also new to log4j, but I believe I can answer some of your questions.
If I
am wrong, someone correct me :)

First are you missing R from "log4j.rootCategory=debug, logfile" ?

Q1:  Answer:  are you working with servlets?  If so I thought it was
required
that you reinitialize Tomcat because of the way a servlet JVM engine is set
up.

Q2: ? anyone?

Q3: ...this means do we have to create static Category instances for each
class
files which I want to debug?
Answer:  From looking at the "Short introduction to log4j", the
"Configuration"
section, this would appear to be the case, you would need to define a
"static
Category" for each class you want detailed debugging on.  What I mean by
this is
that if class A uses class B.  And class B does not define a "static
Category",
then all the messages will appear as if they belonged to class A?

-Ben Stocum


Ken'ichi Unnai wrote:

> Dear All,
>
> I'm new to log4j.
> I'm playing log4j-1.1.3 with simple HelloWorld servlet (on tomcat3.2.1)
> with Ant as compiling tool. My WEB-INF/classes/log4j.properties
> is something like this:
> ---------------
> log4j.rootCategory=debug, logfile
> log4j.appender.logfile=org.apache.log4j.RollingFileAppender
> log4j.appender.logfile.File=debug.log
> log4j.appender.R.MaxFileSize=100KB
> log4j.appender.R.MaxBackupIndex=1
> log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
> log4j.appender.logfile.layout.ConversionPattern=%d %5p [%t] (%F:%L) - %m%n
> ---------------
>
> I have a few questions:
> Q1. When I modify a bit of HelloWorld.java code, rebuild it, and reload
it,
>     then following message appears:
>
> log4j:ERROR No appenders could be found for category (HelloWorld).
> log4j:ERROR Please initialize the log4j system properly.
>
> ...so I have to restart tomcat every time. Is there any tips to avoid
> this?
>
> Q2. When I delete log file, then I have to restart tomcat. Is there any
>     tips to automatically create log file without restarting tomcat?
>
> Q3. I wrote Debug.java file as my utility lib, something like this:
> ----------------
> package util;
> import org.apache.log4j.Category;
> import org.apache.log4j.PropertyConfigurator;
>
> public class Debug {
>     public static Category log =
Category.getInstance(Debug.class.getName());
>
>     public static void initDebug(String prefix, String file) {
>         if (file != null) {
>             PropertyConfigurator.configure(prefix+file);
>         }
>         log.debug("Debug.initDebug("+prefix+file+")");
>     }
> }
> ----------------
> This class is initialized as Debug.initDebug(...) at servlet init()
method.
> I think this is cool & useful because whenever I want to debug,
> then all I have to do is just call
>
> Debug.log.debug("debugging...");
>
> in any class file. This works, but I found this method doesn't enable me
to
> identify package name.
>
> Yep it does make sense, I only create Category instance
>
> Category.getInstance(Debug.class.getName())
>
> ...this means do we have to create static Category instances
> for each class files which I want to debug? Any tips?
>
> Sorry for many questions, but I believe this should be informative
> tips for other forks.
>
> Best regards,
> Ken

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to