Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396225
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:29:05+00:00 2026-06-09T20:29:05+00:00

I hope this question has a simple answer. I am trying to add a

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:29:06+00:00Added an answer on June 9, 2026 at 8:29 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hopefully this question has a simple answer i'm overlooking! I have an IIS webserver
I hope this question has an obvious answer to anyone who knows his way
Python beginner here, so I apologize if this question has a simple answer. (I
I hope this question is not 'controversial' - I'm just basically asking - has
This question has been asked before somewhat, but I hope mine differs. My situation
Unfortunately, the answer to this question isn't quite as simple as it sounds... but
I'm quite new to Caliburn.Micro, so I guess this has a simple answer (or
I hope this is a simple enough question for any SQL people out there...
Update: I can't delete this question, because the answer has been upvoted, yet it
I hope this is a simple question. If I have a UINavigationController and I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.