Wow! Please! Do it again!: Dev: SLF4J 1.7.0 (Logger) now supports varargs

Friday, September 7, 2012

Dev: SLF4J 1.7.0 (Logger) now supports varargs

Prior to SLF4J 1.7.0 (1.6.6 and below)

    String name = "1";
    Long id = 1L;
    int age = 99;

    // one or more variables
    logger.info("[doSomething: id]={}", id);
    logger.info("[doSomething: id, name]={},{}", id, name);

    // 3 or more variables
    logger.info("[doSomething: id, name, age]={},{},{}", new Object[] {id, name, age});

In SLF4J 1.7.0, we need not declare an Object array for >= 3 variables.

    String name = "1";
    Long id = 1L;
    int age = 99;
    String status = "A";

    // one or more variables
    logger.info("[doSomething: id]={}", id);
    logger.info("[doSomething: id, name]={},{}", id, name);

    // 3 or more variables
    logger.info("[doSomething: id, name, age]={},{},{}", id, name, age);
    logger.info("[doSomething: id, name, age, status]={},{},{},{}", id, name, age, status);

Read more here (6th of September, 2012).