"zpetero" <> wrote in message
news: ups.com...
*fixed top post*
> Ryan,
>
> How can I stop logging altogether? I only want to write what I
> specify to the log file.
>
> This line deletes the file if its already there when initialized.
> log4j.appender.dest1.Append=false
>
> What's the difference beteween,
> log4j.rootCategory=DEBUG, dest1 VS. log4j.rootLogger=DEBUG, dest1
> ??
>
>
> Peter
>
> Here's the porperties file,
>
> # debug, info, warn, error, fatal
> log4j.rootCategory=DEBUG, dest1
> #log4j.rootLogger=DEBUG, dest1
> log4j.appender.dest1=org.apache.log4j.FileAppender
> #log4j.appender.dest1.layout=org.apache.log4j.Patt ernLayout
>
> #log4j.logger.com.web=FATAL
>
> # Appender dest1 writes to the file "test" in user's home.
> log4j.appender.dest1.File=${CATALINA_HOME}/webapps/Epiphany/WEB-INF/classes/app/log
>
> # Truncate 'test' if it aleady exists.
> log4j.appender.dest1.Append=false
>
Did you look at the documentation at all? Look through the "Short Manual"
here:
http://logging.apache.org/log4j/docs/manual.html
It provides a decent explanation of what a "logger" is and how the whole
thing works. Basically, loggers are what take your input and write it to
whatever log/logs you set up with the properties file. There can be, and
normally are, multiple loggers in any application. Each logger is named when
created, and you can retrieve loggers by name. In your code, you have
something like:
Log log = LogFactory.getLog(XXX.class);
That gets or creates a logger and assigns it the fully qualified name of the
XXX class. In other words, if XXX.class is in package zzz.yyy, then the
loggers name is zzz.yyy.XXX. Okay so far? Now every call to
LogFactory.getLog(XXX.class) or to LogFactory.getLog("zzz.yyy.XXX") (I think
this is correct syntax) will return that same logger, which means you will
be writing to the same destination. Also, loggers are hierarchical. The
"base" logger is rootLogger (to answer your question about that). You get
that one with LogFactory.getRootLogger(). All other loggers "extend" the
root logger. By default, *everything* that is logged gets written to the
rootLogger. You configure the root logger by first assigning it a logging
level and Appender:
log4j.rootLogger=DEBUG, dest1
DEBUG is the level of messages you want to log (there are multiple levels...
five I think). dest1 is the name of the Appender. That is configured later
in your properties file. An Appender is what does the actual output. You've
told log4j that you want the dest1 Appender to be a FileAppender and given
it a destination file to write to. Finally, the tricky part (to explain
anyway)... Back to the hierarchical thing. Assume that for our zzz package
that we were using, we only want to log FATAL level and above. Like the line
in your properties file, we'd write:
log4j.logger.zzz=FATAL
That sets the logger named zzz to log only FATAL messages. If you wanted to
log it to a different place, you'd specify and configure another Appender:
log4j.logger.zzz=FATAL, dest2
log4j.appender.dest2=org.apache.log4j.FileAppender
....
Now here's the thing. Our zzz.yyy.XXX logger is considered a child of the
zzz logger because of the name. Logger names determine relationships. A
logger named zzz.yyy would be a direct child of zzz. zzz.yyy.XXX is a direct
child of zzz.yyy. Therefore, when you configure the zzz logger, you're also
configuring the zzz.yyy logger and the zzz.yyy.XXX logger. Now assume that
you configure an Appender for each one of zzz, zzz.yyy, and zzz.yyy.XXX:
log4j.logger.zzz=FATAL, dest2
log4j.logger.zzz.yyy=FATAL, dest3
log4j.logger.zzz.yyy.XXX=FATAL, dest4
Then you call LogFactory.getLog(XXX.class). You've now got the zzz.yyy.XXX
logger. What happens when you write something to it? Assuming you write a
FATAL message, of course, it will be written to the dest4 Appender. And the
dest3 Appender. And dest2 and dest1 as well. Logged messages by default
travel up the hierarchy and are logged by every configured Appender, and
remember that I said the root logger is the base of every logger hierarchy.
To stop the message from traveling up the hierarchy, there is an additivity
flag. Assume we want to write to the zzz.yyy.XXX logger, but not have it go
to any of the others. You would set additivity to false on zzz.yyy.XXX, like
so (I think):
log4j.additivity.zzz.yyy.XXX=false
By setting the additivity to false on zzz.yyy instead, you'd get messages
logged to dest4 and dest3 but not the others.
*whew*, that was longer than I'd planned. Hope it gets you going in the
right direction.