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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T02:26:18+00:00 2026-05-24T02:26:18+00:00

I am writing a log4j appender that sends the logs to a server via

  • 0

I am writing a log4j appender that sends the logs to a server via http. I wanted to use HttpClient from apache-commons to keep my code nice and simple.

The problem now is that HttpClient and Co. uses log4j itself. Usually a good thing but when calling them from within a log4j appender implementation, circular references or endless loops are introduced, ultimately leading to an OutOfMemoryException.

Of course I can write what I want without any thrid-party libraries but I just wondered if there is known solution to this kind of problem?

  • 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-24T02:26:19+00:00Added an answer on May 24, 2026 at 2:26 am

    This is a great question! It’s a shame that log4j doesn’t try to help you here. But we can be devious and use log4j to fix log4j! The important concept is a Diagnostic Context:

    private static final String IN_APPEND_KEY = MyAppender.class.getName() + ".inAppend";
    public void append(LoggingEvent e) {
        if (e.getMDC(IN_APPEND_KEY) != null) return;
        MDC.put(IN_APPEND_KEY, this);
        try {
            <your code here>
        } finally {
            MDC.remove(IN_APPEND_KEY);
        }
    }
    

    You basically want to set a flag that will “travel” with the execution flow of your appender. This is exactly what a diagnostic context does, in theory at least. It is thread-local and inherited across threads. Carefully-written code will even preserve the context across other boundaries, such as a message queue. How cool would that be: your appender puts the log message on an HTTP request queue, the queue saves the diagnostic context and restores it when request gets run, the HTTP library logs an error, and your appender still detects the loop! It’s pretty much the best thing you could ask for.

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

Sidebar

Related Questions

Writing a client application that sends images to a server via a webservice. As
I am writing Perl script. In that I have to read log4j.xml file from
I am writing a library. The library can be used by applications that use
I'm writing a custom log4j appender, and I want to rely on another configured
I'm writing a database log4j appender in Play. On its append() method, it creates
I have following information in log4j.properties file.. log4j.appender.file = org.apache.log4j.DailyRollingFileAppender log4j.appender.file.File = C:\\app\\file.log log4j.appender.file.Threshold
We're writing a custom log4j appender for our application. The appender should log its
Writing an HTTP simple server on top of Net node.js module, not using HTTP
Writing documentation in html requires some code examples. What to do with characters that
I writing a JSP program that needs to react on an existing program. It

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.