1
2
3
4
5
6
7
8
9
10
11 package psiprobe.tools.logging.slf4jlogback;
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
34 public class TomcatSlf4jLogbackFactoryAccessor extends DefaultAccessor {
35
36
37
38
39
40
41
42
43
44
45 public TomcatSlf4jLogbackFactoryAccessor(ClassLoader cl)
46 throws ClassNotFoundException, IllegalAccessException, InvocationTargetException {
47
48
49 Class<?> clazz = cl.loadClass("org.apache.juli.logging.org.slf4j.impl.StaticLoggerBinder");
50 Method getSingleton = MethodUtils.getAccessibleMethod(clazz, "getSingleton");
51 Object singleton = getSingleton.invoke(null);
52 Method getLoggerFactory = MethodUtils.getAccessibleMethod(clazz, "getLoggerFactory");
53
54 Object loggerFactory = getLoggerFactory.invoke(singleton);
55
56
57 Class<?> loggerFactoryClass =
58 cl.loadClass("org.apache.juli.logging.ch.qos.logback.classic.LoggerContext");
59 if (!loggerFactoryClass.isInstance(loggerFactory)) {
60 throw new RuntimeException("The singleton SLF4J binding was not Logback");
61 }
62 setTarget(loggerFactory);
63 }
64
65
66
67
68
69
70 public TomcatSlf4jLogbackLoggerAccessor getRootLogger() {
71
72
73
74
75 return getLogger("ROOT");
76 }
77
78
79
80
81
82
83
84
85 public TomcatSlf4jLogbackLoggerAccessor getLogger(String name) {
86 try {
87 Class<? extends Object> clazz = getTarget().getClass();
88 Method getLogger = MethodUtils.getAccessibleMethod(clazz, "getLogger", String.class);
89
90 Object logger = getLogger.invoke(getTarget(), name);
91 if (logger == null) {
92 throw new NullPointerException(getTarget() + ".getLogger(\"" + name + "\") returned null");
93 }
94 TomcatSlf4jLogbackLoggerAccessor accessor = new TomcatSlf4jLogbackLoggerAccessor();
95 accessor.setTarget(logger);
96 accessor.setApplication(getApplication());
97 return accessor;
98
99 } catch (Exception e) {
100 logger.error("{}.getLogger('{}') failed", getTarget(), name, e);
101 }
102 return null;
103 }
104
105
106
107
108
109
110
111 @SuppressWarnings("unchecked")
112 public List<TomcatSlf4jLogbackAppenderAccessor> getAppenders() {
113 List<TomcatSlf4jLogbackAppenderAccessor> appenders = new ArrayList<>();
114 try {
115 Class<? extends Object> clazz = getTarget().getClass();
116 Method getLoggerList = MethodUtils.getAccessibleMethod(clazz, "getLoggerList");
117
118 List<Object> loggers = (List<Object>) getLoggerList.invoke(getTarget());
119 for (Object logger : loggers) {
120 TomcatSlf4jLogbackLoggerAccessor accessor = new TomcatSlf4jLogbackLoggerAccessor();
121 accessor.setTarget(logger);
122 accessor.setApplication(getApplication());
123
124 appenders.addAll(accessor.getAppenders());
125 }
126 } catch (Exception e) {
127 logger.error("{}.getLoggerList() failed", getTarget(), e);
128 }
129 return appenders;
130 }
131
132 }