Cosio
Cosio

Reputation: 121

Error reading log4j.properties in my Maven project

I have a problem with log4j. I'm trying to use this for logging in a file some actions in my Rest Web Service. I am developing the web service in java with jersey framework. I am using Eclipse and a Maven Project.

enter image description here

I've created my MWlogs.properties and placed under src. The content of this file is:

# LOG4J daily rolling log files configuration
log4j.rootLogger=DEBUG, RollingAppender

# RollingAppender configuration
log4j.appender.RollingAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.RollingAppender.File=./logs/mobiwallet.log
log4j.appender.RollingAppender.DatePattern='.'yyyy-MM-dd
log4j.appender.RollingAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RollingAppender.layout.ConversionPattern=[%p] %d %c %M - %m%n

Then under service package I've tried to test log4j but it's not running correctly. What I have in this class under service package is:

...
import org.apache.log4j.Logger; 
import org.apache.log4j.PropertyConfigurator;
...
@Path("/UserActions")
public class UserActions {
   static final Logger logger = Logger.getLogger(UserActions.class);
   ...
   public MW_USER_DATA_QUERY_RESPONSE reqMWUser(InputStream userRequest){
      PropertyConfigurator.configure("MWlogs.properties");
      ...
      logger.info("Consulta de MW USER!!!!!");
      return reply;
   }
}

Tomcat is launching some error when I try to run the project:

log4j:ERROR Could not read configuration file [MWlogs.properties].

And when logger.info is executed Tomcat shows also the following error message:

log4j:WARN No appenders could be found for logger (service.UserActions).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info

Upvotes: 2

Views: 1944

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208944

Tomcat is launching some error when I try to run the project:

log4j:ERROR Could not read configuration file [MWlogs.properties].

It seems either the stacktrace isn't being logged or you're just not showing it to us. But most likely there is also a FileNotFoundException being thrown to go along with the above message.

PropertyConfigurator.configure(String) is going to look for a file on the file system, starting at the working directory.

With the location of your properties file, it will be a class path resource and should be read via an URL, which PropertiesConfigurator has an overloaded confiure(URL) method for. So change the call to

PropertyConfigurator.configure(getClass().getResource("/MWlogs.properties"));

Assuming your MWlogs.properties gets built to root of the classpath (which it should), the above should work. I tested it. If for some reason it still doesn't work, you can try to put the file in src/main/resources or you can try to explicitly put it in the app in tomcat

<tomcat>/webapps/exploded-war/WEB-INF/classes/MWlogs.properties

Upvotes: 1

Related Questions