1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.logging.log4j2;
12
13 import com.google.common.base.Strings;
14
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Map;
18
19 import org.apache.commons.lang3.reflect.MethodUtils;
20
21 import psiprobe.tools.logging.DefaultAccessor;
22
23
24
25
26 public class Log4J2LoggerConfigAccessor extends DefaultAccessor {
27
28
29 private boolean context;
30
31
32 private Log4J2LoggerContextAccessor loggerContext;
33
34
35 private Map<String, Object> appenderMap;
36
37
38
39
40
41
42 @Override
43 @SuppressWarnings("unchecked")
44 public void setTarget(Object target) {
45 super.setTarget(target);
46
47 try {
48 this.appenderMap = (Map<String, Object>) invokeMethod(target, "getAppenders", null, null);
49 } catch (Exception e) {
50 logger.error("{}#getAppenders() failed", target.getClass().getName(), e);
51 }
52 }
53
54
55
56
57
58
59 public List<Log4J2AppenderAccessor> getAppenders() {
60 List<Log4J2AppenderAccessor> appenders = new ArrayList<>();
61 if (appenderMap != null) {
62 for (Object unwrappedAppender : appenderMap.values()) {
63 Log4J2AppenderAccessor appender = wrapAppender(unwrappedAppender);
64 if (appender != null) {
65 appenders.add(appender);
66 }
67 }
68 }
69 return appenders;
70 }
71
72
73
74
75
76
77
78
79 public Log4J2AppenderAccessor getAppender(String name) {
80 if (this.appenderMap != null) {
81 Object appender = appenderMap.get(name);
82 return wrapAppender(appender);
83 }
84 return null;
85 }
86
87
88
89
90
91
92 public boolean isContext() {
93 return context;
94 }
95
96
97
98
99
100
101 public void setContext(boolean context) {
102 this.context = context;
103 }
104
105
106
107
108
109
110 public void setLoggerContext(Log4J2LoggerContextAccessor loggerContext) {
111 this.loggerContext = loggerContext;
112 }
113
114
115
116
117
118
119 public boolean isRoot() {
120 return Strings.isNullOrEmpty(getName());
121 }
122
123
124
125
126
127
128 public String getName() {
129 return (String) getProperty(getTarget(), "name", null);
130 }
131
132
133
134
135
136
137 public String getLevel() {
138 try {
139 Object level = MethodUtils.invokeMethod(getTarget(), "getLevel");
140 return (String) MethodUtils.invokeMethod(level, "toString");
141 } catch (Exception e) {
142 logger.error("{}#getLevel() failed", getTarget().getClass().getName(), e);
143 }
144 return null;
145 }
146
147
148
149
150
151
152 public void setLevel(String newLevelStr) {
153 try {
154 Object level = MethodUtils.invokeMethod(getTarget(), "getLevel");
155 Object newLevel = MethodUtils.invokeMethod(level, "toLevel", newLevelStr);
156 MethodUtils.invokeMethod(getTarget(), "setLevel", newLevel);
157 loggerContext.updateLoggers();
158 } catch (Exception e) {
159 logger.error("{}#setLevel('{}') failed", getTarget().getClass().getName(), newLevelStr, e);
160 }
161 }
162
163
164
165
166
167
168
169
170 private Log4J2AppenderAccessor wrapAppender(Object appender) {
171 try {
172 if (appender == null) {
173 throw new IllegalArgumentException("appender is null");
174 }
175 Log4J2AppenderAccessor appenderAccessor = new Log4J2AppenderAccessor();
176 appenderAccessor.setTarget(appender);
177 appenderAccessor.setLoggerAccessor(this);
178 appenderAccessor.setApplication(getApplication());
179 return appenderAccessor;
180 } catch (Exception e) {
181 logger.error("Could not wrap appender: {}", appender, e);
182 }
183 return null;
184 }
185
186 }