Search code examples
javalogginggarbage-collectionjvm

How to roll over GC logs if we move the file where the logs were being written


I have configured jvm gc logs to write to a file:

"-XX:+PrintGCDetails -XX:+PrintGCDateStamps -Xloggc:${LOG_DIR}/myGCLogs.gc"

When the application starts, if there is any exception during the startup process, I need to capture the logs in a backup folder and refresh the GC logs. What I do is move (mv) the file from the LOG_DIR to a LOG_BACKUP_DIR and then create a new myGCLogs.gc file. After this, I don't see the logs being written to the file anymore. I have to restart my service and then I see the logs getting written to it again.

Is there any solution to this scenario? Can i somehow roll over the GC logs so it starts writing to the new file again without having to restart the service? This is not the exact scenario but I tried to exemplify as much as i could. I can provide more details if needed.

Edit: I can try to work around this part where i don't move the log file if i have to roll back. But for the second part, when everything is successful, i need to move the all the logs to a new volume that we attach to the VM to persist the logs. Since this is now a different filesystem, doing a move of the actual file which was opened when the application started on the original filesystem, will stop writing GC logs until i restart. I was looking for some type of log rotation once we are all set up so that the new GC logs will now write on the new filesystem without having to do a restart. I have tried adding:

-XX:+UseGCLogFileRotation

to my configuration, and then tried:

jcmd <PID> GC.rotate_log

But it doesn't work for me. I get this error:

Target VM does not support GC log file rotation.

PS. I am running this on an EC2 instance.


Solution

  • Found my answer. I had to configure all these 3 flags:

    -XX:+UseGCLogFileRotation
    -XX:NumberOfGCLogFiles
    -XX:GCLogFileSize
    

    And after that, ran the command:

    jcmd <PID> GC.rotate_log
    

    and it actually rolled over the logs.