0
votes

I am trying to reconfigure Tomcat 7 to use log4j for its internal logging.

I have followed all steps in Tomcat 7 docs: http://tomcat.apache.org/tomcat-7.0-doc/logging.html It is still not using Log4J or atleast it is not using the config specified in the log4j.properties file.

What I have tried so far:

  • Copied the necessary jar files to the locations mentioned in Tomcat Docs

  • CATALINA_HOME, CATALINA_BASE environment variables were not present. Added them and set both to C:\Program Files\Apache Software Foundation\Tomcat 7.0

  • Noticed that log4J properties file had references to catalina.base instead of catalina_base so created , catalina.base and catalina.home environment variables and set both to C:\Program Files\Apache Software Foundation\Tomcat 7.0

  • As suggested in another answer I have deleted the "auto" entries in the Monitor Tomcat App. enter image description here

lib/log4j.properties

log4j.rootLogger = INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA = org.apache.log4j.DailyRollingFileAppender
log4j.appender.CATALINA.File = ${catalina.base}/logs/catalina
log4j.appender.CATALINA.Append = true
log4j.appender.CATALINA.Encoding = UTF-8
# Roll-over the log once per day
log4j.appender.CATALINA.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.CATALINA.layout = org.apache.log4j.PatternLayout
log4j.appender.CATALINA.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.LOCALHOST = org.apache.log4j.DailyRollingFileAppender
log4j.appender.LOCALHOST.File = ${catalina.base}/logs/localhost
log4j.appender.LOCALHOST.Append = true
log4j.appender.LOCALHOST.Encoding = UTF-8
log4j.appender.LOCALHOST.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.LOCALHOST.layout = org.apache.log4j.PatternLayout
log4j.appender.LOCALHOST.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.MANAGER.File = ${catalina.base}/logs/manager
log4j.appender.MANAGER.Append = true
log4j.appender.MANAGER.Encoding = UTF-8
log4j.appender.MANAGER.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.HOST-MANAGER = org.apache.log4j.DailyRollingFileAppender
log4j.appender.HOST-MANAGER.File = ${catalina.base}/logs/host-manager
log4j.appender.HOST-MANAGER.Append = true
log4j.appender.HOST-MANAGER.Encoding = UTF-8
log4j.appender.HOST-MANAGER.DatePattern = '.'yyyy-MM-dd'.log'
log4j.appender.HOST-MANAGER.layout = org.apache.log4j.PatternLayout
log4j.appender.HOST-MANAGER.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

log4j.appender.CONSOLE = org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Encoding = UTF-8
log4j.appender.CONSOLE.layout = org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern = %d [%t] %-5p %c- %m%n

# Configure which loggers log to which appenders
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost] = INFO, LOCALHOST
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager] =\
  INFO, MANAGER
log4j.logger.org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager] =\
  INFO, HOST-MANAGER
1

1 Answers

1
votes

CATALINA_HOME defaults to Tomcat's installation directory. You can set it if you like, but it's not necessary, particularly if you are running a simple single instance. Additionally, you only need the underscore versions of the environment variables. The 'dot' versions are referenced via Java rather than the shell - Tomcat knows where/how to get this information.

It would help if you say exactly which jars you put where - there might be something small you have missed, but without knowing precisely what you have done, it is impossible to say. Also, you might want to make sure you have set the environment variables correctly. Open a command prompt and type echo %CATALINA_HOME% and echo %CATALINA_BASE% to be sure. Compare the output to the output of the dir command against those same directories. Do they match? Hint: check for tilde (~) abbreviations.

Finally, are you absolutely certain it is not using log4j? I ask because the log4j.properties file at the site to which you have linked is supposed to behave the same as Tomcat's out-of-the-box logging. If you have just copied that file without changing anything, log4j might actually be working after all.