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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:57:54+00:00 2026-06-15T21:57:54+00:00

I have a specialized logger class which uses the java.util.logging.Logger class. I want to

  • 0

I have a specialized logger class which uses the java.util.logging.Logger class. I want to be able to use this logger in the shutdown hook of another class. However, it doesn’t seem to log at shutdown. From what I’ve read, there may already be a shutdown hook activated for the logger itself which is causing the issue.

How can I get this to work? Ideally, I would like it to be seen in the log file that I did in fact execute the shutdown hook when the process terminated.

  • 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-15T21:57:56+00:00Added an answer on June 15, 2026 at 9:57 pm

    Again looking at the source, the solution appears to be to define a system property java.util.logging.manager which is a subclass of LogManager which overrides the reset(); method so the Loggers continue to work on shutdown.

    import java.util.logging.LogManager;
    import java.util.logging.Logger;
    
    public class Main {
        static {
            // must be called before any Logger method is used.
            System.setProperty("java.util.logging.manager", MyLogManager.class.getName());
        }
    
        public static class MyLogManager extends LogManager {
            static MyLogManager instance;
            public MyLogManager() { instance = this; }
            @Override public void reset() { /* don't reset yet. */ }
            private void reset0() { super.reset(); }
            public static void resetFinally() { instance.reset0(); }
        }
    
        public static void main(String... args) {
            Logger logger1 = Logger.getLogger("Main1");
            logger1.info("Before shutdown");
            Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        Logger logger2 = Logger.getLogger("Main2");
                        logger2.info("Shutting down 2");
    
                    } finally {
                        MyLogManager.resetFinally();
                    }
                }
            }));
        }
    }
    

    prints

    Dec 11, 2012 5:56:55 PM Main main
    INFO: Before shutdown
    Dec 11, 2012 5:56:55 PM Main$1 run
    INFO: Shutting down 2
    

    From this code for LogManager, you can see see there is a shutdown hook which dismantles the handlers and closes them. Logger only works in shutdown if it hasn’t been used before so this code is not run.

    // This private class is used as a shutdown hook.
    // It does a "reset" to close all open handlers.
    private class Cleaner extends Thread {
    
        private Cleaner() {
            /* Set context class loader to null in order to avoid
             * keeping a strong reference to an application classloader.
             */
            this.setContextClassLoader(null);
        }
    
        public void run() {
            // This is to ensure the LogManager.<clinit> is completed
            // before synchronized block. Otherwise deadlocks are possible.
            LogManager mgr = manager;
    
            // If the global handlers haven't been initialized yet, we
            // don't want to initialize them just so we can close them!
            synchronized (LogManager.this) {
                // Note that death is imminent.
                deathImminent = true;
                initializedGlobalHandlers = true;
            }
    
            // Do a reset to close all active handlers.
            reset();
        }
    }
    
    
    /**
     * Protected constructor.  This is protected so that container applications
     * (such as J2EE containers) can subclass the object.  It is non-public as
     * it is intended that there only be one LogManager object, whose value is
     * retrieved by calling Logmanager.getLogManager.
     */
    protected LogManager() {
        // Add a shutdown hook to close the global handlers.
        try {
            Runtime.getRuntime().addShutdownHook(new Cleaner());
        } catch (IllegalStateException e) {
            // If the VM is already shutting down,
            // We do not need to register shutdownHook.
        }
    }
    

    From my own testing

    Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                Logger logger2 = Logger.getLogger("Main2");
                logger2.info("Shutting down 2");
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }));
    

    prints

    Dec 11, 2012 5:40:15 PM Main$1 run
    INFO: Shutting down 2
    

    but if you add

    Logger logger1 = Logger.getLogger("Main1");
    

    outside this block you get nothing.

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

Sidebar

Related Questions

I have a specialized class, BusinessObjectList, with this declaration: public class BusinessObjectList<T> : List<BusinessObject>
I have a specialized object class that sends messages to its components so that
I have a class which is serialized to and from an XML file when
I have a class which gets serialized and deserialized in a session, and I
I have a specialized frame class and a specialized panel class. I have a
I have a template class which looks like the following: template <template <class TypeT>
I have a C++ base class which declares a virtual method with two different
I have a specialized list that holds items of type IThing : public class
I have a web method, which is called from jquery's ajax method, like this:
For a specialized purpose with Aweber regarding a newsletter subscription, I have a page

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.