2

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
Cosio
  • 121
  • 2
  • 13
  • In your main, you can try adding `BasicConfigurator.configure();` (http://stackoverflow.com/questions/12532339/no-appenders-could-be-found-for-loggerlog4j) – Ascalonian Jan 28 '15 at 19:49
  • possible duplicate of [Log4j Warning while initializing?](http://stackoverflow.com/questions/7685510/log4j-warning-while-initializing) – Ascalonian Jan 28 '15 at 19:50

1 Answers1

1

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
Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720