java.util.logging java 8 中的可变参数
Varargs in java.util.logging java 8
我们正在使用 java.util.logging
void log(Level level, Throwable thrown, Supplier<String> msgSupplier)
以及一些更方便的方法,例如
logp(Level level, String sourceClass, String sourceMethod,
String msg, Object params[])
但是没有可变参数方法。怎么会?这看起来像是未来的增强功能,还是有任何充分的理由不使用类似的东西:
public void log( Level level, Throwable t, String msg, Object... params) {
// throw new RuntimeException("No yet impl");
if (!isLoggable(level)) {
return;
}
LogRecord lr = new LogRecord(level, msg);
lr.setParameters(params);
if (t != null) {
lr.setThrown(t);
}
doLog(lr);
}
我创建了助手:
public static void log(Logger logger, Level level, Throwable t, String msg, Object... params) {
// throw new RuntimeException("No yet impl");
if (!logger.isLoggable(level)) {
return;
}
LogRecord lr = new LogRecord(level, msg);
lr.setParameters(params);
if (t != null) {
lr.setThrown(t);
}
logger.log(lr);
}
很想在最后一行调用 doLog 而不是 log,但 doLog 是一个私人助手!不知道为什么,因为我无法设置捆绑包等 - 希望它是
public void doLog(LogRecord lr) 而不是 private.
无论如何,如果我们做同样的事情,下面的方法就可以使用我们自己的可变参数方法?
// resource bundle and then call "void log(LogRecord)".
private void doLog(LogRecord lr) {
lr.setLoggerName(name);
final LoggerBundle lb = getEffectiveLoggerBundle();
final ResourceBundle bundle = lb.userBundle;
final String ebname = lb.resourceBundleName;
if (ebname != null && bundle != null) {
lr.setResourceBundleName(ebname);
lr.setResourceBundle(bundle);
}
log(lr);
}
But no varargs method. How come? Does this seem like a future enhancement...
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)
您可能希望使用 中的版本,因为它将保留正确的 class 和方法名称,因为调用者仍然负责发布日志记录。
is there any good reason not to have something like log(Level level, Throwable t, String msg, Object... params)
这可能是安全的添加。 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<String>
and lean on the java.util.Formatter 提供对可变参数的支持。
} catch (IllegalArgumentException iae) {
logger.log(Level.SEVERE, iae, () -> String.format("%1$s is too many.", count));
}
蔬菜上的甜蜜 lambda 语法!
我们正在使用 java.util.logging
void log(Level level, Throwable thrown, Supplier<String> msgSupplier)
以及一些更方便的方法,例如
logp(Level level, String sourceClass, String sourceMethod,
String msg, Object params[])
但是没有可变参数方法。怎么会?这看起来像是未来的增强功能,还是有任何充分的理由不使用类似的东西:
public void log( Level level, Throwable t, String msg, Object... params) {
// throw new RuntimeException("No yet impl");
if (!isLoggable(level)) {
return;
}
LogRecord lr = new LogRecord(level, msg);
lr.setParameters(params);
if (t != null) {
lr.setThrown(t);
}
doLog(lr);
}
我创建了助手:
public static void log(Logger logger, Level level, Throwable t, String msg, Object... params) {
// throw new RuntimeException("No yet impl");
if (!logger.isLoggable(level)) {
return;
}
LogRecord lr = new LogRecord(level, msg);
lr.setParameters(params);
if (t != null) {
lr.setThrown(t);
}
logger.log(lr);
}
很想在最后一行调用 doLog 而不是 log,但 doLog 是一个私人助手!不知道为什么,因为我无法设置捆绑包等 - 希望它是
public void doLog(LogRecord lr) 而不是 private.
无论如何,如果我们做同样的事情,下面的方法就可以使用我们自己的可变参数方法?
// resource bundle and then call "void log(LogRecord)".
private void doLog(LogRecord lr) {
lr.setLoggerName(name);
final LoggerBundle lb = getEffectiveLoggerBundle();
final ResourceBundle bundle = lb.userBundle;
final String ebname = lb.resourceBundleName;
if (ebname != null && bundle != null) {
lr.setResourceBundleName(ebname);
lr.setResourceBundle(bundle);
}
log(lr);
}
But no varargs method. How come? Does this seem like a future enhancement...
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)
您可能希望使用
is there any good reason not to have something like
log(Level level, Throwable t, String msg, Object... params)
这可能是安全的添加。 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<String>
and lean on the java.util.Formatter 提供对可变参数的支持。
} catch (IllegalArgumentException iae) {
logger.log(Level.SEVERE, iae, () -> String.format("%1$s is too many.", count));
}
蔬菜上的甜蜜 lambda 语法!