使用通用记录器时,如何禁用某些类的记录? [英] How can I disable logging from certain classes when using a common logger?

查看:83
本文介绍了使用通用记录器时,如何禁用某些类的记录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经按照以下方式在许多子类引用的根类中创建了一个通用记录器.

I have created a common logger in the following manner in a root class that is referenced by many sub classes.

public static final Logger LOGGER = Logger.getLogger(RootClass.class.getName()); 

然后,我将以以下方式在子类中访问该记录器.

Then I am accessing that logger in child classes in the following manner.

private static final Logger LOGGER = RootClass.LOGGER;

我有一个主类,可以接收用户的输入,它调用RootClass中的方法来配置记录器.

I have a main classes which receives inputs from users and it calls a method in the RootClass to configure the logger.

logStatus = messageBus.configureLogPath(logPath,logLevel);

下面是上述方法的实现.

Below is the implementation of the above method.

public Boolean configureLogPath(String logPath,String level) {
    Boolean result=false;
    switch(level) {
        case "info" :
            LOGGER.setLevel(Level.INFO);
            break;
        case "severe":
            LOGGER.setLevel(Level.SEVERE);
            break;
        case "debug":
            LOGGER.setLevel(Level.CONFIG);
            break;
        case "off" :
            LOGGER.setLevel(Level.OFF);
            break;
        default : 
            LOGGER.setLevel(Level.SEVERE);
    }
     try {
        simpleFormatter = new SimpleFormatter();
        logFileHandler = new FileHandler(logPath);
        logFileHandler.setFormatter(simpleFormatter);


        LOGGER.addHandler(logFileHandler);
        result =true;
    } catch (SecurityException e1) {
        result= false;
        LOGGER.log(Level.SEVERE, "Security exception when reading log file" + e1);
    } catch (IOException e1) {
        result = false;
        LOGGER.log(Level.SEVERE, "IO Exception when reading log file" + e1);
    }
    return result;
}

我是否可以禁用某些选定子类的日志记录?我有一个要求,用户应该能够设置应为其创建日志的某些子类.但是,由于我在所有子类中都使用通用记录器,因此我不确定如何实现此目的.请指教.

Is it possible for me to disable logging from certain selected child classes? I have a requirement where the user should be able to set certain child classes for which the logs should be created. But since I am using a common logger in all the child classes I am not sure how to achieve this. Please advice.

推荐答案

要满足您的要求,您必须创建自定义

To meet your requirements you have to create a custom log filter that checks the source class name and install it on the root logger.

public class SourceFilter implements Filter {

    @Override
    public boolean isLoggable(LogRecord record) {
        //Source class can return null so handle it.
        String cn = String.valueOf(record.getSourceClassName());
        return !cn.startsWith("foo.bar.Child1")
                && !cn.startsWith("foo.bar.Child2")
                && !cn.startsWith("foo.bar.Child3");
    }
}

此外,级别类具有

Also, the level class has a parse method you can use to get rid of that switch statement.

这篇关于使用通用记录器时,如何禁用某些类的记录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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