Re: How do I know if my program runs with assertions
I am using assertions to test my code. I am also using
java.util.logging which on regular basis writes only the most
What I need is to increase verbosity of logging (and forward the
massages to System.err) when JVM is running in assertion mode, i.e. [sic]
-ea flag has been used.
I wrapped java.util.logging.Logger in my own object as a singleton,
Why? That defeats part of the purpose of Logger. Loggers are tunable
on a class-by-class basis, if you name them for the classes in which
"The [logging] namespace should typically be aligned with the Java
packaging namespace ..."
Assertions are also enabled on a class-by-class basis.
which starts in a static initialization block so it happens on the
very beginning of the program. I would like to put the stuff there,
I prefer to make loggers instance variables. You can initialize
static loggers (if you use those) in the static initialization block
of the classes that use them (virtually all your classes). I
initialize instance Logger variables in the instance initializers.
A static initializer does not run at "the very beginning of the
program", it runs when the particular class is loaded, and that
doesn't happen until the class is referenced in a particular way.
i.e. [sic] the part that detects assertions and changes the behaviour of =
"Detecting" assertions isn't done in the static initializer, but
"... prior to the execution of any field initializers for class
variables (=A188.8.131.52) and static initializers (=A78.7), the class's class
loader determines whether assertions are enabled or disabled ...".
Assertions are enabled or disabled on a class-by-class basies.
Set the logging level and assertion levels extrinsically to the
program. For example, you can modify the logging configuration file
whenever you plan to run with "-ea".
Another way is to assert a logging initialization method that returns
This will only execute the method when assertions are enabled, and if
you write the method to return 'true' it will not throw an
This will work equally well with a static method for static loggers or
an instance method for instance loggers.