0

I need to understand how Tomcat is able to catch JVM garbage collection console output logs and put it in catalina.out.

I tried to redirect System.out and System.err, but no can do. I was not able to get the output of GC logs.
However, when activating -verbose:gc in Tomcat (in catalina.sh, *JAVA_OPT*) and later cheking the output in catalina.out I was able to see the GC logs. How is this possible? How was Tomcat able to redirect that output to a file?

Maybe do you know any way to redirect the GC log output inside some java application using java code?

Thank you in advance.

Hyb
  • 163
  • 1
  • 2
  • 9
  • Did you try `java.lang.System.setErr(...)` and `java.lang.System.setOut(...)` ? – Adrian Pronk Dec 05 '11 at 09:15
  • Yes, I used System.setErr() and System.setOut(). That does not work, the new out (or err source) is redirected, everything I write using System.out.print() is written in the file I use as out destination but when GC prints something it does not get trough the "out" variable (neither trough err as well) and ends up on console. – Hyb Dec 05 '11 at 09:37
  • 2
    I should have looked more in depth in the catalina.sh. The startup command ends with `>> "$CATALINA_OUT" 2>&1 "&"`, thus everything that appears in console is redirected to catalina.out . Problem solved. – Hyb Dec 05 '11 at 10:27

2 Answers2

2

It has nothing to do with code; the catalina.out file is not a "real log", it's just a file into which the standard tomcat startup command pushes all it's output.

In other words, this is a shell question, not a java one :)

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
0

Tomcat must indeed be using System.setErr() and System.setOut(). There's a great discussion about how that works elsewhere on this site: java: "final" System.out, System.in and System.err?

Tomcat redirects all console output to catalina.out. You might be able to configure this via the JULI log settings.

Community
  • 1
  • 1
Jilles van Gurp
  • 7,927
  • 4
  • 38
  • 46