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

5

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.

Peder answered 6/1, 2015 at 22:54 Comment(0)
W
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".

Warison answered 11/1, 2015 at 12:43 Comment(6)
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?Peder
@Peder 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.Warison
@Peder It seems I got the solution, please see my edit. Hope it will work for you.Warison
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 :)Peder
@Peder Ok thanks :) It should work, let me know!Warison
Yep this is money.Doorn
B
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.

Boscage answered 12/8, 2016 at 8:32 Comment(1)
Awesome, exactly what I was looking for! Those other answers are not locale independent...Filmdom
W
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".

Warison answered 11/1, 2015 at 12:43 Comment(6)
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?Peder
@Peder 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.Warison
@Peder It seems I got the solution, please see my edit. Hope it will work for you.Warison
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 :)Peder
@Peder Ok thanks :) It should work, let me know!Warison
Yep this is money.Doorn
O
1

I hope this link help you to solve it

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

Ostium answered 13/1, 2015 at 22:44 Comment(1)
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.Peder
P
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.

Parrett answered 17/1, 2015 at 11:54 Comment(0)
K
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.

Kinsella answered 17/1, 2015 at 20:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.