Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add timestamps GC log file names in a Java Service under Windows?

I've got a Java Application running against Apache Tomcat, under Windows. There are two ways of running this application - either as a Windows Service, or from a batch file to invoke Tomcat manually.

When I start the application via the batch file, I use the following to add GC logs to the JVM parameters:

-Xloggc=%~dp0..\logs\gc-%DATE:~-4%.%DATE:~4,2%.%DATE:~7,2%_%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.log

That causes the GC logs to be output with the date in the file name - but when run as a service, the DATE and TIME variables do not resolve correctly.

When using a Windows Service, what variables must I use in my JVM parameters to ensure the date and time are appended to the GC logs?

I have tried to simplify it - gc-%DATE%.log, gc-${DATE}.log, in all cases the variable will not resolve.

Any help is appreciated!

Edit: Please note that any solution must work when the application runs as a Windows Service. Stand alone is fine, we've got that covered. It's only when a Windows Service is used.

like image 578
EvilChookie Avatar asked Jan 06 '15 22:01

EvilChookie


People also ask

What is the option to enable the GC trace in Java parameters?

To activate the detailed GC logging, we use the argument -XX:+PrintGCDetails.

What is XLOG GC *?

-Xlog:gc is a command-line option to perform unified GC logging using the Azul Platform Prime unified logging framework. Example 1. Syntax of the -Xlog option. -Xlog:gc:<output>:<decorators>:<output_options>


4 Answers

Using java 8 you have access to two new substitutions that is very helpful. %t and %p has been added where %t is the timestamp and %p is the process id.

-Xloggc:C:\logs\gc_%t_%p.log will result in a file named something like this in C:\logs.

gc_2016-08-12_10-12-29_pid7320.log

This feature is not documented very well. I found out about it here: OpenJDK Bug JDK-6950794

As I also want this to work in a windows service I've added the use of this feature to the tomcat service.bat script in the following way.

--JvmOptions "...;-Xloggc:%CATALINA_BASE%\logs\gc-%%t.log;..."

Note the %%t. The extra % is needed as an escape char.

like image 137
LAnd Avatar answered Oct 25 '22 18:10

LAnd


EDIT - Using Tomcat as a Windows Service

I've installed Tomcat as a Windows service using the documentation.

Then I've run this command line from the Tomcat bin directory :

"tomcat7.exe" //US//Tomcat7 ++JvmOptions=-Xloggc:gc-%DATE:~-4%.%DATE:~4,2%.%DATE:~7,2%_%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.log

Note : //US is the Updating services command. Tomcat7 is the name of the service.

Then restart the service, now the log path is correctly resolved.

It should work assuming you have existing directories. Your expression with date expect a directory named gc-2015.1 and create a file named .01_13.43.35.log (if we are the 15th of January 2015 at 13h 43min 35sec)


If you are using catalina.bat

Why it's not working? Because you suffer of a problem of delayed expansion.

You have to enable delayed expansion using setlocal ENABLEDELAYEDEXPANSION in catalina.bat

Then replace % with !. You will ends up with -Xloggc="!~dp0..\logs\gc-!DATE:~-4!.!DATE:~4,2!.!DATE:~7,2!_!TIME:~0,2!.!TIME:~3,2!.!TIME:~6,2!.log".

like image 35
alain.janinm Avatar answered Oct 25 '22 17:10

alain.janinm


I hope this link help you to solve it

http://tomcat.10.x6.nabble.com/gc-log-filename-variables-in-windows-td4987672.html

like image 43
m.hassaballah Avatar answered Oct 25 '22 18:10

m.hassaballah


Even my observations were the same.

It works when we start tomcat using startup.bat and have values set for JAVA_OPTS variable in catalina.bat which are related to gc logging.

It makes sense why it works because the batch interpretator would resolve this

-Xloggc=%~dp0..\logs\gc-%DATE:~-4%.%DATE:~4,2%.%DATE:~7,2%_%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%.log

to something like this

-Xloggc:C:\logs\gc-2015.01.17_17.14.09.log

and pass it as argument to the jvm.

Nearest what I could get was by making the following configuration using tomcat7w.exe which is located under bin directory.

Apache Tomcat Properties

Log files would start rolling once they reach specified size. You can find the timestamp in the log files.

Had also tried configuring setenv.bat but it looks as if the tomcat7.exe does not use it.

Hope this helps.

like image 44
jithin iyyani Avatar answered Oct 25 '22 17:10

jithin iyyani