You don't say the following:

Tomcat version
JRE/JDK version
Platform (OS and version)

That being said, I use log4j (with or without Apache commons logging) for 
nearly 
every web application I run on Tomcat.

Short answer: You don't manually read in your properties file with log4j.

Read a tutorial on log4j for a more complete explanation.

What follows below is an overview. Please refer to the log4j documentation, the 
log4j javadocs, and tutorials around the Internet for a more complete 
discussion.

Code:

In each class that you want to use logging, add the following:

package foo; // replace with your package name

import org.apache.log4j.Logger;

public class MyFoo { // replace with your class name
    private static final Logger log = Logger.getLogger(MyFoo.class); // note 1

    /*
     * rest of class including log.xxxx("message") where xxxx is a level
     */
}

note 1: While it is traditional that one uses getLogger(MyFoo.class), which is 
shorthand for getLogger(clazz.getName()), you can also name your loggers with 
any legal string. See the javadoc for more information.

Properties File:

In your properties file, you'll need appenders as well individual lines for 
non-default logging levels for each logger name.

### direct messages to file foo.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=${catalina.home}/logs/foo.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

# default logging level
log4j.rootLogger=warn, file

# logging for foo.MyFoo - name matches the logger name
log4j.logger.foo.MyFoo.type=info

Since there is only a file listed in the appenders, then that's where the 
logging will go.

Note that ${catalina.home} was used for logging. This should probably 
be ${catalina.base} just in case more than one copy of Tomcat is run from a 
base 
installation (see RUNNING.txt). This is a handy way of storing log files.

If you're doing some in-IDE testing where ${catalina.home} or ${catalina.base} 
is not set, then hopefully your IDE will let you pass in a Java parameter. Just 
set
-Dcatalina.home=<some-place> (or -Dcatalina.base=<some-place>) while testing.

Application Structure:

Properties file

Package up the application so that log4j.properties gets placed in
WEB-INF/classes/log4j.properties. Log4j looks for the properties file in the 
classpath. Placement in your IDE's project depends on how your IDE packages 
files.

Log4j library

Package up the application so that log4j-1.2.15.jar (or whatever version you 
are 
using) gets placed in WEB-INF/lib/log4j-1.2.15.jar. Placement in your IDE's 
project depends on how your IDE packages files.

. . . . just my two cents.

/mde/


----- Original Message ----
From: cpanon <cpa...@yahoo.com>
To: Tomcat <users@tomcat.apache.org>
Sent: Wed, December 8, 2010 8:23:46 PM
Subject: deploy log4j

Hello
I have an app that work perfectly in my IDE(JBuilder05, yes I know but it work 
fine), but on deployment I believe it is not reading the log4j with this error:
java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)

followed by 
log4j:WARN No appenders could be found for logger 

I am loading the prop file with this(that works in the IDE)
java.util.Properties props = new java.util.Properties();
    try {
      props.load(getClass().getResourceAsStream("/log4j.properties"));
    }
    catch (IOException ex) {
    }
    PropertyConfigurator.configure(props);
    //ver03_lfj.setLevel(Level.DEBUG);
    ver03_lfj.debug("yippie");



      

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to