Where to configure internal tomcat7 stdout/stderr

2020-04-02 06:00发布

问题:

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

回答1:

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.



回答2:

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.



回答3:

On windows you'll have to go to tomcat/bin/service.bat and edit PR_LOGPATH variable (all of them). And probably reinstall service by "service remove" and "service install" On linux systems solution must be very similar



回答4:

Disable File Creation via Apache "Commons Daemon Service Manager"

Short version: Remove the auto text inside the service app. This will disable creation of the stdout and stderr files. The "commons-daemon" file you're stuck with, I think.

Long version see below.


Tomcat for Windows has a special little service application. This app goes by several names. E.g.: Commons Daemon Service Manager, procrun. And the EXE file will usually be called something like Tomcat7w.exe/YourServiceNameHerew.exe/Tomcat9w.exe.

The Windows icon is just labeled Monitor Tomcat. And starting this will take you directly to the Logging tab.

Here's a screenshot of the "Logging" tab. This app can generate several files:

  1. The commons-daemon log file.
  2. The Pid file. We don't care about this right now.
  3. The Redirect Stdout log file.
  4. The Redirect Stderror log file.

So there are 3 log files. NONE of these are rotated by default. ProcRun does NOT offer facilities for that.

So there are several workarounds that come to my mind:

  1. Disable creation of stdout/stderr files (see below). You will STILL need a way to clean up the old "commons-daemon" files. (I know of no way to disable creation of those files. Just emptying the text-field will NOT do the trick.) But at least those should be small in size.
  2. Periodically restart the Tomcat service. This will create a file timestamped with the current date. You will STILL need a way to clean up the old commons-daemon/stdout/stderr files.
  3. Use an external Logrotate-for-Windows utility to both rotate the current file and clean up the old rotated files.
  4. Do not use ProcRun and start your Windows Tomcat in some other way. This will create none of the commons-daemon/stdout/stderr files.

I think the official position can roughly be summarized as "What? Your catalina/stdout/stderr files are getting too large? Well, you shouldn't be logging anything to those anyway." In other words: the only officially supported option seems to be number 1 (and 4).

Disabling creation of stdout/stderr

In my Tomcats the "commons-daemon" file is usually very small and only ever receives a few new lines when Tomcat is started or shut down. Even at Level: Debug.

The redirected stdout and stderr files may become VERY large.

You may however remove the auto text from the text fields. This should completely disable generation of these files.

Further reading

  • Official Apache Tomcat Windows service HOW-TO. For: Tomcat 7.0, Tomcat 8.0, Tomcat 8.5, Tomcat 9.0


回答5:

For those who just want to customize the location where these 3 logs are created: If you run Tomcat as a service and you want to customize where the commons-daemon, stderror and stdout reside you can start your service with the command line parameter --LogPath or set on the server machine an environment variable PR_LOGPATH with your custom path.

This is documented also here together will other params: https://tomcat.apache.org/tomcat-8.0-doc/windows-service-howto.html