11

Is there a way to forward the garbage collection information (for example the output of -XX:+PrintGCDetails or -verbose:gc) to a logger within the Java application (sl4j + logback in my case)?

assylias
  • 321,522
  • 82
  • 660
  • 783

2 Answers2

6

Those messages are generated by the JVM native process and not from Java code, so you just can

  1. Redirect output to file using -Xloggc (no rotation)
  2. Pipe stdout directly (several rotation options)
Community
  • 1
  • 1
fglez
  • 8,422
  • 4
  • 47
  • 78
3

An interesting approach would be to redirect gc.log to a named pipe -Xloggc:/my/named/pipe How to write GC log to named pipe

then read that pipe form the application itself: How to open a Windows named pipe from Java?

and log to an arbitrary (e.g. async rolling) logback logger from the code.

Tried that on a Windows machine. Unfortunately, it is trickier to setup on Windows than on Linux.

On Windows it works basically with help of an additional Powershell script (can be a dedicated application as well). This sample project also contains a demo application that can be used right away to test the GC logs redirection to Logback via SLF4J.

Community
  • 1
  • 1
Sergey Shcherbakov
  • 4,534
  • 4
  • 40
  • 65