public final class LogCapture extends java.lang.Object implements LDLogAdapter
Calling Logs.capture() provides a LogCapture object that accumulates
all log output from any code that is configured to use it as the log adapter. This is
mainly intended for testing.
All messages that come to this object are captured regardless of the log level. If you
want to filter out messages below a certain level, you can apply Logs.level(LDLogAdapter, LDLogLevel)
and pass the resulting filtered adapter to whatever component will be doing the logging,
in place of the original LogCapture object.
Example of usage in the server-side Java SDK:
LogCapture logSink = Logs.capture();
LDConfig config = new LDConfig.Builder()
.logging(
Components.logging().adapter(logSink)
)
.build();
// create the LDClient and do some things that might produce log output...
// now, retrieve the captured output
List<LogCapture.Message> messages = logSink.getMessages();
| Modifier and Type | Class and Description |
|---|---|
static class |
LogCapture.Message
Information about a captured log message.
|
LDLogAdapter.Channel, LDLogAdapter.IsConfiguredExternally| Modifier and Type | Method and Description |
|---|---|
LogCapture.Message |
awaitMessage(LDLogLevel level,
long timeoutMilliseconds)
Removes and returns a captured log message of the specified level in FIFO
order, waiting if necessary until one is available.
|
LogCapture.Message |
awaitMessage(long timeoutMilliseconds)
Removes and returns a captured log message in FIFO order, waiting if
necessary until one is available.
|
java.util.List<LogCapture.Message> |
getMessages()
Returns all captured messages.
|
java.util.List<java.lang.String> |
getMessageStrings()
Returns all captured messages converted to strings.
|
LDLogAdapter.Channel |
newChannel(java.lang.String name)
Called internally by the SDK.
|
LogCapture.Message |
requireMessage(LDLogLevel level,
long timeoutMilliseconds)
Same as
awaitMessage(LDLogLevel, long), but throws an exception on timeout. |
LogCapture.Message |
requireMessage(long timeoutMilliseconds)
Same as
awaitMessage(long), but throws an exception on timeout. |
public LDLogAdapter.Channel newChannel(java.lang.String name)
newChannel in interface LDLogAdaptername - a logger name assigned by the SDKLDLogAdapter.Channelpublic java.util.List<LogCapture.Message> getMessages()
public java.util.List<java.lang.String> getMessageStrings()
The format is always "LEVEL:text".
public LogCapture.Message awaitMessage(long timeoutMilliseconds)
This method and requireMessage(long) allow you to use
LogCapture like a blocking queue, so a test can wait for log output
that is being generated by another thread.
timeoutMilliseconds - the maximum time to waitpublic LogCapture.Message awaitMessage(LDLogLevel level, long timeoutMilliseconds)
This method and requireMessage(long) allow you to use
LogCapture like a blocking queue, so a test can wait for log output
that is being generated by another thread.
level - the desired message level, or null for anytimeoutMilliseconds - the maximum time to waitpublic LogCapture.Message requireMessage(long timeoutMilliseconds)
awaitMessage(long), but throws an exception on timeout.timeoutMilliseconds - the maximum time to waitjava.lang.AssertionError - if no log message was available within the timeoutpublic LogCapture.Message requireMessage(LDLogLevel level, long timeoutMilliseconds)
awaitMessage(LDLogLevel, long), but throws an exception on timeout.level - the desired message level, or null for anytimeoutMilliseconds - the maximum time to waitjava.lang.AssertionError - if no log message was available within the timeout