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 8833625
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:49:31+00:00 2026-06-14T08:49:31+00:00

I am learning the java.util.Logging API and I cannot understand why it produces redundant

  • 0

I am learning the java.util.Logging API and I cannot understand why it produces redundant output, as the sample code below demonstrates:

Logger logger      = Logger.getLogger("");
Logger logger1     = Logger.getLogger("1");
Logger logger1_2   = Logger.getLogger("1.2");

logger1    .addHandler(new ConsoleHandler());
logger1_2  .addHandler(new ConsoleHandler());

logger     .info("msg:");
logger1    .info("msg: 1");
logger1_2  .info("msg: 1.2");

The output to the console:

14-01-2012 10:32:41 java.util.logging.LogManager$RootLogger log
INFO: msg:
14-01-2012 10:32:42 logging.LoggingExamples main
INFO: msg: 1
14-01-2012 10:32:42 logging.LoggingExamples main
INFO: msg: 1
14-01-2012 10:32:42 logging.LoggingExamples main
INFO: msg: 1.2
14-01-2012 10:32:42 logging.LoggingExamples main
INFO: msg: 1.2
14-01-2012 10:32:42 logging.LoggingExamples main
INFO: msg: 1.2

Why were the log messages passed up to the parent and the same message displayed so many times!? This is totally not efficient, and the log file will increase too fast for duplicated information. Anyone know why? Is this the nature that Logging should have?

Note: The sample code was copied from http://tutorials.jenkov.com/java-logging/logger-hierarchy.html

  • 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-14T08:49:32+00:00Added an answer on June 14, 2026 at 8:49 am

    I think the problem is that you are adding a handler to each of you Log instances when you should only be adding it to the top level instance.

    Here is how I used java.util.Loggin in practice:
    In my main class, I create a global logger from my package name:
    globalLog = Logger.getLogger("thornworks");

    In my main class I then add a handler to this global log (logging messages get captured by Netbeans and end up in my console window anyway, so I wanted to write the messages to a file that I can inspect later):

        Date now = new Date();
        java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("D_hh_mm");
        String logName = "[baseFilename]" + df.format(now);
        File logFolder = new File(someDirectory, "logs");
        try {
            if(!logFolder.exists())
                logFolder.mkdir();
            File logFile = new File(logFolder,logName+".log");
            Handler logFileHandler = new FileHandler(logFile.getAbsolutePath());
            logFileHandler.setFormatter(new SimpleFormatter());
            globalLog.addHandler(logFileHandler);            
        }
        catch(java.io.IOException ioe) { /* Log file could not be created. */  }
        globalLog.setLevel(Level.INFO);
    

    Then other classes that need to do some logging create loggers off of this global logger:

    For instance, fileLogger = GUI.globalLog.getLogger(FileFunction.class.getName());

    The name for this logger is now thornworks.FileFunction. Since it is a child of the parent logger, globalLog, all of the messages generated by fileLogger get passed up to globalLog. Messages generated by fileLog will indicate which class they came from because of the way I obtained the fileLogger.

    Now if I change my mind about how and where I wish to store all of these logging messages, I only need to change a few lines of code in the main class and all of the logging messages in my entire application will get directed to this new location. I can also change the level of the main logger so that less messages get generated, but then increase the level of some class level logger to get more information about that portion of my code.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning Java on my own; and therefore the code below has no function
Below is the partial code. import java.io.*; import java.util.*; public class inputting { public
I'm learning Java and as I know only abstract classes and interfaces cannot be
I am learning Java, theres one thing I do not understand.. in the main
Here is the code: import java.util.*; public class dayName { public static void main(String[]
I am learning Java in Eclipse IDE. I cannot figure out how to get
I'm learning Java development and for a homework assignment I've been instructed to code
I am learning java. I wrote the following code but I am getting this
I just started learning code (Java specifically), and i'm testing out a password system
I'm learning databases using Java, and here is the simple code for that: import

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.