「Java8」lambda式でdebugログを出力する方法

サンプルコード

public class LogWrapper implements Logger {
    private Logger logger;

    public LogWrapper(Logger logger) {
        this.logger = logger;
    }
    /**
     * lambdaのdebugメソッド
     * @param func
     */
    public void debug(LogArgs func) {
        if (logger.isDebugEnabled()) {
            String[] args = func.args();
            debug(args[0], slice(args, 1));
        }
    }

    private String[] slice(String[] strs, int start) {
        final int LEN = strs.length;
        if (LEN < start + 1) {
            return null;
        }

        String[] slices = new String[LEN - 1];
        for (int ix = 0 ; ix < slices.length ; ++ix) {
            slices[ix] = strs[ix + start];
        }

        return slices;
    }

使い方
log.debug( () -> new String[] {“test, {}", name});

Java

Posted by arkgame