views:

1404

answers:

5

I want to change the logging level depending if I'm debbugging or not, but I can't find a code snippet to check if the application is running in debug mode.

I'm using eclipse to debug the application, so if the solution only works within Eclipse it will be fine.

+1  A: 

If you are setting the debug level from your own program, may be a line like:

public static final boolean DEBUG_MODE = !System.getProperty("java.vm.info", "").contains("sharing");

would do the trick.

Just tested it in eclipse3.5:

package test;

public class Test
{

    /**
     * @param args
     */
    public static void main(String[] args)
    {
        System.out.println(System.getProperty("java.vm.info", ""));
    }

}

will display:

mixed mode, sharing

if launched without debug

mixed mode

if executed with debug launcher


Joachim Sauer comments:

This is highly system depending.
I assume the "sharing" indicates that cross-VM class-sharing is active.
This is a very new feature and is only available on some platforms.
Furthermore there can be many possible reasons to en- or disable it, so I wouldn't use this for debug-mode detection.

(Note: I tested it with the latest jdk1.6b14. I leave this as a CW answer.)

VonC
I was thinking there might be a property, but my tests of full dumps of properties from a debug launch and a run launch (using Eclipse 3.3, Java 1.6.0_13, Ubuntu 9.04) were identical. (I also tried just your property, including a debug run where I stepped over the System.out line, and both the Run and Debug launches gave 'mixed mode'.
gojomo
This is highly system depending. I assumie the "sharing" indicates that cross-VM class-sharing is active. This is a very new feature and is only available on some platforms. Furthermore there can be many possible reasons to en- or disable it, so I wouldn't use this for debug-mode detection.
Joachim Sauer
@Joachim: good point (I am with the latest jdk 1.6b14 here). I will update my answer and leave it for archive as Community Wiki.
VonC
A: 

Have a look here:

http://wiki.eclipse.org/FAQ_How_do_I_use_the_platform_debug_tracing_facility%3F

Moreover, I think you can't know if your app is run in debug mode. The only thing you can do is to pass an argument to your JVM when you debug.

Manu

Manuel Selva
that is for eclipse platform/plugin tracing, not normal debugging.
J-16 SDiZ
+7  A: 

You could modify the Debug Configuration. For example add a special VM argument only in the Debug Configuration. You can use System.getProperties() to read the supplied arguments.

Even better, modify the configurations (Run and Debug) to load a different logging configuration file. It isn't good if you need to write code to determine the logging level. This should only be a matter of configuration.

kgiannakakis
+2  A: 

Have you tried add a vm argument in the eclipse run config?

Something like

-Dxxxx=true

and you can use Boolean.getProperty("xxxx") to check this.

J-16 SDiZ
+2  A: 

There is not a way to reliably determine if any given JVM is in debug mode, and relying on artifacts will just break your code some time in the future.

You will therefore need to introduce a methology yourself. Suggestions:

  • A system property.
  • An environment variable (shell variable like $HOME or %HOME%)
  • Ask the JVM about the physical location of a given resource - http://www.exampledepot.com/egs/java.lang/ClassOrigin.html - and based on it, make your decision (does the path contain the word "debug"? is it inside a jar or an unpacked class file? etc).
  • JNDI
  • The existance or content of a particular resource.
Thorbjørn Ravn Andersen