Class LazyArgs


  • public final class LazyArgs
    extends Object
    Static utility methods for lazy argument evaluation in Flogger. The lazy(LazyArg) method allows lambda expressions to be "cast" to the LazyArg interface.

    In cases where the log statement is strongly expected to always be enabled (e.g. unconditional logging at warning or above) it may not be worth using lazy evaluation because any work required to evaluate arguments will happen anyway.

    If lambdas are available, users should prefer using this class rather than explicitly creating LazyArg instances.

    See Also:
    Original Java code of Google Flogger
    • Method Detail

      • lazy

        public static <T> LazyArg<T> lazy​(LazyArg<T> lambdaOrMethodReference)
        Coerces a lambda expression or method reference to return a lazily evaluated logging argument. Pass in a compatible, no-argument, lambda expression or method reference to have it evaluated only when logging will actually occur.
        
         logger.atFine().log("value=%s", lazy(() -> doExpensive()));
         logger.atWarning().atMostEvery(5, MINUTES).log("value=%s", lazy(stats::create));
         
        Evaluation of lazy arguments occurs at most once, and always in the same thread from which the logging call was made.

        Note also that it is almost never suitable to make a toString() call "lazy" using this mechanism and, in general, explicitly calling toString() on arguments which are being logged is an error as it precludes the ability to log an argument structurally.