【问题标题】:How can I use a static method with another returntype like the class where the method is coded如何将静态方法与另一个返回类型一起使用,例如编码该方法的类
【发布时间】:2017-03-10 07:43:41
【问题描述】:

我正在尝试编写一个新的 Logger 类,它使用 org.slf4j.Logger 和它自己的 LogUtil 类。

所以这是我的问题。我想尝试创建一个名为getInstance() 的静态方法,它将使用Vlogger 类的变量进行初始化。但是方法的返回类型是Logger

public class VLogger  {
    private static LogMessageBuilder logMessageBuilder;
    private LogUtil logUtil;

    public static Logger getInstance() {
        // String className = new Exception().getStackTrace()[1].getClassName();
        return LoggerFactory.getLogger(Reflection.getCallerClass().toString());
    }}

在这里你可以看到我想要的旧电话和新电话:

Vlogger vlog = VLogger.getInstance();

对比:

private static Logger LOG =
    LoggerFactory.getLogger(ResourceStatements.class);

【问题讨论】:

  • 不要。做。这个。请。只需按预期使用 Slf4j - 这种“日志记录实用程序class”只会让事情变得更糟。
  • P.S.你知道什么是拼写检查器……对吧?
  • P.P.S 如果您正在考虑展开堆栈以计算调用者 - 也不要这样做。简而言之——放弃这个想法;太可怕了。
  • @BoristheSpider 哈哈。 +3 虚拟评论支持,分布在您认为合适的地方
  • 有什么好笑的?

标签: java logging static


【解决方案1】:

我得到了答案:

public static VLogger getInstance() {
        VLogger result = new VLogger();
        result.l = LoggerFactory.getLogger(new Exception().getStackTrace()[1].getClassName());
        return result;
    }

和电话:

vLogger.message("Folgende Statements wurden verwednet").att(test, m).debug();

【讨论】:

  • @BoristheSpider
猜你喜欢
  • 2023-04-03
  • 2021-03-12
  • 2015-02-13
  • 2011-05-20
  • 1970-01-01
  • 1970-01-01
  • 2013-10-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多