Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to configure internal tomcat7 stdout/stderr log files

I'm using Apache Tomcat 7.0.40 with Log4j config according to http://tomcat.apache.org/tomcat-7.0-doc/logging.html

Everything's working as expected, except that some log files are created, which are actually not configured in my log4j.properties:

log4j.rootLogger=INFO, CATALINA

# Define all the appenders
log4j.appender.CATALINA=org.apache.log4j.RollingFileAppender
log4j.appender.CATALINA.File=${catalina.base}/logs/catalina.log
log4j.appender.CATALINA.MaxFileSize=3MB
log4j.appender.CATALINA.MaxBackupIndex=10
log4j.appender.CATALINA.Append=true
log4j.appender.CATALINA.Encoding=UTF-8
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.RollingFileAppender
log4j.appender.LOCALHOST.File=${catalina.base}/logs/localhost.log
log4j.appender.LOCALHOST.MaxFileSize=3MB
log4j.appender.LOCALHOST.MaxBackupIndex=10
log4j.appender.LOCALHOST.Append=true
log4j.appender.LOCALHOST.Encoding=UTF-8
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.RollingFileAppender
log4j.appender.MANAGER.File=${catalina.base}/logs/manager.log
log4j.appender.MANAGER.MaxFileSize=3MB
log4j.appender.MANAGER.MaxBackupIndex=10
log4j.appender.MANAGER.Append=true
log4j.appender.MANAGER.Encoding=UTF-8
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.RollingFileAppender
log4j.appender.HOST-MANAGER.File=${catalina.base}/logs/host-manager.log
log4j.appender.HOST-MANAGER.MaxFileSize=3MB
log4j.appender.HOST-MANAGER.MaxBackupIndex=10
log4j.appender.HOST-MANAGER.Append=true
log4j.appender.HOST-MANAGER.Encoding=UTF-8
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

The files which get created although they are NOT in my configuration are:

  • commons-daemon.yyyy-MM-dd.log
  • tomcat7-stderr.yyyy-MM-dd.log
  • tomcat7-stdout.yyyy-MM-dd.log

As you see they use a DailyRollingFileAppender, which is not desired, as I need a maintenance free system. So I would prefer a RollingFileAppender with maxBackupIndex to prevent unlimited growth of the logs.

So where can I configure the stdout, stderr and commons-daemon logs? Is there some configuration somewhere or can I override the config with my own Log4j config? Thx

like image 964
räph Avatar asked Nov 05 '13 10:11

räph


People also ask

Where are tomcat9 logs?

The main log is in /opt/tomcat/logs/catalina. <date>. log . A copy of these message also end up on the standard error, which end up (by default) in systemd journal.

Where are tomcat logs on Windows?

Apache Tomcat Logs Location in Windows By default, Apache Tomcat logs are stored in the install_dir/logs where the install_dir refers to the Apache Tomcat installation directory. The log files are stored in the logs directory.


2 Answers

I foresee us having a similar requirement/facing same issue some time later in future on one of our systems. So was keen on finding some solution/workaround myself.

Looked into the commons-daemon first to see if there is any configuration we can do to achieve a maxbackupindex on the logs; but the search didn't help much.

However i found two approaches that people take for dealing with "tomcat logs crossing limits".

First Approach: Configure internally in Tomcat

  1. Find context.xml in Tomcat in the path :-

    YourTomcatInstallDir\conf\context.xml  
    
  2. Edit the context tag to add swallowOutput="true" like below:-

    <Context swallowOutput="true">
    

    (This swallows all your stdout/stderr and redirects to your underlying logging system.)

  3. In your log4j.properties; add config to redirect your org.apache.catalina logs to your own log file.

    Your logger will look like this:-

    log4j.logger.org.apache.catalina=INFO, YourAppender
    

    (On YourAppender now you can set MaxBackupIndex and MaxFileSize to achieve your defined number of rollovers and a limit on the log size. )

If the above approach doesn't work; you can try using an external utility as mentioned in the second approach

Second Approach: Configure externally with "logrotate"

There's a simple tool called logrotate available to achieve the desired effect on logs you do not have control over. Here are some links to get you started.

  • How to rotate Tomcat catalina.out
  • Understanding logrotate utility
  • logrotate usage
  • logrotate on windows

The usage of this tool is pretty straightforward and a quick run through these links should get you what you need.

like image 131
M.. Avatar answered Oct 14 '22 17:10

M..


I had the same issue and finally stumbled on the solution. Tomcat 7 for windows has a tomcat properties application called Monitor Tomcat. On the logging tab you need to delete the "auto" value in the redirect stdout and stderr input boxes. Just leave it blank and it won't create those files. Then you can use log4j to create rolling log files for those critical files for future debug purposes.

Hope that helps.

like image 21
msilveus Avatar answered Oct 14 '22 16:10

msilveus