Skip to content

Commit cb905f3

Browse files
committed
LOG4J2-913 - Support reconfiguration from HTTP. Support Docker. Support Spring Cloud Config
1 parent efa64bf commit cb905f3

File tree

119 files changed

+6063
-301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

119 files changed

+6063
-301
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache license, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the license for the specific language governing permissions and
15+
* limitations under the license.
16+
*/
17+
package org.apache.logging.log4j.util;
18+
19+
import java.util.Base64;
20+
21+
22+
/**
23+
* C
24+
*/
25+
public class Base64Util {
26+
27+
private static final Base64.Encoder encoder = Base64.getEncoder();
28+
29+
public static String encode(String str) {
30+
return str != null ? encoder.encodeToString(str.getBytes()) : null;
31+
}
32+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache license, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the license for the specific language governing permissions and
15+
* limitations under the license.
16+
*/
17+
package org.apache.logging.log4j.util;
18+
19+
import java.lang.reflect.Method;
20+
21+
import org.apache.logging.log4j.status.StatusLogger;
22+
23+
/**
24+
*
25+
*/
26+
public class Base64Util {
27+
28+
private static Method encodeMethod = null;
29+
private static Object encoder = null;
30+
31+
static {
32+
try {
33+
Class<?> clazz = LoaderUtil.loadClass("java.util.Base64");
34+
Class<?> encoderClazz = LoaderUtil.loadClass("java.util.Base64$Encoder");
35+
Method method = clazz.getMethod("getEncoder");
36+
encoder = method.invoke(null);
37+
encodeMethod = encoderClazz.getMethod("encodeToString", byte[].class);
38+
} catch (Exception ex) {
39+
try {
40+
Class<?> clazz = LoaderUtil.loadClass("javax.xml.bind.DataTypeConverter");
41+
encodeMethod = clazz.getMethod("printBase64Binary");
42+
} catch (Exception ex2) {
43+
LowLevelLogUtil.logException("Unable to create a Base64 Encoder", ex2);
44+
}
45+
}
46+
}
47+
48+
public static String encode(String str) {
49+
if (str == null) {
50+
return null;
51+
}
52+
byte [] data = str.getBytes();
53+
if (encodeMethod != null) {
54+
try {
55+
return (String) encodeMethod.invoke(encoder, data);
56+
} catch (Exception ex) {
57+
StatusLogger.getLogger().warn("Unable to encode String: " + ex.getMessage());
58+
return str;
59+
}
60+
}
61+
StatusLogger.getLogger().warn("No Encoder, unable to encode string");
62+
return str;
63+
}
64+
}

log4j-api/src/main/java/org/apache/logging/log4j/util/PropertiesUtil.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@
4646
public final class PropertiesUtil {
4747

4848
private static final String LOG4J_PROPERTIES_FILE_NAME = "log4j2.component.properties";
49+
private static final String LOG4J_SYSTEM_PROPERTIES_FILE_NAME = "log4j2.system.properties";
50+
private static final String SYSTEM = "system:";
4951
private static final PropertiesUtil LOG4J_PROPERTIES = new PropertiesUtil(LOG4J_PROPERTIES_FILE_NAME);
5052

5153
private final Environment environment;
@@ -178,9 +180,9 @@ public Charset getCharsetProperty(final String name, final Charset defaultValue)
178180
if (Charset.isSupported(charsetName)) {
179181
return Charset.forName(charsetName);
180182
}
181-
ResourceBundle bundle = getCharsetsResourceBundle();
183+
final ResourceBundle bundle = getCharsetsResourceBundle();
182184
if (bundle.containsKey(name)) {
183-
String mapped = bundle.getString(name);
185+
final String mapped = bundle.getString(name);
184186
if (Charset.isSupported(mapped)) {
185187
return Charset.forName(mapped);
186188
}
@@ -315,6 +317,19 @@ private static class Environment {
315317
private final Map<List<CharSequence>, String> tokenized = new ConcurrentHashMap<>();
316318

317319
private Environment(final PropertySource propertySource) {
320+
PropertyFilePropertySource sysProps = new PropertyFilePropertySource(LOG4J_SYSTEM_PROPERTIES_FILE_NAME);
321+
try {
322+
sysProps.forEach(new BiConsumer<String, String>() {
323+
@Override
324+
public void accept(String key, String value) {
325+
if (System.getProperty(key) == null) {
326+
System.setProperty(key, value);
327+
}
328+
}
329+
});
330+
} catch (SecurityException ex) {
331+
// Access to System Properties is restricted so just skip it.
332+
}
318333
sources.add(propertySource);
319334
for (final ClassLoader classLoader : LoaderUtil.getClassLoaders()) {
320335
try {
@@ -327,6 +342,7 @@ private Environment(final PropertySource propertySource) {
327342
*/
328343
}
329344
}
345+
330346
reload();
331347
}
332348

log4j-api/src/test/java/org/apache/logging/log4j/Timer.java renamed to log4j-api/src/main/java/org/apache/logging/log4j/util/Timer.java

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,11 @@
1414
* See the license for the specific language governing permissions and
1515
* limitations under the license.
1616
*/
17-
package org.apache.logging.log4j;
17+
package org.apache.logging.log4j.util;
1818

1919
import java.io.Serializable;
2020
import java.text.DecimalFormat;
2121

22-
import org.apache.logging.log4j.util.StringBuilderFormattable;
23-
import org.apache.logging.log4j.util.Strings;
24-
2522
/**
2623
*
2724
*/
@@ -30,13 +27,20 @@ public class Timer implements Serializable, StringBuilderFormattable
3027
private static final long serialVersionUID = 9175191792439630013L;
3128

3229
private final String name; // The timer's name
33-
private String status; // The timer's status
34-
private long startTime; // The start time
30+
public enum Status {
31+
Started, Stopped, Paused
32+
}
33+
private Status status; // The timer's status
3534
private long elapsedTime; // The elapsed time
3635
private final int iterations;
3736
private static long NANO_PER_SECOND = 1000000000L;
3837
private static long NANO_PER_MINUTE = NANO_PER_SECOND * 60;
3938
private static long NANO_PER_HOUR = NANO_PER_MINUTE * 60;
39+
private ThreadLocal<Long> startTime = new ThreadLocal<Long>() {
40+
@Override protected Long initialValue() {
41+
return 0L;
42+
}
43+
};
4044

4145

4246
/**
@@ -56,48 +60,56 @@ public Timer(final String name)
5660
public Timer(final String name, final int iterations)
5761
{
5862
this.name = name;
59-
startTime = 0;
60-
status = "Stopped";
63+
status = Status.Stopped;
6164
this.iterations = (iterations > 0) ? iterations : 0;
6265
}
6366

6467
/**
6568
* Start the timer.
6669
*/
67-
public void start()
70+
public synchronized void start()
6871
{
69-
startTime = System.nanoTime();
72+
startTime.set(System.nanoTime());
7073
elapsedTime = 0;
71-
status = "Start";
74+
status = Status.Started;
75+
}
76+
77+
public synchronized void startOrResume() {
78+
if (status == Status.Stopped) {
79+
start();
80+
} else {
81+
resume();
82+
}
7283
}
7384

7485
/**
7586
* Stop the timer.
7687
*/
77-
public void stop()
88+
public synchronized String stop()
7889
{
79-
elapsedTime += System.nanoTime() - startTime;
80-
startTime = 0;
81-
status = "Stop";
90+
elapsedTime += System.nanoTime() - startTime.get();
91+
startTime.set(0L);
92+
status = Status.Stopped;
93+
return toString();
8294
}
8395

8496
/**
8597
* Pause the timer.
8698
*/
87-
public void pause()
99+
public synchronized void pause()
88100
{
89-
elapsedTime += System.nanoTime() - startTime;
90-
startTime = 0;
91-
status = "Pause";
101+
elapsedTime += System.nanoTime() - startTime.get();
102+
startTime.set(0L);
103+
status = Status.Paused;
92104
}
93105

94106
/**
95107
* Resume the timer.
96108
*/
97-
public void resume()
109+
public synchronized void resume()
98110
{
99-
startTime = System.nanoTime();
100-
status = "Resume";
111+
startTime.set(System.nanoTime());
112+
status = Status.Started;
101113
}
102114

103115
/**
@@ -134,7 +146,7 @@ public long getElapsedNanoTime()
134146
* Resume).
135147
* @return the string representing the last operation performed.
136148
*/
137-
public String getStatus()
149+
public Status getStatus()
138150
{
139151
return status;
140152
}
@@ -154,16 +166,13 @@ public String toString()
154166
public void formatTo(final StringBuilder buffer) {
155167
buffer.append("Timer ").append(name);
156168
switch (status) {
157-
case "Start":
169+
case Started:
158170
buffer.append(" started");
159171
break;
160-
case "Pause":
172+
case Paused:
161173
buffer.append(" paused");
162174
break;
163-
case "Resume":
164-
buffer.append(" resumed");
165-
break;
166-
case "Stop":
175+
case Stopped:
167176
long nanoseconds = elapsedTime;
168177
// Get elapsed hours
169178
long hours = nanoseconds / NANO_PER_HOUR;
@@ -262,7 +271,8 @@ public int hashCode() {
262271
int result;
263272
result = (name != null ? name.hashCode() : 0);
264273
result = 29 * result + (status != null ? status.hashCode() : 0);
265-
result = 29 * result + (int) (startTime ^ (startTime >>> 32));
274+
long time = startTime.get();
275+
result = 29 * result + (int) (time ^ (time >>> 32));
266276
result = 29 * result + (int) (elapsedTime ^ (elapsedTime >>> 32));
267277
return result;
268278
}

log4j-api/src/test/java/org/apache/logging/log4j/ThreadContextUtilityClass.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import java.util.Map;
2020

21-
import org.apache.logging.log4j.Timer;
21+
import org.apache.logging.log4j.util.Timer;
2222
import org.apache.logging.log4j.ThreadContext;
2323
import static org.junit.Assert.*;
2424

log4j-api/src/test/java/org/apache/logging/log4j/message/MessageFormatsPerfTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package org.apache.logging.log4j.message;
1818

19-
import org.apache.logging.log4j.Timer;
19+
import org.apache.logging.log4j.util.Timer;
2020
import org.junit.AfterClass;
2121
import org.junit.Test;
2222

log4j-api/src/test/java/org/apache/logging/log4j/util/PropertiesUtilTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,13 @@ public void testNonStringSystemProperties() {
108108
System.getProperties().remove(key2);
109109
}
110110
}
111+
112+
@Test
113+
public void testPublish() {
114+
final Properties props = new Properties();
115+
final PropertiesUtil util = new PropertiesUtil(props);
116+
String value = System.getProperty("Application");
117+
assertNotNull("System property was not published", value);
118+
assertEquals("Log4j", value);
119+
}
111120
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Application=Log4j

log4j-core-its/src/test/java/org/apache/logging/log4j/core/SimplePerfTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import org.apache.logging.log4j.Level;
2424
import org.apache.logging.log4j.LogManager;
25-
import org.apache.logging.log4j.Timer;
25+
import org.apache.logging.log4j.util.Timer;
2626
import org.apache.logging.log4j.categories.PerformanceTests;
2727
import org.apache.logging.log4j.core.config.Configuration;
2828
import org.apache.logging.log4j.core.config.DefaultConfiguration;

log4j-core-its/src/test/java/org/apache/logging/log4j/core/ThreadedPerfTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121

2222
import org.apache.logging.log4j.Level;
2323
import org.apache.logging.log4j.LogManager;
24-
import org.apache.logging.log4j.Timer;
2524
import org.apache.logging.log4j.categories.PerformanceTests;
25+
import org.apache.logging.log4j.util.Timer;
2626
import org.junit.Test;
2727
import org.junit.experimental.categories.Category;
2828

0 commit comments

Comments
 (0)