View Javadoc
1   /*
2    * Licensed under the GPL License. You may not use this file except in compliance with the License.
3    * You may obtain a copy of the License at
4    *
5    *   https://www.gnu.org/licenses/old-licenses/gpl-2.0.html
6    *
7    * THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
8    * WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
9    * PURPOSE.
10   */
11  package psiprobe.tools.logging.commons;
12  
13  import psiprobe.tools.Instruments;
14  import psiprobe.tools.logging.DefaultAccessor;
15  import psiprobe.tools.logging.jdk.Jdk14LoggerAccessor;
16  import psiprobe.tools.logging.log4j.Log4JLoggerAccessor;
17  
18  /**
19   * The Class AbstractLoggerAccessorVisitor.
20   */
21  public abstract class AbstractLoggerAccessorVisitor extends DefaultAccessor {
22  
23    /**
24     * Visit.
25     */
26    public void visit() {
27      Object logger = Instruments.getField(getTarget(), "logger");
28      if (logger != null) {
29        if ("org.apache.log4j.Logger".equals(logger.getClass().getName())) {
30          Object level = Instruments.getField(logger, "level");
31          if (level == null) {
32            // This Logger is part of the slf4j bridge.
33            return;
34          }
35          while (logger != null) {
36            Log4JLoggerAccessor accessor = new Log4JLoggerAccessor();
37            accessor.setTarget(logger);
38            accessor.setApplication(getApplication());
39            accessor.setContext(true);
40            visit(accessor);
41            logger = invokeMethod(logger, "getParent", null, null);
42          }
43        } else if ("java.util.logging.Logger".equals(logger.getClass().getName())) {
44          while (logger != null) {
45            Jdk14LoggerAccessor accessor = new Jdk14LoggerAccessor();
46            accessor.setTarget(logger);
47            accessor.setApplication(getApplication());
48            accessor.setContext(true);
49            visit(accessor);
50            logger = invokeMethod(logger, "getParent", null, null);
51          }
52        }
53      }
54    }
55  
56    /**
57     * Visit.
58     *
59     * @param accessor the accessor
60     */
61    public abstract void visit(Log4JLoggerAccessor accessor);
62  
63    /**
64     * Visit.
65     *
66     * @param accessor the accessor
67     */
68    public abstract void visit(Jdk14LoggerAccessor accessor);
69  
70  }