扩展java.util.logging.Logger不起作用 [英] extends java.util.logging.Logger not working

查看:172
本文介绍了扩展java.util.logging.Logger不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想扩展java.util.logging.Logger类。

I want to extend the java.util.logging.Logger class.

我有以下代码。我在我们的应用程序中强迫一个错误,我创建的类不被调用。

I have the following code. I am "forcing an error" in our app and the class I created is not being called.

这是我正在生成的错误,但它不会在我写的代码中:

Here is the error I am generating on purpose but it doesn't go in the code I wrote:

//Generate error to test the logger
String[] myStringArray = new String[3];
String test;
test = myStringArray[5];    

我也尝试过这个,但即使我有相同的功能,它仍然不会在我的代码中定义:

I also tried this but it still doesn't go in my code even if I have the same function definition:

logger.log(Level.FINE, "test my logger");

这是我的记录器扩展名。当一个未处理的异常被提高时,这可能被触发。

Here is my logger extension. Is it possible for this to get triggered when an unhandled exception is "raised".

import java.util.logging.Level;
import java.util.logging.LogRecord;

public class MyLogger extends java.util.logging.Logger
{

    public MyLogger(String name, String resourceBundleName)
    {
        super(name, resourceBundleName);
        runMyLog();
    }


    public void log(Level level, String msg) {
        runMyLog();
    }   

    public void error(String msg)
    {
        super.log(Level.SEVERE, msg);
        runMyLog();
    }

    public void log(LogRecord record)
    {
        super.log(Level.SEVERE, record.getMessage());
        runMyLog();
    }

    public void severe(String msg) {
        log(Level.SEVERE, msg);
        runMyLog();
    }

    public void runMyLog(){
        String str = "lll";

        str="fdsafasdfdasfasd";
    }
}


推荐答案

@leonbloy和@ E-Riz指出,根据文档

As @leonbloy and @E-Riz pointed out, per the documentation:


因此,Logger的任何子类(除非与新的LogManager类结合使用)请注意从LogManager类获取Logger实例,并应将操作(如isLoggable和logRecord)委派给该实例。请注意,为了拦截所有日志记录输出,子类只需要覆盖日志(LogRecord)方法。所有其他日志记录方法都被实现为这个日志(LogRecord)方法的调用。

Therefore, any subclasses of Logger (unless they are implemented in conjunction with a new LogManager class) should take care to obtain a Logger instance from the LogManager class and should delegate operations such as "isLoggable" and "log(LogRecord)" to that instance. Note that in order to intercept all logging output, subclasses need only override the log(LogRecord) method. All the other logging methods are implemented as calls on this log(LogRecord) method.

所以这里就是一个必须运行的例子使用 -Djava.util.logging.manager = bad.idea.OdiousLogManager ,所以JVM使用新的LogManager:

So here would be an example that must be run with -Djava.util.logging.manager=bad.idea.OdiousLogManager so the JVM uses the new LogManager:

package bad.idea;

import java.awt.Toolkit;
import java.util.logging.Level;
import java.util.logging.LogManager;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class OdiousLogManager extends LogManager {

    @Override
    public synchronized Logger getLogger(String name) {
        Logger l = super.getLogger(name);
        if (l == null) {
            l = new AbhorentLogger(name, null);
            super.addLogger(l);
        }
        return l;
    }


    private static class AbhorentLogger extends Logger {

        AbhorentLogger(String name, String resourceBundleName) {
            super(name, resourceBundleName);
        }

        @Override
        public void log(LogRecord record) {
            super.log(record);
            mightyCatHearMeRoar(record);
        }

        private void mightyCatHearMeRoar(LogRecord record) {
            if (super.isLoggable(record.getLevel())) {
                Toolkit.getDefaultToolkit().beep();
            }
        }
    }


    //...
    private static final Logger logger = Logger.getLogger("godaweful");
    public static void main(String[] args) {
        logger.severe(logger.getClass().getName());
    }
}

如果你只想听事件,那么你应该只需执行自定义处理程序,并避免扩展记录器。

If you only want to listen to events then you should simply implement a custom handler and avoid extending logger.

这篇关于扩展java.util.logging.Logger不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆