How can I disable logging from certain classes when using a common logger?

点点圈 提交于 2020-02-08 02:31:29

问题


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;

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.


回答1:


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.




回答2:


We can mention the below code for logging off from particular Java Classes as:

Logger.getLogger("cs.fid.tm.reportingService.config.test1").setLevel(Level.OFF);



回答3:


If all else fails, you could always just walk the call stack and see where your function was called from. Perhaps not the most efficient solution, but it should work in your case.



来源:https://stackoverflow.com/questions/33538174/how-can-i-disable-logging-from-certain-classes-when-using-a-common-logger

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