Note that there are some explanatory texts on larger screens.

plurals
  1. POJava Logger Console Stream Duplicate Output
    primarykey
    data
    text
    <p>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.</p> <p>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.</p> <p>Ill post my code below, not sure at all what I am doing wrong.<br> Any help will be greatly appreciated.</p> <pre><code>// 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); </code></pre> <p><strong>EDIT:</strong> Answer</p> <p>Just wanted to post my final code here to help anyone with a similar problem.</p> <pre><code>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); </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload