public final class Log extends Handler
Constructor and Description |
---|
Log()
Creates a new instance of Log
|
Modifier and Type | Method and Description |
---|---|
static void |
assertInstances(String msg)
Assert to verify that all collected instances via
Log.enableInstances(java.util.logging.Logger, java.lang.String, java.util.logging.Level)
can disappear. |
static void |
assertInstances(String msg,
String... names)
Assert to verify that all properly named instances collected via
Log.enableInstances(java.util.logging.Logger, java.lang.String, java.util.logging.Level)
can disappear. |
void |
close() |
static void |
controlFlow(Logger listenTo,
Logger reportTo,
String order,
int timeout)
Can emulate the execution flow of multiple threads in a deterministic
way so it is easy to emulate race conditions or deadlocks just with
the use of additional log messages inserted into the code.
|
static CharSequence |
enable(String loggerName,
Level level)
Enables logging for given logger name and given severity.
|
static void |
enableInstances(Logger log,
String msg,
Level level)
Starts to listen on given log and collect parameters of messages that
were send to it.
|
void |
flush() |
void |
publish(LogRecord record) |
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
public static CharSequence enable(String loggerName, Level level)
toString
.
The logging stops when the returned object is garbage collected.
loggerName
- the name to capture logging forlevel
- the level of details one wants to getpublic static void controlFlow(Logger listenTo, Logger reportTo, String order, int timeout)
The best example showing usage of this method is real life test. Read FlowControlTest.java to know everything about the expected usage of this method.
The method does listen on output send to a logger listenTo
by various threads and either suspends them or wake them up trying
as best as it can to mimic the log output described in order
.
Of course, this may not always be possible, so there is the timeout
value which specifies the maximum time a thread can be suspended while
waiting for a single message. The information about the internal behaviour
of the controlFlow method can be send to reportTo
logger,
if provided, so in case of failure one can analyse what went wrong.
The format of the order is a set of lines like:
THREAD:name_of_the_thread MSG:message_to_expectwhich define the order saying that at this time a thread with a given name is expected to send given message. Both the name of the thread and the message are regular expressions so one can shorten them by using
.*
or any other trick. Btw. the format of the order
is similar
to the one logged by the Log.enable(java.lang.String, java.util.logging.Level)
or NbTestCase.logLevel()
methods,
so when one gets a test failure with enabled logging,
it is enough to just delete the unnecessary messages, replace too specific
texts like @574904
with .*
and the order is
ready for use.listenTo
- the logger to listen to and guide the execution according to messages sent to itreportTo
- the logger to report internal state to or null
if the logging is not neededorder
- the string describing the expected execution order of threadstimeout
- the maximal wait time of each thread on given message, zero if the waiting shall be infinitepublic static void enableInstances(Logger log, String msg, Level level)
Log.assertInstances(java.lang.String)
.log
- logger to listen on, if null, it uses the standard timers/counters onemsg
- name of messages to collect, if null, all messages will be recordedlevel
- level of messages to recordpublic static void assertInstances(String msg)
Log.enableInstances(java.util.logging.Logger, java.lang.String, java.util.logging.Level)
can disappear. Uses NbTestCase.assertGC(java.lang.String, java.lang.ref.Reference<?>)
on each of them.msg
- message to display in case of potential failurepublic static void assertInstances(String msg, String... names)
Log.enableInstances(java.util.logging.Logger, java.lang.String, java.util.logging.Level)
can disappear. Uses NbTestCase.assertGC(java.lang.String, java.lang.ref.Reference<?>)
on each of them.msg
- message to display in case of potential failurenames
- list of names of instances to test for and verify that they disappear