Bug 53930 - allow capture of catalina stdout/stderr to a command instead of just a file [PATCH]
Summary: allow capture of catalina stdout/stderr to a command instead of just a file [...
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 8
Classification: Unclassified
Component: Catalina (show other bugs)
Version: 8.0.x-trunk
Hardware: Macintosh Mac OS X 10.4
: P2 enhancement with 13 votes (vote)
Target Milestone: ----
Assignee: Tomcat Developers Mailing List
URL:
Keywords: PatchAvailable
Depends on:
Blocks:
 
Reported: 2012-09-24 15:53 UTC by Casey Lucas
Modified: 2018-12-03 15:08 UTC (History)
2 users (show)



Attachments
patch to catalina.sh adding support for CATALINA_OUT_CMD (2.61 KB, patch)
2012-09-24 15:53 UTC, Casey Lucas
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Casey Lucas 2012-09-24 15:53:47 UTC
Created attachment 29414 [details]
patch to catalina.sh adding support for CATALINA_OUT_CMD

catalina.sh currently supports passing in a value for CATALINA_OUT. If supplied the stdout/stderr of the java process will be redirected to this file. This single file scenario makes it difficult to keep file contents for long running process separated on a per day basis as is typical for production logging scenarios.  We use cronolog (http://http://cronolog.org/) to do automatic log rolling but others may have different needs.  The attached patch adds support to catalina.sh for an optional CATALINA_OUT_CMD variable. If this variable is defined, the stdout and stderr of the java process will be piped to the command.  We use it as in:

CATALINA_OUT_CMD="cronolog $CATALINA_BASE/logs/catalina.%Y-%m-%d.out >/dev/null 2>&1"
export CATALINA_OUT_CMD
catalina.sh start $@

Of course long-running, server-side applications should typically not be writing to stdout/stderr but sometimes this is out of your control.
Comment 1 Christopher Schultz 2013-10-28 14:32:28 UTC
I'm not sure if this matters a lot, but the "touch $CATALINA_OUT" has been dropped from this patch. Was that intentional?
Comment 2 Casey Lucas 2013-10-28 16:29:14 UTC
(In reply to Christopher Schultz from comment #1)
> I'm not sure if this matters a lot, but the "touch $CATALINA_OUT" has been
> dropped from this patch. Was that intentional?

I doubt it was intentional. However, the file should only be touched if will be used - basically, around line 380.
Comment 3 peterhansson_se 2016-12-02 10:22:32 UTC
This is a very important addition. 
If you search the internet you'll find lots of recommendations on allowing log-rotation and all of them tell you to modify your catalina.sh file. The point here is that users really shouldn't modify catalina.sh as it breaks the upgrade path. Hence the feature should be supported in the script itself as per Casey's patch.

Since this RFE was launched the catalina.sh script has been modified to also support NOHUP which means the Casey's patch no longer applies verbatim.
Comment 4 peterhansson_se 2016-12-02 15:33:04 UTC
As a workaround: Use a copy-and-truncate strategy instead. This doesn't require changes to catalina.sh script.

On Linux: Use logrotate utility with 'copytruncate' option.

On Solaris: Use logadm utility with '-c' option. (or on Solaris 11 and later you can install logrotate by doing 'pkg install file/logrotate' if you really, really want to use logrotate instead of logadm)



The proposed change basically allows for a pipe strategy. This strategy is better than a copy-and-truncate strategy for the following reasons:

- The copy-and-truncate strategy requires extra disk space when the operation is being executed.
- The copy-and-truncate strategy has a small time gap between the copy and the truncate. During the gap messages may be lost.
- The copy-and-truncate strategy requires changes to setup outside of the Tomcat configuration: it requires changes to logrotate/logadm tool and potentially a new entry in cron table.
Comment 5 Mark Thomas 2018-12-03 15:08:19 UTC
Thanks for the patch.

Sorry it took so long to apply it.

Fixed in:
- trunk for 9.0.14 onwards
- 8.5.x for 8.5.36 onwards
- 7.0.x for 7.0.93 onwards