Skip to content

Commit

Permalink
[PAXLOGGING-222] Update to log4j 2.8.1
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Mar 31, 2017
1 parent eb87a98 commit 0ae483e
Show file tree
Hide file tree
Showing 102 changed files with 12,553 additions and 1,609 deletions.
9 changes: 5 additions & 4 deletions pax-logging-api/osgi.bnd
Expand Up @@ -26,10 +26,10 @@ Export-Package: \
org.apache.log4j; version=1.2.15; provider=paxlogging, \
org.apache.log4j.spi; version=1.2.15; provider=paxlogging, \
org.apache.log4j.xml; version=1.2.15; provider=paxlogging, \
org.apache.logging.log4j; version=2.4.1; provider=paxlogging, \
org.apache.logging.log4j.message; version=2.4.1; provider=paxlogging, \
org.apache.logging.log4j.spi; version=2.4.1; provider=paxlogging, \
org.apache.logging.log4j.util; version=2.4.1; provider=paxlogging, \
org.apache.logging.log4j; version=2.8.1; provider=paxlogging, \
org.apache.logging.log4j.message; version=2.8.1; provider=paxlogging, \
org.apache.logging.log4j.spi; version=2.8.1; provider=paxlogging, \
org.apache.logging.log4j.util; version=2.8.1; provider=paxlogging, \
org.knopflerfish.service.log; version=1.1.0; provider=paxlogging, \
org.ops4j.pax.logging; version=${pom.version}; provider=paxlogging, \
org.ops4j.pax.logging.avalon; version=${pom.version}; provider=paxlogging, \
Expand Down Expand Up @@ -58,6 +58,7 @@ Export-Package: \

Import-Package: \
org.osgi.framework; version="[1.0.0,2.0.0)", \
org.osgi.framework.wiring; version="[1.0.0,2.0.0)", \
org.osgi.util.tracker; version="[1.0.0,2.0.0)", \
org.ops4j.pax.logging; version="[0.9.5,2.0.0)", \
org.ops4j.pax.logging.avalon; version="[0.9.5,2.0.0)", \
Expand Down
@@ -0,0 +1,227 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.apache.logging.log4j;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
* Adds entries to the {@link ThreadContext stack or map} and them removes them when the object is closed, e.g. as part
* of a try-with-resources. User code can now look like this:
* <pre>
* try (final CloseableThreadContext.Instance ignored = CloseableThreadContext.put(key1, value1).put(key2, value2)) {
* callSomeMethodThatLogsALot();
*
* // Entries for key1 and key2 are automatically removed from the ThreadContext map when done.
* }
* </pre>
*
* @since 2.6
*/
public class CloseableThreadContext {

private CloseableThreadContext() {
}

/**
* Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when
* the instance is closed.
*
* @param message The new diagnostic context information.
* @return a new instance that will back out the changes when closed.
*/
public static CloseableThreadContext.Instance push(final String message) {
return new CloseableThreadContext.Instance().push(message);
}

/**
* Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when
* the instance is closed.
*
* @param message The new diagnostic context information.
* @param args Parameters for the message.
* @return a new instance that will back out the changes when closed.
*/
public static CloseableThreadContext.Instance push(final String message, final Object... args) {
return new CloseableThreadContext.Instance().push(message, args);
}

/**
* Populates the Thread Context Map with the supplied key/value pair. Any existing key in the
* {@link ThreadContext} will be replaced with the supplied value, and restored back to their original value when
* the instance is closed.
*
* @param key The key to be added
* @param value The value to be added
* @return a new instance that will back out the changes when closed.
*/
public static CloseableThreadContext.Instance put(final String key, final String value) {
return new CloseableThreadContext.Instance().put(key, value);
}

/**
* Populates the Thread Context Stack with the supplied stack. The information will be popped off when
* the instance is closed.
*
* @param messages The list of messages to be added
* @return a new instance that will back out the changes when closed.
* @since 2.8
*/
public static CloseableThreadContext.Instance pushAll(final List<String> messages) {
return new CloseableThreadContext.Instance().pushAll(messages);
}

/**
* Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the
* {@link ThreadContext} will be replaced with the supplied values, and restored back to their original value when
* the instance is closed.
*
* @param values The map of key/value pairs to be added
* @return a new instance that will back out the changes when closed.
* @since 2.8
*/
public static CloseableThreadContext.Instance putAll(final Map<String, String> values) {
return new CloseableThreadContext.Instance().putAll(values);
}

public static class Instance implements AutoCloseable {

private int pushCount = 0;
private final Map<String, String> originalValues = new HashMap<>();

private Instance() {
}

/**
* Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when
* the instance is closed.
*
* @param message The new diagnostic context information.
* @return the instance that will back out the changes when closed.
*/
public Instance push(final String message) {
ThreadContext.push(message);
pushCount++;
return this;
}

/**
* Pushes new diagnostic context information on to the Thread Context Stack. The information will be popped off when
* the instance is closed.
*
* @param message The new diagnostic context information.
* @param args Parameters for the message.
* @return the instance that will back out the changes when closed.
*/
public Instance push(final String message, final Object[] args) {
ThreadContext.push(message, args);
pushCount++;
return this;
}

/**
* Populates the Thread Context Map with the supplied key/value pair. Any existing key in the
* {@link ThreadContext} will be replaced with the supplied value, and restored back to their original value when
* the instance is closed.
*
* @param key The key to be added
* @param value The value to be added
* @return a new instance that will back out the changes when closed.
*/
public Instance put(final String key, final String value) {
// If there are no existing values, a null will be stored as an old value
if (!originalValues.containsKey(key)) {
originalValues.put(key, ThreadContext.get(key));
}
ThreadContext.put(key, value);
return this;
}

/**
* Populates the Thread Context Map with the supplied key/value pairs. Any existing keys in the
* {@link ThreadContext} will be replaced with the supplied values, and restored back to their original value when
* the instance is closed.
*
* @param values The map of key/value pairs to be added
* @return a new instance that will back out the changes when closed.
* @since 2.8
*/
public Instance putAll(final Map<String, String> values) {
final Map<String, String> currentValues = ThreadContext.getContext();
ThreadContext.putAll(values);
for (final String key : values.keySet()) {
if (!originalValues.containsKey(key)) {
originalValues.put(key, currentValues.get(key));
}
}
return this;
}

/**
* Populates the Thread Context Stack with the supplied stack. The information will be popped off when
* the instance is closed.
*
* @param messages The list of messages to be added
* @return a new instance that will back out the changes when closed.
* @since 2.8
*/
public Instance pushAll(final List<String> messages) {
for (final String message : messages) {
push(message);
}
return this;
}

/**
* Removes the values from the {@link ThreadContext}.
* <p>
* Values pushed to the {@link ThreadContext} <em>stack</em> will be popped off.
* </p>
* <p>
* Values put on the {@link ThreadContext} <em>map</em> will be removed, or restored to their original values it they already existed.
* </p>
*/
@Override
public void close() {
closeStack();
closeMap();
}

private void closeMap() {
for (final Iterator<Map.Entry<String, String>> it = originalValues.entrySet().iterator(); it.hasNext(); ) {
final Map.Entry<String, String> entry = it.next();
final String key = entry.getKey();
final String originalValue = entry.getValue();
if (null == originalValue) {
ThreadContext.remove(key);
} else {
ThreadContext.put(key, originalValue);
}
it.remove();
}
}

private void closeStack() {
for (int i = 0; i < pushCount; i++) {
ThreadContext.pop();
}
pushCount = 0;
}
}
}
23 changes: 17 additions & 6 deletions pax-logging-api/src/main/java/org/apache/logging/log4j/Level.java
Expand Up @@ -24,6 +24,7 @@
import java.util.concurrent.ConcurrentMap;

import org.apache.logging.log4j.spi.StandardLevel;
import org.apache.logging.log4j.util.Strings;

/**
* Levels used for identifying the severity of an event. Levels are organized from most specific to least:
Expand Down Expand Up @@ -109,8 +110,8 @@ public final class Level implements Comparable<Level>, Serializable {
private final StandardLevel standardLevel;

private Level(final String name, final int intLevel) {
if (name == null || name.isEmpty()) {
throw new IllegalArgumentException("Illegal null Level constant");
if (Strings.isEmpty(name)) {
throw new IllegalArgumentException("Illegal null or empty Level name.");
}
if (intLevel < 0) {
throw new IllegalArgumentException("Illegal Level int less than zero.");
Expand Down Expand Up @@ -157,6 +158,11 @@ public boolean isInRange(final Level minLevel, final Level maxLevel) {
/**
* Compares this level against the level passed as an argument and returns true if this level is the same or is less
* specific.
* <p>
* Concretely, {@link #TRACE} is less specific than {@link #DEBUG}, which is less specific than {@link #INFO},
* etc., until {@link #FATAL}, and finally {@link #OFF}, which is the most specific standard level.
* The least specific level is {@link #ALL}.
* </p>
*
* @param level The level to test.
* @return True if this level Level is less specific or the same as the given Level.
Expand All @@ -168,6 +174,11 @@ public boolean isLessSpecificThan(final Level level) {
/**
* Compares this level against the level passed as an argument and returns true if this level is the same or is more
* specific.
* <p>
* Concretely, {@link #FATAL} is more specific than {@link #ERROR}, which is more specific than {@link #WARN},
* etc., until {@link #TRACE}, and finally {@link #ALL}, which is the least specific standard level.
* The most specific level is {@link #OFF}.
* </p>
*
* @param level The level to test.
* @return True if this level Level is more specific or the same as the given Level.
Expand Down Expand Up @@ -219,7 +230,7 @@ public String toString() {

/**
* Retrieves an existing Level or creates on if it didn't previously exist.
*
*
* @param name The name of the level.
* @param intValue The integer value for the Level. If the level was previously created this value is ignored.
* @return The Level.
Expand All @@ -240,7 +251,7 @@ public static Level forName(final String name, final int intValue) {

/**
* Return the Level associated with the name or null if the Level cannot be found.
*
*
* @param name The name of the Level.
* @return The Level or null.
*/
Expand Down Expand Up @@ -277,7 +288,7 @@ public static Level toLevel(final String name, final Level defaultLevel) {

/**
* Return an array of all the Levels that have been registered.
*
*
* @return An array of Levels.
*/
public static Level[] values() {
Expand All @@ -287,7 +298,7 @@ public static Level[] values() {

/**
* Return the Level associated with the name.
*
*
* @param name The name of the Level to return.
* @return The Level.
* @throws java.lang.NullPointerException if the Level name is {@code null}.
Expand Down

0 comments on commit 0ae483e

Please sign in to comment.