1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.util;
18
19 import java.util.Map;
20
21
22
23
24
25
26
27
28
29
30 public class EnvironmentPropertySource implements PropertySource {
31
32 private static final String PREFIX = "LOG4J_";
33 private static final int DEFAULT_PRIORITY = -100;
34
35 @Override
36 public int getPriority() {
37 return DEFAULT_PRIORITY;
38 }
39
40 @Override
41 public void forEach(final BiConsumer<String, String> action) {
42 final Map<String, String> getenv;
43 try {
44 getenv = System.getenv();
45 } catch (final SecurityException e) {
46
47 LowLevelLogUtil.logException(
48 "The system environment variables are not available to Log4j due to security restrictions: " + e,
49 e);
50 return;
51 }
52 for (final Map.Entry<String, String> entry : getenv.entrySet()) {
53 final String key = entry.getKey();
54 if (key.startsWith(PREFIX)) {
55 action.accept(key.substring(PREFIX.length()), entry.getValue());
56 }
57 }
58 }
59
60 @Override
61 public CharSequence getNormalForm(final Iterable<? extends CharSequence> tokens) {
62 final StringBuilder sb = new StringBuilder("LOG4J");
63 for (final CharSequence token : tokens) {
64 sb.append('_');
65 for (int i = 0; i < token.length(); i++) {
66 sb.append(Character.toUpperCase(token.charAt(i)));
67 }
68 }
69 return sb.toString();
70 }
71 }