public abstract class LogValues
extends java.lang.Object
Modifier and Type | Class and Description |
---|---|
static interface |
LogValues.StringProvider
Functional interface for use with
defer(com.launchdarkly.logging.LogValues.StringProvider) . |
Modifier and Type | Method and Description |
---|---|
static java.lang.Object |
defer(LogValues.StringProvider stringProvider)
Converts any method or lambda returning a string into an object for calling it lazily.
|
static java.lang.Object |
exceptionSummary(java.lang.Throwable e)
Returns an object whose
toString() method returns only the class name and
optional message of the exception. |
static java.lang.Object |
exceptionTrace(java.lang.Throwable e)
Returns an object that lazily constructs an exception stacktrace.
|
public static java.lang.Object defer(LogValues.StringProvider stringProvider)
This returns an object which delegates its toString()
implementation to the
specified method or lambda.
Sometimes log messages may include a computed value that has enough computational
overhead that you would prefer not to compute it unless it really will be logged.
The LDLogger
methods that take parameters of type Object
do not
call toString()
to convert those parameters to strings immediately; if
logging of this message has been disabled, for instance by level filtering, or if
all logging is disabled because the destination is Logs.none()
, then
toString()
is not called.
// Here, computeJsonData is only called if debug-level logging is enabled
logger.debug("The JSON data is: {}", () -> computeJsonData());
stringProvider
- a method or lambda that returns a stringstringProvider
if toString
is calledpublic static java.lang.Object exceptionSummary(java.lang.Throwable e)
toString()
method returns only the class name and
optional message of the exception.
That is already the behavior of Exception.toString()
, so this method just
returns the exception itself. It is provided here in order to make the intended
behavior clearer in logging code (and for consistency with the
LogValues.ExceptionSummary
method in the .NET equivalent of this package).
Also, future versions of this package might define additional behavior, with the
constraint that it will never include a stacktrace.
e
- an exceptionpublic static java.lang.Object exceptionTrace(java.lang.Throwable e)
Calling toString()
on the object returned by this method returns the
exception's stacktrace as a string. This string is not constructed unless
toString()
is called, so writing exceptions to the log in this way incurs
very little overhead if logging is not enabled for the specified log level.
try { ... }
catch (Exception e) {
logger.error("Error: {}", LogValues.exceptionSummary(e));
logger.debug("{}", LogValues, exceptionTrace(e));
e
- an exceptiontoString()
method provides a stacktrace