1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.logging.logback13;
12
13 import java.lang.reflect.InvocationTargetException;
14 import java.lang.reflect.Method;
15 import java.util.ArrayList;
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
26
27
28
29
30
31
32
33 public class Logback13FactoryAccessor extends DefaultAccessor {
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public Logback13FactoryAccessor(ClassLoader cl)
48 throws ClassNotFoundException, IllegalAccessException, InvocationTargetException,
49 NoSuchMethodException, SecurityException, IllegalArgumentException {
50
51
52 final List<?> providers = findServiceProviders(cl);
53 if (providers.isEmpty()) {
54 throw new RuntimeException("The SLF4J provider binding was not Logback");
55 }
56
57
58 Object provider = providers.get(0);
59
60
61 Method initialize = MethodUtils.getAccessibleMethod(provider.getClass(), "initialize");
62 initialize.invoke(provider);
63
64
65 Method getLoggerFactory =
66 MethodUtils.getAccessibleMethod(provider.getClass(), "getLoggerFactory");
67 Object loggerFactory = getLoggerFactory.invoke(provider);
68
69
70 Class<?> loggerFactoryClass = cl.loadClass("ch.qos.logback.classic.LoggerContext");
71 if (!loggerFactoryClass.isInstance(loggerFactory)) {
72 throw new RuntimeException("The SLF4J provider binding was not Logback");
73 }
74 setTarget(loggerFactory);
75 }
76
77
78
79
80
81
82 public Logback13LoggerAccessor getRootLogger() {
83
84
85 return getLogger("ROOT");
86 }
87
88
89
90
91
92
93
94
95 public Logback13LoggerAccessor getLogger(String name) {
96 try {
97 Class<? extends Object> clazz = getTarget().getClass();
98 Method getLogger = MethodUtils.getAccessibleMethod(clazz, "getLogger", String.class);
99
100 Object logger = getLogger.invoke(getTarget(), name);
101 if (logger == null) {
102 throw new NullPointerException(getTarget() + ".getLogger(\"" + name + "\") returned null");
103 }
104 Logback13LoggerAccessor accessor = new Logback13LoggerAccessor();
105 accessor.setTarget(logger);
106 accessor.setApplication(getApplication());
107 return accessor;
108
109 } catch (Exception e) {
110 logger.error("{}.getLogger('{}') failed", getTarget(), name, e);
111 }
112 return null;
113 }
114
115
116
117
118
119
120 @SuppressWarnings("unchecked")
121 public List<Logback13AppenderAccessor> getAppenders() {
122 List<Logback13AppenderAccessor> appenders = new ArrayList<>();
123 try {
124 Class<? extends Object> clazz = getTarget().getClass();
125 Method getLoggerList = MethodUtils.getAccessibleMethod(clazz, "getLoggerList");
126
127 List<Object> loggers = (List<Object>) getLoggerList.invoke(getTarget());
128 for (Object logger : loggers) {
129 Logback13LoggerAccessor accessor = new Logback13LoggerAccessor();
130 accessor.setTarget(logger);
131 accessor.setApplication(getApplication());
132
133 appenders.addAll(accessor.getAppenders());
134 }
135 } catch (Exception e) {
136 logger.error("{}.getLoggerList() failed", getTarget(), e);
137 }
138 return appenders;
139 }
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155 private static List<?> findServiceProviders(final ClassLoader cl)
156 throws NoSuchMethodException, SecurityException, ClassNotFoundException,
157 IllegalAccessException, IllegalArgumentException, InvocationTargetException {
158 final Class<?> loggerFactory = cl.loadClass("org.slf4j.LoggerFactory");
159 final Method findServiceProviders = loggerFactory.getDeclaredMethod("findServiceProviders");
160
161 findServiceProviders.setAccessible(true);
162 final List<?> providers = (List<?>) findServiceProviders.invoke(null);
163 findServiceProviders.setAccessible(false);
164 return providers;
165 }
166
167 }