Is there a Java 1.5 varargs API for slf4j yet?

坚强是说给别人听的谎言 提交于 2019-12-12 09:28:53

问题


I want to get rid of this lot...

public void info(String msg);
public void info(String format, Object arg);
public void info(String format, Object arg1, Object arg2);
public void info(String format, Object[] argArray);

...and replace it with this one...

public void info(String format, Object ... args);

...so that my logging syntax doesn't have to change depending on the number of arguments I want to log. There seems to be lots of discussion and work around it, but where is it? Or should I wrap the wrapper that is slf4j?


回答1:


The real question is "Why must a jdk < 5 be supported by this any longer"? If you have an older version of java, then use the older API. It's that simple. Why not make this fit better into the current java world? I mean, JDK 5 isn't even supported without a support contract from Sun/Oracle. Backward compatibility is a joke in this case.




回答2:


This is finally solved. SLF4J 1.7.0 now required JDK 1.5 and has backward-compatible varargs methods.




回答3:


No.

The issue is still open how to do it right while still maintaining 100% backwards compatibility.

Feel free to see the discussion at http://bugzilla.slf4j.org/show_bug.cgi?id=31




回答4:


What about this:

package util;

public class Util {
  public static Object[] va(Object... args) {
    return args;
  }
}

package foo;
import static util.Util.va;
...
 logger.info("a {}, b {}, c c {}", va("A", "B", "C"));
...

you can use va() in other places too.




回答5:


From reading the SLF4J javadoc for Logger the simple answer would appear to be no. From what I have read they want to stay compatible with older versions of the JDK.

If you are not really tied to using SLF4J then maybe log5j is an option?




回答6:


There is a solution of using varargs with SLF4J.

There is an open source project called Lumberjack that extends SLF4J to provides varargs logging methods. The extension is very natural, you don't feel any difference compared to using SLF4J (this is because Lumberjack is only a wrapper around SLF4J, so all the functionality is still provided by SLF4J).

Example usage:

JackLogger logger = JackLoggerFactory.getLogger(LoggerFactory.getLogger(Weather.class));

logger.info("Hello {}! The current time is {}:{}:{}, and after {} hours the weather will be {}.", "Jack", 13, 30, 0, 5, "sunny");

Lumberjack website: https://github.com/bogdanu/lumberjack

The Lumberjack license is the same as SLF4J's license, the MIT license, so there is no additional licensing restriction.

Disclaimer: I am the author of Lumberjack




回答7:


Try jcabi-log toolkit, which wraps SLF4J logging with a conveninent vararg interface.



来源:https://stackoverflow.com/questions/1952947/is-there-a-java-1-5-varargs-api-for-slf4j-yet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!