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.util.ArrayList;
14  import java.util.Collections;
15  import java.util.Enumeration;
16  import java.util.List;
17  
18  import org.apache.commons.lang3.reflect.MethodUtils;
19  
20  import psiprobe.tools.logging.DefaultAccessor;
21  
22  /**
23   * The Class Log4JLoggerAccessor.
24   */
25  public class Log4JLoggerAccessor extends DefaultAccessor {
26  
27    /** The context. */
28    private boolean context;
29  
30    /**
31     * Gets the appenders.
32     *
33     * @return the appenders
34     */
35    @SuppressWarnings("unchecked")
36    public List<Log4JAppenderAccessor> getAppenders() {
37      List<Log4JAppenderAccessor> appenders = new ArrayList<>();
38      try {
39        for (Object unwrappedAppender : Collections
40            .list((Enumeration<Object>) MethodUtils.invokeMethod(getTarget(), "getAllAppenders"))) {
41          Log4JAppenderAccessor appender = wrapAppender(unwrappedAppender);
42          if (appender != null) {
43            appenders.add(appender);
44          }
45        }
46      } catch (Exception e) {
47        logger.error("{}#getAllAppenders() failed", getTarget().getClass().getName(), e);
48      }
49      return appenders;
50    }
51  
52    /**
53     * Gets the appender.
54     *
55     * @param name the name
56     *
57     * @return the appender
58     */
59    public Log4JAppenderAccessor getAppender(String name) {
60      try {
61        Object appender = MethodUtils.invokeMethod(getTarget(), "getAppender", name);
62        return wrapAppender(appender);
63      } catch (Exception e) {
64        logger.error("{}#getAppender() failed", getTarget().getClass().getName(), e);
65      }
66      return null;
67    }
68  
69    /**
70     * Checks if is context.
71     *
72     * @return true, if is context
73     */
74    public boolean isContext() {
75      return context;
76    }
77  
78    /**
79     * Sets the context.
80     *
81     * @param context the new context
82     */
83    public void setContext(boolean context) {
84      this.context = context;
85    }
86  
87    /**
88     * Checks if is root.
89     *
90     * @return true, if is root
91     */
92    public boolean isRoot() {
93      return "root".equals(getName()) && "org.apache.log4j.spi.RootLogger".equals(getTargetClass());
94    }
95  
96    /**
97     * Gets the name.
98     *
99     * @return the name
100    */
101   public String getName() {
102     return (String) getProperty(getTarget(), "name", null);
103   }
104 
105   /**
106    * Gets the level.
107    *
108    * @return the level
109    */
110   public String getLevel() {
111     try {
112       Object level = MethodUtils.invokeMethod(getTarget(), "getLevel");
113       return (String) MethodUtils.invokeMethod(level, "toString");
114     } catch (Exception e) {
115       logger.error("{}#getLevel() failed", getTarget().getClass().getName(), e);
116     }
117     return null;
118   }
119 
120   /**
121    * Sets the level.
122    *
123    * @param newLevelStr the new level
124    */
125   public void setLevel(String newLevelStr) {
126     try {
127       Object level = MethodUtils.invokeMethod(getTarget(), "getLevel");
128       Object newLevel = MethodUtils.invokeMethod(level, "toLevel", newLevelStr);
129       MethodUtils.invokeMethod(getTarget(), "setLevel", newLevel);
130     } catch (Exception e) {
131       logger.error("{}#setLevel('{}') failed", getTarget().getClass().getName(), newLevelStr, e);
132     }
133   }
134 
135   /**
136    * Wrap appender.
137    *
138    * @param appender the appender
139    *
140    * @return the log4 j appender accessor
141    */
142   private Log4JAppenderAccessor wrapAppender(Object appender) {
143     try {
144       if (appender == null) {
145         throw new IllegalArgumentException("appender is null");
146       }
147       Log4JAppenderAccessor appenderAccessor = new Log4JAppenderAccessor();
148       appenderAccessor.setTarget(appender);
149       appenderAccessor.setLoggerAccessor(this);
150       appenderAccessor.setApplication(getApplication());
151       return appenderAccessor;
152     } catch (Exception e) {
153       logger.error("Could not wrap appender: {}", appender, e);
154     }
155     return null;
156   }
157 
158 }