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

  • Home
  • SEARCH
  • 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 7130005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T11:18:14+00:00 2026-05-28T11:18:14+00:00

In my application I have written my own Logging utility using Java.Util.Logging import java.io.IOException;

  • 0

In my application I have written my own Logging utility using Java.Util.Logging

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.SimpleFormatter;



public class Logger {

    public final static String PROPERTIES_FILE = "Logger.properties";
    private static java.util.logging.Logger logger = null;

    private static void initialize() {
        try {
            logger = java.util.logging.Logger.getLogger(Logger.class.getName());
            FileHandler fh = new FileHandler("D:\\MyLogFile.log", true);
            logger.addHandler(fh);
            logger.setLevel(Level.ALL);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
            logger.log(Level.INFO, "Test Message Logged");

        }
        catch (IOException e) {
          System.out.println("Warning: Unable to read properties file " +
                             PROPERTIES_FILE );
        }   
      }

    public static synchronized java.util.logging.Logger getLogger(String name)
    {
        if(logger==null)
        {
        Logger.initialize();
        }
        logger.getLogger(name);
        return logger;
    }


}

Do I need to use Synchronization for getLogger method? Please give your comments. (This code is running in Multi-threaded environment)

  • 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-05-28T11:18:14+00:00Added an answer on May 28, 2026 at 11:18 am

    I agree with other commenters that lazy initialization doesn’t seem necessary here. The simplest way to initialize the logger variable is in a static initializer, which is guaranteed to only execute once at class loading time:

    public class Logger {
    
        public final static String PROPERTIES_FILE = "Logger.properties";
        private static java.util.logging.Logger logger = null;
    
        private static void initialize() {
            try {
                logger = java.util.logging.Logger.getLogger(Logger.class.getName());
                FileHandler fh = new FileHandler("D:\\MyLogFile.log", true);
                logger.addHandler(fh);
                logger.setLevel(Level.ALL);
                SimpleFormatter formatter = new SimpleFormatter();
                fh.setFormatter(formatter);
                logger.log(Level.INFO, "Test Message Logged");
    
            }
            catch (IOException e) {
              System.out.println("Warning: Unable to read properties file " +
                                 PROPERTIES_FILE );
            }   
        }
    
        static {
            initialize();
        }
    
        public static java.util.logging.Logger getLogger(String name)
        {
            logger.getLogger(name);
            return logger;
        }
    
    
    }
    

    However, You can avoid most synchronization with double-checked locking.

    public class Logger {
    
        // note: volatile is required
        private volatile static java.util.logging.Logger logger = null;
    
        //... 
    
        public static java.util.logging.Logger getLogger(String name)
        {
            if(logger==null)
            {
               synchronized(Logger.class) 
               {
                  if(logger == null)
                    Logger.initialize();
                  }
               }
            }
            logger.getLogger(name);
            return logger;
        }
    }
    

    In fact, in your case I think you can avoid synchronization altogether if you rewrite your initialize function so that it completely configures the logger in an local variable prior to assigning it to the (volatile) class variable:

    private volatile static java.util.logging.Logger logger = null;
    private static void initialize() {
        try {
            Logger logger = java.util.logging.Logger.getLogger(Logger.class.getName());
            FileHandler fh = new FileHandler("D:\\MyLogFile.log", true);
            logger.addHandler(fh);
            logger.setLevel(Level.ALL);
            SimpleFormatter formatter = new SimpleFormatter();
            fh.setFormatter(formatter);
            logger.log(Level.INFO, "Test Message Logged");
    
            Logger.logger = logger;
        }
        catch (IOException e) {
          System.out.println("Warning: Unable to read properties file " +
                             PROPERTIES_FILE );
        }   
    
        public static java.util.logging.Logger getLogger(String name)
        {
            if(logger==null)
            {
            Logger.initialize();
            }
            logger.getLogger(name);
            return logger;
        }
    }
    

    This has a potential to have initialize() execute several times, but I don’t think you care, so long that every getLogger invocation will have a valid logger instance, even if that instance varies.

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

Sidebar

Related Questions

I have a ASP.NET application that we've written our own logging module for. My
I have written an application in Java and succesfully compiled it using gcj .
We have a standard j2ee application written using jboss richfaces 3.3.x. we have several
I have written an application (using Delphi 2009) that allows a user to select
I have written a TCP server implementation using which I created an application which
I have just written my own logging framework (very lightweight, no need for a
I have an application written in QuickBasic (a game). I own the rights to
I have written an application (intended only for my own use) that monitors Core
I have an game application I have written for Windows Mobile and I want
Good Morning, I'm working on an ASP.NET 3.5 webforms application and have written the

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.