Skip to content

Commit

Permalink
[36] Override the ThreadContextMap in the provider.
Browse files Browse the repository at this point in the history
resolves #36

Signed-off-by: James R. Perkins <jperkins@redhat.com>
  • Loading branch information
jamezp committed Jun 27, 2023
1 parent 37982a3 commit 9473d68
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/org/jboss/logmanager/log4j/JBossProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.jboss.logmanager.log4j;

import org.apache.logging.log4j.spi.Provider;
import org.apache.logging.log4j.spi.ThreadContextMap;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
Expand All @@ -29,4 +30,14 @@ public class JBossProvider extends Provider {
public JBossProvider() {
super(500, "2.6.0", JBossLoggerContextFactory.class);
}

@Override
public String getThreadContextMap() {
return ThreadContextMDCMap.class.getName();
}

@Override
public Class<? extends ThreadContextMap> loadThreadContextMap() {
return ThreadContextMDCMap.class;
}
}
78 changes: 78 additions & 0 deletions src/main/java/org/jboss/logmanager/log4j/ThreadContextMDCMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* JBoss, Home of Professional Open Source.
*
* Copyright 2023 Red Hat, Inc., and individual contributors
* as indicated by the @author tags.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.jboss.logmanager.log4j;

import org.apache.logging.log4j.spi.ThreadContextMap;
import org.jboss.logmanager.MDC;

import java.util.Map;

/**
* A {@link ThreadContextMap} implementation which delegates to {@link MDC}.
*
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class ThreadContextMDCMap implements ThreadContextMap {
@Override
public void clear() {
MDC.clear();
}

@Override
public boolean containsKey(final String key) {
return MDC.get(key) != null;
}

@Override
public String get(final String key) {
return MDC.get(key);
}

@Override
public Map<String, String> getCopy() {
return MDC.copy();
}

@Override
public Map<String, String> getImmutableMapOrNull() {
final Map<String, String> copy = MDC.copy();
return copy.isEmpty() ? null : Map.copyOf(copy);
}

@Override
public boolean isEmpty() {
// TODO (jrp) this is not fast and should be thought about, should we add this to the log managers MDC?
return MDC.copy().isEmpty();
}

@Override
public void put(final String key, final String value) {
if (value == null) {
MDC.remove(key);
} else {
MDC.put(key, value);
}
}

@Override
public void remove(final String key) {
MDC.remove(key);
}
}

0 comments on commit 9473d68

Please sign in to comment.