Java Logger Console Stream Duplicate Output

六眼飞鱼酱① 提交于 2020-01-23 06:36:44

问题


I hope this question has a simple answer. I am trying to add a logger to my Java application. I was able to format the log file perfectly, but I ran into an issue when trying to add a ConsoleHandler to the logger to format the console output.

Once I added the ConsoleHandler, every log message is not printed out three times, twice with the proper formatting I want, and then once with the original format that I do not want.

Ill post my code below, not sure at all what I am doing wrong.
Any help will be greatly appreciated.

// remove and handlers that will be replaced
Handler[] handlers = logger.getHandlers();
for(Handler handler : handlers)
{
    if(handler.getClass() == ConsoleHandler.class || handler.getClass() == FileHandler.class)
        logger.removeHandler(handler);
}

// file handler
FileHandler fh = new FileHandler(file.toString());
fh.setFormatter(new MS2Formatter());
logger.addHandler(fh);

// console handler
ConsoleHandler ch = new ConsoleHandler();
ch.setFormatter(new MS2Formatter());
logger.addHandler(ch);

logger.setLevel(Level.INFO);

EDIT: Answer

Just wanted to post my final code here to help anyone with a similar problem.

logger = Logger.getLogger("My Logger");     
logger.setUseParentHandlers(false);

// remove and handlers that will be replaced
Handler[] handlers = logger.getHandlers();
for(Handler handler : handlers)
{
        if(handler.getClass() == ConsoleHandler.class)
            logger.removeHandler(handler);
}

// setup the file
File file = new File(location + "/" + fileName);

// file handler
FileHandler fh = new FileHandler(file.toString(), true);
fh.setFormatter(new MS2Formatter());
logger.addHandler(fh);

// console handler
ConsoleHandler ch = new ConsoleHandler();
ch.setFormatter(new MS2Formatter());
logger.addHandler(ch);

// remove and handlers that will be replaced
logger.setLevel(Level.INFO);

回答1:


Exactly how many handlers do you need? addHandler() adds the handler you've created to the handlers associated with that logger. So you have the default handler, and the 2 you've added in your code - FileHandler and ConsoleHandler.

You can get the current set of handlers using the getHandlers() method and use removeHandler() to remove the handlers you don't need.

EDIT

In your case, chances are that the parent handlers are being used. So even though you think you are removing the handlers, if you actually debug the code, you'll see that during execution the in the for loop you never really remove a handler at all (or at least not a ConsoleHandler).

To prevent parent handlers from being used, use this statement.

logger.setUseParentHandlers(false);



回答2:


Before adding ConsoleHandlers, seek to remove old console handlers. Odds are you are now logging to the console in a manner that didn't alter the original console handler, and it is also in the chain to echo out the output of the newly added console handler, hence 3 logs for each message.



来源:https://stackoverflow.com/questions/12013393/java-logger-console-stream-duplicate-output

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