Logging using function as decorator in Guava I needed to add a logger into my flow based on FluentIterable from the Guava library. I have a lot of functions and in some cases I would like to add logging when in other cases (for the same function) I would not. 

Of course the natural solution for this problem is to use the decorator pattern, so I created something like this:

/**
 * Decorator function that add logging into function passed as parameter.
 *
 * @param &lt;I&gt; input type
 * @param &lt;O&gt; output type.
 */
public class DecoratorLoggerInfoFunc&lt;I, O&gt; implements Function&lt;I, O&gt; {

    /**
     * Original function
     */
    private final Function&lt;I, O&gt; function;

    /**
     * Current logger.
     */
    private final Logger logger;

    /**
     * "Builder" that takes function and return function decorated by logger.
     *
     * @param function function to decorate.
     * @param &lt;I&gt;      input type
     * @param &lt;O&gt;      output type
     * @return function with logger.
     */
    public static &lt;I, O&gt; Function&lt;I, O&gt; of(Function&lt;I, O&gt; function) {
        checkNotNull(function, "Function to decorate cannot be null");
        return new DecoratorLoggerInfoFunc&lt;I, O&gt;(function);
    }

    /**
     * Initialize logger!
     *
     * @param function original function.
     */
    private DecoratorLoggerInfoFunc(Function&lt;I, O&gt; function) {
        this.function = function;
        this.logger = LoggerFactory.getLogger(function.getClass());
    }

    /**
     * Flow:
     * &lt;ul&gt;
     * &lt;li&gt;Call original function&lt;/li&gt;
     * &lt;li&gt;Log input and output&lt;/li&gt;
     * &lt;li&gt;Return output of original function&lt;/li&gt;
     * &lt;/ul&gt;
     *
     * @param input
     * @return
     */
    @Override
    public O apply(I input) {
        O output = function.apply(input);
        logger.info("Input is {} Output is {}", input, output);
        return output;
    }
}


Is this code a good way to solve my problem? Is there a better way to do this?
The concept seems fine to me. The problem is that you will still need to decide whether to decorate, or not decorate the function, and that is done outside the function. That makes it a once-per-create decision, which is OK, but you may still end up with a lot of conditions in your code:

Function f = logFunction() ? new DecoratorLoggerInfoFunc(f) : f;


The actual apply method is not very robust though. I would include logging of any potential exceptions that are not declared to be thrown on the function itself...:

public O apply(I input) {
    try {
        O output = function.apply(input);
        logger.info("Input is {} Output is {}", input, output);
        return output;
    } catch (RuntimeException e) {
        logger.warn("Exception running with input {}", input, e);
        throw e;
    }
}