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 fa08dbb commit ccf8518
Show file tree
Hide file tree
Showing 6 changed files with 331 additions and 29 deletions.
27 changes: 0 additions & 27 deletions src/main/java/org/jboss/logmanager/log4j/JBossLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,8 @@

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;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.message.Message;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.spi.AbstractLogger;
Expand Down Expand Up @@ -149,13 +145,6 @@ public void logMessage(final String fqcn, final Level level, final Marker marker
if (message.getParameters() != null) {
record.setParameters(message.getParameters());
}
if (ThreadContext.isEmpty()) {
record.setMdc(Collections.emptyMap());
} else {
record.setMdc(ThreadContext.getContext());
}

record.setNdc(getNdc());
record.setThrown(t == null ? message.getThrowable() : t);
logger.log(record);
}
Expand All @@ -169,20 +158,4 @@ public Level getLevel() {
}
return levelTranslator.translateLevel(logger.getEffectiveLevel());
}

private String getNdc() {
final ThreadContext.ContextStack contextStack = ThreadContext.getImmutableStack();
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();
}
}
101 changes: 101 additions & 0 deletions src/main/java/org/jboss/logmanager/log4j/Log4jMdcProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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.LinkedHashMap;
import java.util.Map;

import org.apache.logging.log4j.ThreadContext;
import org.jboss.logmanager.MDCProvider;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class Log4jMdcProvider implements MDCProvider {
@Override
public String get(String key) {
return ThreadContext.get(key);
}

@Override
public Object getObject(String key) {
return ThreadContext.get(key);
}

@Override
public String put(String key, String value) {
try {
return ThreadContext.get(key);
} finally {
ThreadContext.put(key, value);
}
}

@Override
public Object putObject(String key, Object value) {
try {
return ThreadContext.get(key);
} finally {
if (value == null) {
ThreadContext.put(key, null);
} else {
ThreadContext.put(key, String.valueOf(value));
}
}
}

@Override
public String remove(String key) {
try {
return ThreadContext.get(key);
} finally {
ThreadContext.remove(key);
}
}

@Override
public Object removeObject(String key) {
try {
return ThreadContext.get(key);
} finally {
ThreadContext.remove(key);
}
}

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

@Override
public Map<String, Object> copyObject() {
return new LinkedHashMap<>(ThreadContext.getContext());
}

@Override
public boolean isEmpty() {
return ThreadContext.isEmpty();
}

@Override
public void clear() {
ThreadContext.clearMap();
}
}
82 changes: 82 additions & 0 deletions src/main/java/org/jboss/logmanager/log4j/Log4jNdcProvider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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.Iterator;

import org.apache.logging.log4j.ThreadContext;
import org.jboss.logmanager.NDCProvider;

/**
* @author <a href="mailto:jperkins@redhat.com">James R. Perkins</a>
*/
public class Log4jNdcProvider implements NDCProvider {
@Override
public int push(String context) {
ThreadContext.push(context);
return ThreadContext.getDepth();
}

@Override
public String pop() {
return ThreadContext.pop();
}

@Override
public void clear() {
ThreadContext.clearStack();
}

@Override
public void trimTo(int size) {
ThreadContext.trim(size);
}

@Override
public int getDepth() {
return ThreadContext.getDepth();
}

@Override
public String get() {
final ThreadContext.ContextStack contextStack = ThreadContext.getImmutableStack();
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();
}

@Override
public String get(int n) {
final ThreadContext.ContextStack contextStack = ThreadContext.getImmutableStack();
if (contextStack.isEmpty() || (contextStack.size() + 1 > n)) {
return null;
}
return ThreadContext.getImmutableStack().asList().get(n);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# 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.
#

org.jboss.logmanager.log4j.Log4jMdcProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#
# 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.
#

org.jboss.logmanager.log4j.Log4jNdcProvider

0 comments on commit ccf8518

Please sign in to comment.