15

I use this jvm option in order to create gc logs and enable rolling:

$ java -Xloggc:gc.log -verbose:gc -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+UseGCLogFileRotation -XX:NumberOfGCLogFiles=5  XX:GCLogFileSize=128K

However, I have a problem when I restart my application. After a restart, the first log file gc.log.0 is overwritten and the data of that file is not rolled to gc.log.1 and hence lost.

I'm wondering if I'm right and if there's a solution for this.

Thanks in advance!

ebarrenechea
  • 3,775
  • 1
  • 31
  • 37
alid
  • 163
  • 1
  • 7

2 Answers2

18

You can also use java own timestamps for that:

java -Xloggc:gc-%t.log ...(rest of your line)...

%t will be replaced with a timestamp by java (see https://bugs.openjdk.java.net/browse/JDK-6950794 for information and other formats supported by -Xloggc

Krzysztof Krasoń
  • 26,515
  • 16
  • 89
  • 115
  • My favorite solution to the problem, because it also works inside of properties files (such as wrapper.conf). Thanks! – dokaspar Oct 04 '16 at 21:11
  • When I tested this, the file for the pattern `gc_%t.log` looked like: `gc_2016-12-06_16-46-30.log.0.current` which contradicts the documentation you linked: `%t - date stamp when log file is created (format: YYYY-MM-DD)` – joseph Dec 06 '16 at 16:48
5

Same problem here, I fixed it by adding the timestamp to the gc log file name like this (in this case /etc/default/tomcat7):

DATE=`date +%Y-%m-%d-%H-%M`
JAVA_OPTS="-Xloggc:/var/log/tomcat7/gc-$DATE.log ..."

this way you keep your gc logs after restart as jvm starts with a different timestamp and does not overwrite gc.logs written before. you need to clean up these files manually from time to time (cronjob).

Janning Vygen
  • 8,877
  • 9
  • 71
  • 102