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.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   * The Class Log4JManagerAccessor.
25   */
26  public class Log4JManagerAccessor extends DefaultAccessor {
27  
28    /**
29     * Instantiates a new log4 j manager accessor.
30     *
31     * @param cl the cl
32     *
33     * @throws ClassNotFoundException the class not found exception
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     * Gets the root logger.
46     *
47     * @return the root logger
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     * Gets the logger.
71     *
72     * @param name the name
73     *
74     * @return the logger
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     * Gets the appenders.
98     *
99     * @return the appenders
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 }