Re: How do I know if my program runs with assertions
dgront wrote:
I am using assertions to test my code. I am also using
java.util.logging which on regular basis writes only the most
important messages.
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
they appear.
"The [logging] namespace should typically be aligned with the Java
packaging namespace ..."
<http://java.sun.com/javase/6/docs/technotes/guides/logging/
overview.html>
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.
<http://java.sun.com/docs/books/jls/third_edition/html/
execution.html#12.4>
i.e. [sic] the part that detects assertions and changes the behaviour of =
my
logger.
"Detecting" assertions isn't done in the static initializer, but
"... prior to the execution of any field initializers for class
variables (=A78.3.2.1) and static initializers (=A78.7), the class's class
loader determines whether assertions are enabled or disabled ...".
<http://java.sun.com/docs/books/jls/third_edition/html/
statements.html#14.10>
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
<http://java.sun.com/javase/6/docs/technotes/guides/logging/
overview.html#1.8>
whenever you plan to run with "-ea".
Another way is to assert a logging initialization method that returns
'true'.
assert setDetailedLogging();
This will only execute the method when assertions are enabled, and if
you write the method to return 'true' it will not throw an
AssertionError.
This will work equally well with a static method for static loggers or
an instance method for instance loggers.
--
Lew