7

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.

EvilChookie
  • 563
  • 3
  • 14
  • 31

5 Answers5

8

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.

LAnd
  • 101
  • 1
  • 4
5

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".

alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • It works fine when we launch the product using catalina.bat. The problem is that the Windows Service doesn't use catalina.bat at all, so whatever we put in there is flat out ignored. The question was to get this form of GC logging into the application when running as a service, not as a standalone application. Unless of course, the service somehow invokes catalina.bat? – EvilChookie Jan 15 '15 at 00:45
  • @EvilChookie It seems it doesn't. I guess you install it as a service following tomcat doc. I realize my test was flawed because I call catalina.bat in my service but it's probably not the case for you. I'll try with the installation recommended by Tomcat and get back to you. – alain.janinm Jan 15 '15 at 06:26
  • @EvilChookie It seems I got the solution, please see my edit. Hope it will work for you. – alain.janinm Jan 15 '15 at 18:49
  • Now that looks promising! I'm not in a position to check it, but will let you know tomorrow. Thanks for the persistance, you at least get the bounty :) – EvilChookie Jan 18 '15 at 06:48
  • @EvilChookie Ok thanks :) It should work, let me know! – alain.janinm Jan 18 '15 at 08:53
  • Yep this is money. – Nicholas DiPiazza Jun 26 '16 at 13:49
1

I hope this link help you to solve it

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

m.hassaballah
  • 250
  • 1
  • 5
  • Unfortunately, it does not - we've already looked at this thread. We're not just adding GC logging, we're adding GC logging to a windows service. – EvilChookie Jan 15 '15 at 00:46
1

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.

jithin iyyani
  • 751
  • 1
  • 7
  • 19
0

Have you tried invoking a the batch file through a wrapper like this one ?

Edit: Also keep in mind having to use delayed expansion if necessary as @alain-janinm suggested.

Ravindra HV
  • 2,558
  • 1
  • 17
  • 26