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