Search code examples
javatomcatloggingtomcat9

How to save all logs printed by System.out.println to ta file in Tomcat 9


I have some windows product, which comes as .exe file. When I run it, it has tomcat 9 server with some webapp inside and it runs the tomcat via startup.bat in the background. Running startup.bat in the background means I do not see the tomcat window at all. I only see the java.exe process in task manager and logs like catalina.log, but I am sure it runs tomcat via startup.bat, not via tomcat.exe. Since startup.bat put all output printed via System.out.println() to console, it is not visible (completely lost) for me at all. Because it is proprietary product, I cannot modify the code and I do not have access to the code.

I would like to ask how can I redirect all console output printed with System.out.println() to a file with the possibility of log rotations and filtering by class/package. My restrictions:

  • I cannot change System.out.println() to Logger.info() because it is proprietary product
  • I cannot call System.setOut(...); with my Logger.
  • I can change tomcat configs, such as logging.properties or log4j2.properties inside webapp.

I tried to change startup.bat inside tomcat by adding >>..\logs\myconsole.log 2>&1. It works, but there is no logs rotation and if I keep server running for a few months, log file will be unmanageable. Also, I cannot filter logs only to specific classes/packages.

There is no Windows registry setting for this tomcat, I cannot change logging setting there.

I found about sysout-over-slf4j library which does this but it is too outdated and I even cannot download it anymore from official website.

Is there way to do this?


Solution

  • Finally the solution was to change conf/context.xml configuration of the tomcat server and add swallowOutput="true" attribute to the <Context> tag like this: <Context swallowOutput="true">.

    Output goes to this file logs/localhost.*.log.

    Although, as I understand this swallowOutput="true" is a trick and has some limitation as written here but I could not find any other way.

    At the end, this is what vendor of the product recommended as well :)