-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[36] Override the ThreadContextMap in the provider.
resolves #36 Signed-off-by: James R. Perkins <jperkins@redhat.com>
- Loading branch information
Showing
6 changed files
with
331 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
src/main/java/org/jboss/logmanager/log4j/Log4jMdcProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
82
src/main/java/org/jboss/logmanager/log4j/Log4jNdcProvider.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/main/resources/META-INF/services/org.jboss.logmanager.MDCProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
20 changes: 20 additions & 0 deletions
20
src/main/resources/META-INF/services/org.jboss.logmanager.NDCProvider
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.