Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compatibility old log4j #34

Open
duylong opened this issue Jan 28, 2014 · 2 comments
Open

Compatibility old log4j #34

duylong opened this issue Jan 28, 2014 · 2 comments

Comments

@duylong
Copy link

duylong commented Jan 28, 2014

Hi,

Many applications still use a log4j version < 1.2.15. Is it possible to keep compatibility with these versions? because we can not update.

The differences are mainly attributes which change public to private and cause some errors "method not found".

Best regards,

@lusis
Copy link
Contributor

lusis commented Jan 28, 2014

@duylong Let me do some investigation. I might be able to pull something off as long as it means it still works going forward.

@duylong
Copy link
Author

duylong commented Jan 28, 2014

I tried copying the missing methods and I have a working version but I think it is not very clean.

Here are my changes :

+ import java.util.Collections;
+ import java.util.Hashtable;
+ import org.apache.log4j.MDC;
...
+    private Hashtable mdcCopy;
+    private boolean mdcCopyLookupRequired = true;
...
+   /**
+    * Obtain a copy of this thread's MDC prior to serialization or
+    * asynchronous logging.
+    */
+    public
+    void getMDCCopy() {
+       if(this.mdcCopyLookupRequired) {
+            this.mdcCopyLookupRequired = false;
+            // the clone call is required for asynchronous logging.
+            // See also bug #5932.
+            Hashtable t = (Hashtable) MDC.getContext();
+            if(t != null) {
+                this.mdcCopy = (Hashtable) t.clone();
+            }
+        }
+    }

+    /**
+     * Returns the set of properties for the event. The returned set is unmodifiable by the caller.
+     */
+    public Map getProperties() {
+        this.getMDCCopy();
+        Map properties;
+        if (this.mdcCopy == null) {
+            properties = new HashMap();
+        } else {
+            properties = this.mdcCopy;
+        }
+        return Collections.unmodifiableMap(properties);
+    }
...
-        timestamp = loggingEvent.getTimeStamp();
+        timestamp = loggingEvent.timeStamp;
...         
-        mdc = loggingEvent.getProperties();
+        mdc = this.getProperties();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants