1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.logging.log4j;
12
13 import java.lang.reflect.Method;
14 import java.util.ArrayList;
15 import java.util.Collections;
16 import java.util.Enumeration;
17 import java.util.List;
18
19 import org.apache.commons.lang3.reflect.MethodUtils;
20
21 import psiprobe.tools.logging.DefaultAccessor;
22
23
24
25
26 public class Log4JManagerAccessor extends DefaultAccessor {
27
28
29
30
31
32
33
34
35 public Log4JManagerAccessor(ClassLoader cl) throws ClassNotFoundException {
36 Class<?> clazz = cl.loadClass("org.apache.log4j.LogManager");
37 Method exists = MethodUtils.getAccessibleMethod(clazz, "exists", String.class);
38 if (exists == null) {
39 throw new RuntimeException("The LogManager is part of the slf4j bridge.");
40 }
41 setTarget(clazz);
42 }
43
44
45
46
47
48
49 public Log4JLoggerAccessor getRootLogger() {
50 try {
51 Class<?> clazz = (Class<?>) getTarget();
52 Method getRootLogger = MethodUtils.getAccessibleMethod(clazz, "getRootLogger");
53
54 Object logger = getRootLogger.invoke(null);
55 if (logger == null) {
56 throw new NullPointerException(
57 getTarget().getClass().getName() + "#getRootLogger() returned null");
58 }
59 Log4JLoggerAccessor accessor = new Log4JLoggerAccessor();
60 accessor.setTarget(logger);
61 accessor.setApplication(getApplication());
62 return accessor;
63 } catch (Exception e) {
64 logger.error("{}#getRootLogger() failed", getTarget().getClass().getName(), e);
65 }
66 return null;
67 }
68
69
70
71
72
73
74
75
76 public Log4JLoggerAccessor getLogger(String name) {
77 try {
78 Class<?> clazz = (Class<?>) getTarget();
79 Method getLogger = MethodUtils.getAccessibleMethod(clazz, "getLogger", String.class);
80
81 Object logger = getLogger.invoke(null, name);
82 if (logger == null) {
83 throw new NullPointerException(
84 getTarget().getClass().getName() + "#getLogger(\"" + name + "\") returned null");
85 }
86 Log4JLoggerAccessor accessor = new Log4JLoggerAccessor();
87 accessor.setTarget(logger);
88 accessor.setApplication(getApplication());
89 return accessor;
90 } catch (Exception e) {
91 logger.error("{}#getLogger('{}') failed", getTarget().getClass().getName(), name, e);
92 }
93 return null;
94 }
95
96
97
98
99
100
101 @SuppressWarnings("unchecked")
102 public List<Log4JAppenderAccessor> getAppenders() {
103 List<Log4JAppenderAccessor> appenders = new ArrayList<>();
104 try {
105 appenders.addAll(getRootLogger().getAppenders());
106
107 Class<?> clazz = (Class<?>) getTarget();
108 Method getCurrentLoggers = MethodUtils.getAccessibleMethod(clazz, "getCurrentLoggers");
109
110 for (Object currentLogger : Collections
111 .list((Enumeration<Object>) getCurrentLoggers.invoke(null))) {
112 Log4JLoggerAccessor accessor = new Log4JLoggerAccessor();
113 accessor.setTarget(currentLogger);
114 accessor.setApplication(getApplication());
115
116 appenders.addAll(accessor.getAppenders());
117 }
118 } catch (Exception e) {
119 logger.error("{}#getCurrentLoggers() failed", getTarget().getClass().getName(), e);
120 }
121 return appenders;
122 }
123
124 }