Varargs in java.util.logging java 8

后端 未结 1 1772
独厮守ぢ
独厮守ぢ 2021-01-25 04:21

We are using java.util.logging

void log(Level level, Throwable thrown, Supplier msgSupplier) 

and a few more convenience methods

1条回答
  •  醉话见心
    2021-01-25 04:30

    But no varargs method. How come? Does this seem like a future enhancement...

    There are multiple requests for Use varargs in java.util.logging.Logger on the OpenJDK site over the years. The API wasn't really maintained after JDK 5. Logging was updated in JDK 8 for Lambda Expressions but the logging guide wasn't updated for best practices just a few examples in the java.util.logging.Logger.

    I created helper public static void log(Logger logger, Level level, Throwable t, String msg, Object... params)

    You might want to use the version in How can I log with params with a thrown? as it will retain the correct class and method name because the caller is still responsible for publishing the log record.

    is there any good reason not to have something like log(Level level, Throwable t, String msg, Object... params)

    It is probably safe addition. Oracle ran a survey and it is clear they would prefer you to use a lambda or method reference with one of the log methods that takes a Supplier and lean on the java.util.Formatter providing the supporting for var-args.

    } catch (IllegalArgumentException iae) {
        logger.log(Level.SEVERE, iae, () -> String.format("%1$s is too many.", count)); 
    }
    

    Sugary sweet lambda syntax over vegetables!

    0 讨论(0)
提交回复
热议问题