views:

544

answers:

2

I have an embedded tomcat app and whenever I start it up I see this error printed to the console twice:

Unable to configure the logging system.  No log.properties was found.

It seems stupid, but I've done some googling and searched stackoverflow and can't seem to find someone experiencing this problem.

My main class Looks roughly like this:

public class AuthServerEntryPoint {
  static {
    org.apache.log4j.PropertyConfigurator.configure("conf/log4j.properties");
  }
public static void main(String[] args) {
  // ...
}

"conf/log4j.properties" contains a seemingly valid configuration:

log4j.appender.mainAppend=org.apache.log4j.ConsoleAppender
log4j.appender.mainAppend.layout=org.apache.log4j.PatternLayout
log4j.appender.mainAppend.layout.ConversionPattern=%d %p [%t] %c -- %m%n 

log4j.appender.fileAppend=org.apache.log4j.FileAppender
log4j.appender.fileAppend.layout=org.apache.log4j.PatternLayout
log4j.appender.fileAppend.layout.ConversionPattern=%d %p [%t] %c - %m%n 
log4j.appender.fileAppend.file=logs/myservice.log

log4j.rootLogger = info, fileAppend
log4j.logger.com.mycompany.myservice = debug, fileAppend

And the logging actually does work - i.e., logs are correctly written to myservice.log. So what gives?

Thanks! -Carl

+1  A: 

By default, log4j will look for a logging properties file on the classpath. Put your webapp's logging properties config file into its WEB-INF/classes directory; e.g.

$CATALINA_HOME/webapps/<yourApp>/WEB-INF/classes/log4j.properties

This is the simple approach to getting a webapp to use Log4j is recommended by the referenced documentation. And it is the approach I use myself for webapp logging.

There are various other ways to configure Log4j logging on Tomcat as well. It is possible that your Tomcat has been (partly) configured to use one of them, but something has not been done quite right.

Configuring Tomcat' log4j logging via the system properties is an option that avoids figuring out where log4j is looking ... and what is going wrong. But you are then stuck with creating/using a custom launch script or having to remember to set environment variables before launching Tomcat.

References:

Stephen C
I tried placeing both "log.properties" and "log4j.properties" in the WEB-INF/classes directory, and neither made this error message go away.Even if it did, that doesn't explain why the above code doesn't work - I am explicitly telling it exactly where to find the file, and it clearly is finding it becasue it is logging to the proper file. Since the command is run as static, I can't see how anything is logging before the logger itself is initialized.Thanks for the suggestion though.
cmyers
Your original question refers to conf/log4j.properties. Have you tried creating a conf directory in the classes folder and putting the config file in there.
Jherico
@cmyers - Try getting rid of the call to `PropertyConfigurator.configure()`. It is probably resolving the path "conf/log4j.properties" in the current directory (wherever that might be). Besides, you shouldn't hardwire the logging property file location into your source-code.
Stephen C
+1  A: 

By embedded Tomcat app, do you mean that you are starting Tomcat from Java code and are using the class org.apache.catalina.startup.Embedded?

If yes, my guess is that Tomcat might not be able to find its logging configuration file that is set up in catalina.sh (or equivalent) when using the scripts:

LOGGING_CONFIG="-Djava.util.logging.config.file=$CATALINA_BASE/conf/logging.properties"

The odd part is that this file is called logging.properties, not log.properties, so I'm really not sure. I didn't check Tomcat sources though, maybe they are doing some kind of black magic in there.

Could you just try to rename $CATALINA_BASE/conf/logging.properties into log.properties (or not) and to put it on the classpath of your app (or to set -Djava.util.logging.config.file)?

Pascal Thivent
Thanks for the suggestion - I am invoking tomcat from java code, so I am just running my class with the main, and it has a TomcatServletContainer object which it starts.
cmyers