1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.async;
18
19 import org.apache.logging.log4j.Level;
20 import org.apache.logging.log4j.Logger;
21 import org.apache.logging.log4j.core.util.Loader;
22 import org.apache.logging.log4j.status.StatusLogger;
23 import org.apache.logging.log4j.util.PropertiesUtil;
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45 public class AsyncQueueFullPolicyFactory {
46 static final String PROPERTY_NAME_ASYNC_EVENT_ROUTER = "log4j2.AsyncQueueFullPolicy";
47 static final String PROPERTY_VALUE_DEFAULT_ASYNC_EVENT_ROUTER = "Default";
48 static final String PROPERTY_VALUE_DISCARDING_ASYNC_EVENT_ROUTER = "Discard";
49 static final String PROPERTY_NAME_DISCARDING_THRESHOLD_LEVEL = "log4j2.DiscardThreshold";
50
51 private static final Logger LOGGER = StatusLogger.getLogger();
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 public static AsyncQueueFullPolicy create() {
68 final String router = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME_ASYNC_EVENT_ROUTER);
69 if (router == null || isRouterSelected(
70 router, DefaultAsyncQueueFullPolicy.class, PROPERTY_VALUE_DEFAULT_ASYNC_EVENT_ROUTER)) {
71 return new DefaultAsyncQueueFullPolicy();
72 }
73 if (isRouterSelected(
74 router, DiscardingAsyncQueueFullPolicy.class, PROPERTY_VALUE_DISCARDING_ASYNC_EVENT_ROUTER)) {
75 return createDiscardingAsyncQueueFullPolicy();
76 }
77 return createCustomRouter(router);
78 }
79
80 private static boolean isRouterSelected(
81 String propertyValue,
82 Class<? extends AsyncQueueFullPolicy> policy,
83 String shortPropertyValue) {
84 return propertyValue != null && (shortPropertyValue.equalsIgnoreCase(propertyValue)
85 || policy.getName().equals(propertyValue)
86 || policy.getSimpleName().equals(propertyValue));
87 }
88
89 private static AsyncQueueFullPolicy createCustomRouter(final String router) {
90 try {
91 final Class<? extends AsyncQueueFullPolicy> cls = Loader.loadClass(router).asSubclass(AsyncQueueFullPolicy.class);
92 LOGGER.debug("Creating custom AsyncQueueFullPolicy '{}'", router);
93 return cls.newInstance();
94 } catch (final Exception ex) {
95 LOGGER.debug("Using DefaultAsyncQueueFullPolicy. Could not create custom AsyncQueueFullPolicy '{}': {}", router,
96 ex.toString());
97 return new DefaultAsyncQueueFullPolicy();
98 }
99 }
100
101 private static AsyncQueueFullPolicy createDiscardingAsyncQueueFullPolicy() {
102 final PropertiesUtil util = PropertiesUtil.getProperties();
103 final String level = util.getStringProperty(PROPERTY_NAME_DISCARDING_THRESHOLD_LEVEL, Level.INFO.name());
104 final Level thresholdLevel = Level.toLevel(level, Level.INFO);
105 LOGGER.debug("Creating custom DiscardingAsyncQueueFullPolicy(discardThreshold:{})", thresholdLevel);
106 return new DiscardingAsyncQueueFullPolicy(thresholdLevel);
107 }
108 }