java 获取具体子类_java – 如何获取一个子类的记录器?

这个原因是在JavaDoc for

LogRecord中:

Note that if the client application has not specified an explicit source method name and source class name,then the LogRecord class will infer them automatically when they are first accessed (due to a call on getSourceMethodName or getSourceClassName) by analyzing the call stack.

在这种情况下,调用堆栈以SuperClass定义的方法结束,所以LogRecord假定它是被调用的类.如果要查看此操作,请执行以下代码:

public class Example {

public static class Superclass {

public void foo() {

new Exception().printStackTrace();

}

}

public static class Subclass extends Superclass {

// nothing here

}

public static void main(String[] argv)

throws Exception

{

Superclass s = new Subclass();

s.foo();

}

}

编辑:我不使用j.u.l,但希望有一个简单的方法来配置输出(像其他所有的logger实现提供).看来你必须实现自己的Formatter类,并使用java.util.logging.ConsoleHandler.formatter属性指定它.

如果可能,我建议切换到SLF4J.您可以将所有现有的j.u.l代码通过SLF4J桥接到实际的记录器,从而可以更好地控制其输出(如Log4J或Logback).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值