Circular dependency on logging in to appender 4jj

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

Now the problem is that HttpClient and Co. use log4j. This is usually good, but when you call them from the log4j appender implementation, you will get circular links or infinite loops, which ultimately leads to an OutOfMemoryException.

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

+3
source share
2 answers

! , log4j . log4j log4j! :

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);
    }
}

, "" . , , , . - . , . : appender HTTP-, , HTTP , appender ! , .

+3

Apache HttpClient Appender? Appender, - .

0

All Articles