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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:41:00+00:00 2026-05-28T07:41:00+00:00

Java Code : DOMConfigurator.configureAndWatch(log4jConfigFile.getAbsolutePath(),100000L); I need to add some appenders programmaticaly whenever Thread created

  • 0

Java Code :
DOMConfigurator.configureAndWatch(log4jConfigFile.getAbsolutePath(),100000L);

I need to add some appenders programmaticaly whenever Thread created by configureAndWatch method reloads the log4j.xml.

Actually i am setting some appenders programmatically but its get reset when Thread reloads the log4j.xml so i need to again add the appenders programmatically.For this i need a callback when ever Thread reloads the log4j.xml

Any Idea ?? How do i do this….

  • 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-28T07:41:00+00:00Added an answer on May 28, 2026 at 7:41 am

    Unfortunately Log4j does not provide a way to register a callback on configuration load. It just creates a “watchdog” thread to monitor the file and reload configuration on file changes.

    The only way I see it done is by spawning your own thread that reloads configuration and also adds the extra appenders.

    Instead of :

    DOMConfigurator.configureAndWatch(log4jConfigFile.getAbsolutePath(), 100000L);
    

    you do this:

    String configFilename = log4jConfigFile.getAbsolutePath();
    DOMConfigurator.configure(configFilename);
    
    // set your appenders programmatically here
    // ...
    
    MyXMLWatchdog watchDogThread = new MyXMLWatchdog(configFilename);
    watchDogThread.setDelay(100000L);
    watchDogThread.start();
    

    You configured Log4j, added the appenders and started the monitoring thread. In the thread you reload configuration on file change:

    import org.apache.log4j.LogManager;
    import org.apache.log4j.helpers.FileWatchdog;
    import org.apache.log4j.xml.DOMConfigurator;
    
    public class MyXMLWatchdog extends FileWatchdog {
        public MyXMLWatchdog(String filename) {
            super(filename);
        }
    
        public void doOnChange() {
            new DOMConfigurator().doConfigure(this.filename, LogManager.getLoggerRepository());
            // set your appenders programmatically here
            // ...
        }
    }
    

    Have a look at the source code of these classes for further details: DOMConfigurator (scroll to the bottom) and FileWatchdog.

    My suggestion though will be to just have everything configured in the log4j.xml file and not resort to workarounds.

    P.S. You don’t mention what type of Java application you have, but be aware that configureAndWatch comes with a warning in Java EE applications as the thread does not stop on application shutdowns; it stops only on JVM shutdown.

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

Sidebar

Related Questions

I have some java code that work on the command line, and need to
I have some Java code that uses curly braces in two ways // Curly
I am converting some Java code to C#. This code is using getGlyphOutline from
Java Code need to write an equivalent in objectiveC. CAn you please help me
I see some java code does a cleaner way to exit as shown in
I came across some java code for doing a prefix search on a JList.
I've written some Java code, as shown below, but it isn't behaving as I
I'm debugging some Java code that uses Apache POI to pull data out of
Following java code returns hash code of a string. String uri = Some URI
I have some java code as follows: try { String outString =java -jar C:\\ami\\bin\\ImmediateSubmit.jar

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.