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

[36] Override the ThreadContextMap in the provider. #37

Merged
merged 1 commit into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 1 addition & 10 deletions src/main/java/org/jboss/logmanager/log4j/JBossLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
package org.jboss.logmanager.log4j;

import java.util.Collections;
import java.util.Iterator;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Marker;
Expand Down Expand Up @@ -175,14 +174,6 @@ private String getNdc() {
if (contextStack.isEmpty()) {
return "";
}
final StringBuilder result = new StringBuilder();
final Iterator<String> iter = contextStack.iterator();
while (iter.hasNext()) {
result.append(iter.next());
if (iter.hasNext()) {
result.append('.');
}
}
return result.toString();
return String.join(".", contextStack);
}
}
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;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 java.util.Map;

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

/**
* 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() {
return MDC.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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,55 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.ThreadContext;
import org.jboss.logmanager.MDC;
import org.jboss.logmanager.NDC;
import org.jboss.logmanager.formatters.PatternFormatter;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import io.smallrye.common.constraint.Assert;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class ThreadContextMapTestCase extends AbstractTestCase {

@BeforeEach
public void clear() {
ThreadContext.clearAll();
MDC.clear();
NDC.clear();
}

@Test
public void clearThreadContext() {
final String key = "test.clear.key";
ThreadContext.put(key, "test clear value");

Assertions.assertEquals("test clear value", ThreadContext.get(key));
Assertions.assertEquals("test clear value", MDC.get(key));

ThreadContext.clearMap();
Assert.assertTrue(ThreadContext.isEmpty());
Assert.assertTrue(MDC.isEmpty());
}

@Test
public void clearMdc() {
final String key = "test.clear.key";
ThreadContext.put(key, "test clear value");

Assertions.assertEquals("test clear value", ThreadContext.get(key));
Assertions.assertEquals("test clear value", MDC.get(key));

MDC.clear();
Assert.assertTrue(ThreadContext.isEmpty());
Assert.assertTrue(MDC.isEmpty());
}

@Test
public void testPut() {
public void putThreadContext() {
final String key = "test.key";
final TestQueueHandler handler = new TestQueueHandler(new PatternFormatter("%X{" + key + "}"));
org.jboss.logmanager.Logger.getLogger("").addHandler(handler);
Expand All @@ -51,7 +89,26 @@ public void testPut() {
}

@Test
public void testPush() {
public void putMdc() {
final String key = "test.key";
final TestQueueHandler handler = new TestQueueHandler(new PatternFormatter("%X{" + key + "}"));
org.jboss.logmanager.Logger.getLogger("").addHandler(handler);
MDC.put(key, "test value");

final Logger logger = LogManager.getLogger();

logger.info("Test message");

Assertions.assertEquals("test value", handler.pollFormatted());

ThreadContext.remove(key);

logger.info("Test message");
Assertions.assertEquals("", handler.pollFormatted());
}

@Test
public void pushThreadContext() {
final TestQueueHandler handler = new TestQueueHandler(new PatternFormatter("%x"));
org.jboss.logmanager.Logger.getLogger("").addHandler(handler);

Expand All @@ -71,4 +128,32 @@ public void testPush() {
Assertions.assertEquals("value-1.value-2", handler.pollFormatted());
}

@Test
public void removeThreadContext() {
final String key = "test.clear.key";
ThreadContext.put(key, "test clear value");

Assertions.assertEquals("test clear value", ThreadContext.get(key));
Assertions.assertEquals("test clear value", MDC.get(key));

ThreadContext.remove(key);

Assertions.assertNull(ThreadContext.get(key));
Assertions.assertNull(MDC.get(key));
}

@Test
public void removeMdc() {
final String key = "test.clear.key";
MDC.put(key, "test clear value");

Assertions.assertEquals("test clear value", ThreadContext.get(key));
Assertions.assertEquals("test clear value", MDC.get(key));

MDC.remove(key);

Assertions.assertNull(ThreadContext.get(key));
Assertions.assertNull(MDC.get(key));
}

}